Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Luke Lee
7,577 PointsDoes the order of media queries matter?
Is this css layout good?
/* Meida for print */
@media print {
...
}
/* Mobile - Portrait */
@media only screen and (max-width: 479px) {
...
}
/* Mobile - Landscape */
@media only screen and (min-width: 480px) and (max-width: 599px) {
...
}
/* Tablet - iPad */
@media only screen and (min-width: 600px) and (max-width: 959px) {
...
}
/* Desktop & Widescreen */
@media only screen and (min-width: 960px) {
...
}
I have seen some css codes are outside of any queries. Should I place them on top or bottom?
Thanks
3 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi Luke,
In your particular case I don't think the order will matter because you are using both min-width and max-width and there's no overlap between any of your media queries. So I don't think you'll have a situation where more than one media query takes effect at one time. For maintenance and readability reasons you should have them in a general increasing or decreasing order though. Even if you could put them out of order.
You should have some css outside of any media query because if the device doesn't support media queries, like older versions of IE, then you're not going to get any css applied.
In general, if you were taking a mobile first approach you would have all your site wide css and the css for small layouts at the beginning before any media queries. Then have media queries setup for increasingly wider widths where new styles will take effect.
Also, you don't necessarily need to use both min-width and max-width together. It depends on how you have your css setup and whether you want properties to carry over from one media query to another.
For example, if you had some styles that you wanted to apply for both "Mobile - Landscape" and "Table - iPad" you would have to repeat them in both media queries because only one is going to be active at any given width.
This is a contrived example but suppose you wanted paragraphs to be "red" in both of those media queries. You would have to do this:
/* Mobile - Landscape */ @media only screen and (min-width: 480px) and (max-width: 599px) {
p {
color: red;
}
}
/* Tablet - iPad */ @media only screen and (min-width: 600px) and (max-width: 959px) {
p {
color: red;
}
}
Whereas if you remove the max-width restriction then styles from "mobile - Landscape" can carry over and you can do this:
/* Mobile - Landscape */ @media only screen and (min-width: 480px) {
p {
color: red;
}
}
/* Tablet - iPad */ @media only screen and (min-width: 600px) {
/* Override any styles from Mobile - Landscape that you no longer wish to have here. The rest will carry over */
/* Add new styles here that are specific to this breakpoint */
}
The color of paragraphs will still remain red beyond 599px because both media queries are applied.
So it's probably a matter of personal preference and how you like to work but I'd say to do it in a way where you're not repeating css.
Laura Cressman
12,548 PointsLooks good to me! The thing about CSS is that the stylesheets are cascading (the "C" in CSS). This means that the property values are changed in the order that they are read. An example I gave in another post was that if we did something like div {background-color: red; background-color: blue;}, the background would be blue because the second property and value listed are applied last. Similarly, with media queries, you would want to make sure that the biggest screens are listed last, particularly with the min-width property, because any screen for instance that is 700 pixels wide would fall into both the min-width: 480px and min-width: 600px buckets. Since I'm assuming you want the min-width: 600px CSS applied to a screen that is 700 pixels wide, you would want to list that query after the min-width: 480px one.
TL;DR - lookin' good :)
Luke Lee
7,577 PointsGreat! They r really helpful. Just learned something new.