Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The min/max-width media query doesn't make grammatical sense

I'm finding the concept of the (min-width/max-width) media query a bit confusing.

Naturally if I was to design a media query I would want to say (in pseudo-code)....

if(screen.width < 420)
{
     ApplyStyle();
}

This concept of talking about min and max doesn't make any sense since the 'min-width' of something like a div element is a command not a question.

I know that the following is true when my screen goes below 420px...

@media screen and (max-width:420px) {


}

I just don't know why because the max width is something I tell it to have. If I have told it to have something why is css checking it? Surely it already knows it.

I'm perhaps missing the grammer/context here. Can someone please explain?

like image 573
Exitos Avatar asked Aug 25 '13 17:08

Exitos


People also ask

Should I use max width or min-width in media queries?

If you are designing your website for smaller devices first then set your initial breakpoints with min-width, whereas, if you are designing for larger devices first then use max-width. This post discusses min-width, and max-width media features in detail along with relevant examples.

What size media queries should I use?

In my experience, 320px, 768px, and 1200px are the most commonly used; these three values should be sufficient for targeting smart phones, tablets/laptops, and desktops, respectively.

What is max width in media query?

Taking a look at the CSS file, you'll notice that the media query has a minimum width of 320px and a maximum width of 576px . This just means that all the styles that will go into this rule will only be applicable to devices with extra-small and small widths.


2 Answers

min-width in media queries is not related to the min-width property you set on elements, those are two different things.

In media queries min-width: X is true if the viewport has a width greater or equal to X, effectively working as screen.width >= X. Obviously max-width would then be equal to screen.width <= X

To me it makes perfect sense, if you read @media screen and (max-width:420px) as a screen with a maximum width of 420px, so anything from 0 to 420px

like image 184
omma2289 Avatar answered Sep 29 '22 11:09

omma2289


Here is a simple example, hopefully it helps..

Say we have a website with the following media queries:

/* #1- Large desktop */
@media (min-width: 980px) { ... }

/* #2- Portrait tablet to landscape and desktop */
@media (min-width: 768px) and (max-width: 979px) { ... }

/* #3- Landscape phone to portrait tablet */
@media (max-width: 767px) { ... }

/* #4- Landscape phones and down */
@media (max-width: 480px) { ... }

If the screen size of the browser is 1200px, query #1 will be satisfied, as the minimum width of the browser has to be 980px for this query to be displayed.

Lets say we resize the browser now, and bring it all the way down to 250px.. query #4 is satisfied as the MAX is 480px..

Here is a simple translation of the queries..

@media (min-width: 980px) { ... }

Display if screen is greater than or equal to 980px

@media (min-width: 768px) and (max-width: 979px) { ... }

Display if screen is greater than or equal to 768px and less than or equal to 978px

@media (max-width: 767px) { ... }

Display if screen is greater than 480px and less than or equal to 767px.

@media (max-width: 480px) { ... }

Display if screen is less than or equal to 480px

Using these queries, you will always have a result, as one query is always satisfied.

like image 34
Josh Crozier Avatar answered Sep 29 '22 10:09

Josh Crozier