I found this piece of code in a CSS file I inherited, but I can’t make any sense out of it:
@media screen and (max-width: 1024px){
img.bg {
left: 50%;
margin-left: -512px; }
}
Specifically, what is happening on the first line?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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-widthin 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-widthmedia query.Here’s the Media Queries spec, it’s pretty readable: