FAQ: Media Queries - Range

This community-built FAQ covers the “Range” exercise from the lesson “Media Queries”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Learn Responsive Design

FAQs on the exercise Range

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

In the indication says:
The example above can be written using two separate rules as well:

@media only screen and (min-width: 320px) {
/* ruleset for >= 320px */
}

@media only screen and (min-width: 480px) {
/* ruleset for >= 480px */
}

it should’t be max-widht :480px ? to be equal to:

@media only screen and (min-width: 320px) and (max-width: 480px) {
/* ruleset for 320px - 480px */
}

4 Likes

How do you know which media query is applied if there are multiple?
In the example, the second media query (min-width: 480px) overrides the first (min-width: 320px). Is it just the further down a rule is in the css stylesheet, the higher priority?

3 Likes

Hi, not sure if you still need a response on this? I had the same question but (I think!) I worked it out during the process of trying to put it down in words :0) I think that it works like this:

  1. Width below 320px - no media query CSS rules executed
  2. Width meets or exceeds 320px - CSS rules stated in the 320px media query executed (e.g. set smaller font-size - which would drive any em or rem relative values that may have also been set up)
  3. Width meets or exceeds 480px - CSS rules stated in the 480px media query executed which would either override (for example move font-size back up to level that would be more appropriate to a wider screen) and/or introduce additional rules.
3 Likes

What other alternatives can be substituted with keyword “screen”, the exercises mentioned print and a couple others, but are there more alternatives to make a website better?

1 Like

Hi beta, I am thinking the same thing. why would it change from (min-width: 320px) and (max-width: 480px) to (min-width: 320px) and (min-width: 480px)?
Did you ever get an answer to this? I read the answer from microace but I did not undertand it.
Thanks,
Gilbert

I think they made a mistake here and the second example should be max-width, not min-width:

Hi, in this
@media only screen and (min-width: 320px) and (max-width: 480px) {

.page-title{

width: 270px;

}

.gallery-item .thumbnail{

width: 95%;

}

}

I don’t understand the image extends between 320px and 480px. However, it takes 95% of its parent element.

Well yeah, but that’s not the same thing as the first media query.

@media only screen and (min-width: 320px) and (max-width: 480px) {

}

The media query above will apply the CSS rules between its opening and closing braces only if the screen our web page is being viewed on has a width that is between 320px - 480px

1 Like

these ‘words’ are known as media types. They are simply generic categories of devices.

  • screen: any device with a screen (phone, tablet, laptop, etc.)
  • print: the screen shown in the print preview page
  • speech: something to do with screen readers (I’m not really sure - if anyone knows, please tell me)
  • all: every screen category

Hi,

Is there a standard set of media queries that are used to cover the range of all screen sizes?

Thanks,
James

There is no mistake. Second example is right.
We can use another variant too, but! If we use separate query min-width : 320px and separate query max-width : 480px, we override first media query up to 480px AND doesn’t override rules from first query for screens with with more then 480px!!! Hence the first query works for screens from 480px.
If we use min-width : 320px and separate query min-width : 480px. We add “upper bound” for the first query and override anything more than 480px width. Hence the first query works for screens with width from 320px to 480px.

2 Likes

No, there aren’t. If you find any online you probably shouldn’t use them.

Media queries should be written by yourself to make sure the website looks great on all screen sizes. The way to determine that is by shrinking your browser screen. Whenever you find elements in wrong positions when the screen size has shrunk, use a media query to fix it. Different websites will had completely different media queries for different screen sizes.

I’m sorry but I don’t understand how this:

@media only screen and (min-width: 320px) and (max-width: 480px) {
    /* ruleset for 320px - 480px */
}

is the same as this:

@media only screen and (min-width: 320px) { 
    /* ruleset for >= 320px */
}
@media only screen and (min-width: 480px) { 
    /* ruleset for >= 480px */
}

It is a little tricky to reconcile the two approaches, but playing around with an example was useful for me.
I did the following to better understand how one approach could substitute for the other:

  1. Go to the w3schools online editor/browser example for media query: Tryit Editor v3.7

  2. Use the media query

@media only screen and (min-width: 320px) and (max-width: 480px) {
    p {
        font-size: 40px;
    }
}

click the “Run” button and drag the (w3schools) browser pane to resize it (the dimensions of the browser pane are also shown). When the width is between 320px-480px, the paragraph’s font is 40px. Outside this range, the font defaults to medium (or 16px).

  1. Delete the above query and replace it with two queries:
@media only screen and (min-width: 320px) {
    p {
        font-size: 40px;
    }
}

@media only screen and (min-width: 480px) {
    p {
        font-size: 16px;
    }
}

Now click “Run” and then drag and resize the browser pane. Same result as the first approach is observed. When the width is in the range 320px-480px, font size of paragraph is 40px. If we go beyond 480px, then the second media query overrides the first query and font size reverts to 16px.

4 Likes

When we use these two media queries,

@media only screen and (min-width: 320px) {
    p {
        font-size: 40px;
    }
}

@media only screen and (min-width: 480px) {
    p {
        font-size: 16px;
    }
}

and the screen width is 600px, that qualifies for both media queries, as in 600 is larger than both 320 and 480. So when you say the second media query overrides the first query, is that because CSS always goes with the media query with the biggest possible dimension for the screen size?

No, it doesn’t have to do with the dimension. It is because the latest rule wins.
In the above snippet, the 480px rule comes after the 320px rule, so the font size will be 40px when width is in the range 320px-480px. If width goes over 480px, font size becomes 16px.

Suppose we reverse the order of the queries so that the 320px query is below the 480px query.

@media only screen and (min-width: 480px) {
    p {
        font-size: 16px;
    }
}

@media only screen and (min-width: 320px) {
    p {
        font-size: 40px;
    }
}

Now, the font size will be 40px for all widths above 320px. The 320px query will override the 480px query, because the 320px query is at the bottom. You can confirm this by playing around in the editor I mentioned in the post above (don’t forget to click Run after you make any edits). If you swap the media queries, you will see that from 320px onward, font size is 40px.

Furthermore, if you add a rule below the media queries, e.g.

// 320px media query

// 480px media query

// A relevant rule below the queries
p {
  font-size: 10px; 
}

In this case, the rule at the bottom will override both media queries and font will be 10px for all widths (again try it out in the editor). That is one of the reasons the media queries should be placed at the bottom.

3 Likes

i literally dont see any differences applying these media queries. it seems just normal as before applying query.

The range where the queries are applied is narrow (320px ->480px). You could help yourself with a wide flashy color border, who appears when you’re in this range.

Thank you!
I was just looking at this thinking that the website had made a mistake and put “min” instead of “max” but this explanation clears it up a lot.

Wish they had such a good explanation in the actual lesson!