What does @media screen and (max-width: 1024px) mean in CSS?
That’s a media query. It prevents the CSS inside it from being run unless the browser passes the tests it contains.
The tests in this media query are:
@media screen
— The browser identifies itself as being in the “screen” category. This roughly means the browser considers itself desktop-class — as opposed to e.g. an older mobile phone browser (note that the iPhone, and other smartphone browsers, do identify themselves as being in the screen category), or a screenreader — and that it’s displaying the page on-screen, rather than printing it.max-width: 1024px
— the width of the browser window (including the scroll bar) is 1024 pixels or less. (CSS pixels, not device pixels.)
That second test suggests this is intended to limit the CSS to the iPad, iPhone, and similar devices (because some older browsers don’t support max-width
in media queries, and a lot of desktop browsers are run wider than 1024 pixels).
However, it will also apply to desktop browser windows less than 1024 pixels wide, in browsers that support the max-width
media query.
Here’s the Media Queries spec, it’s pretty readable:
- http://www.w3.org/TR/css3-mediaqueries/
@media not working with width less than 1024px
Every rule in CSS is able to override any previous rule to the same selector. So you just need to switch your code in order to get it working:
@media (max-width: 1023.9px) {
width: 33.3333%;
}
// experimental
@media (max-width: 1000px) {
width: 50%;
}
@media (max-width: 768px) {
width: 50%;
}
@media (max-width: 599px) {
width: 100%;
}
//
@media (min-width: 1024px) {
width: 25%;
}
The reason why your rules override each other is because they all have the same selector and while max-width: 599px
is accurate and correct, the later appearing max-width: 1023.9px
is it, too and thus it’s overriding the previous width: 100%;
from the max-width: 599px
media query.
And a side note here: Use integer values only for media queries. There is no screen in the world, which has .9
or even .5
pixels.
In Wordpress CSS Styling @media screen and (max-width: 1024px) will not work
Your max-width: 1024px
query must be placed before the max-width: 425px
query in the code, otherwise, as an expected result of CSS specificity, overriding will occur.
Demo of wrong order:
#imgWrapper { border: 1px dashed red; padding: 10px; position: relative; }
#imgWrapper::after { content:"Default - Desktop/Large Screen"; }
@media only screen and (max-width: 425px) { #imgWrapper { position: relative; width: 50%; } #imgWrapper::after { content:"Max-425"; }}
@media only screen and (max-width: 1024px) { #imgWrapper { position: relative; width: 75%; } #imgWrapper::after { content:"Max-1024"; }}
<div id="imgWrapper">Media Query: </div>
@Media min-width & max-width
I've found the best method is to write your default CSS for the older browsers, as older browsers (including IE 5.5, 6, 7 and 8) can't read @media
. When I use @media
, I use it like this:
<style type="text/css">
/* default styles here for older browsers.
I tend to go for a 600px - 960px width max but using percentages
*/
@media only screen and (min-width: 960px) {
/* styles for browsers larger than 960px; */
}
@media only screen and (min-width: 1440px) {
/* styles for browsers larger than 1440px; */
}
@media only screen and (min-width: 2000px) {
/* for sumo sized (mac) screens */
}
@media only screen and (max-device-width: 480px) {
/* styles for mobile browsers smaller than 480px; (iPhone) */
}
@media only screen and (device-width: 768px) {
/* default iPad screens */
}
/* different techniques for iPad screening */
@media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:portrait) {
/* For portrait layouts only */
}
@media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:landscape) {
/* For landscape layouts only */
}
</style>
But you can do whatever you like with your @media
. This is just an example of what I've found best for me when building styles for all browsers.
iPad CSS specifications.
Also! If you're looking for printability you can use @media print{}
.
What does this line of CSS mean? @media only screen and (min-device-width: 320px) and (max-device-width: 480px)
It's called CSS3 Media Queries which is a technique to help your website adapt to various of screen dimensions or basically, it helps to deliver different styles to different devices.
Since you're new, let's break down your media query to two parts:
@media only screen
This means we will apply css styles to a device with a screen. The keyword only
used here to hide style sheets from older browsers from seeing phone style sheet.
and (min-device-width: 320px) and (max-device-width: 480px)
This is quite obvious since it means that the specified css only applied when a device has a screen's size with minimum 320px
and maximum of 480px
in width dimension.
Why are my media queries stepping on each other?
I think you are having hard time with orientation
remove orientation
and all works fine.
Check that snippet.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html, body {
height: 100%;
}
body {
background-color: yellow;
}
@media only screen and (min-width: 1024px) and (max-width: 1366px) {
body {
background-color: blue;
}
}
@media only screen and (min-width: 810px) and (max-width: 1080px) {
body {
background-color: red;
}
}
@media only screen and (min-width: 768px) and (max-width: 1024px) {
body {
background-color: pink;
}
}
CSS media queries: max-width OR max-height
Use a comma to specify two (or more) different rules:
@media screen and (max-width: 995px),
screen and (max-height: 700px) {
...
}
From https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries
Commas are used to combine multiple media queries into a single rule. Each query in a comma-separated list is treated separately from the others. Thus, if any of the queries in a list is true, the entire media statement returns true. In other words, lists behave like a logical or operator.
Related Topics
Select All Child Elements Recursively in Css
How to Use Svg as Content in a Pseudo Element ::Before or ::After
Difference Between :Focus and :Active
Css Media Queries - Order Matters
Is a Relative Path in a CSS File Relative to the CSS File
CSS - Center Two Images in CSS Side by Side
Changing the Width of Bootstrap Popover
Add CSS Cursor Property When Using "Pointer-Events: None"
Css3 Property Webkit-Overflow-Scrolling:Touch Error
Transition of Background-Color
How to Position an Element Fixed Relative to Parent
Does Element Width Include Padding
How to Clear the Cache of Nginx
How to Store Inherit Value Inside a CSS Variable (Aka Custom Property)
Using CSS Transitions in CSS Grid Layout
How to Style the Resize Grabber of Textarea
iOS 7 iPad Safari Landscape Innerheight/Outerheight Layout Issue