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

CSS

Kieran Barker
Kieran Barker
15,028 Points

Best Practice for CSS Media Queries

When implementing CSS media queries, which of these two approaches would be better?

For readability, I think this option is better: have multiple of the same media query under different headings. But I wonder how this affects the loading speed. Here's an example:

/* Header
--------------------------------- */

@media screen and (min-width: 400px) {

  /* Some CSS rules for the header */

}

/* Something else
--------------------------------- */

@media screen and (min-width: 400px) {

  /* Some CSS rules for something else */

}

Notice in the above example that the media query is the same, but organised under different headings to improve readability.

The other option would be to use only one of each media query, and then put the headings under each media query. Personally, I find this harder to scan — but does it improve page loading speed? Which approach is better?

1 Answer

Max Senden
Max Senden
23,177 Points

Hi Kieran,

It's a bit of a curveball answer to your question, but regarding loading issues it's better to use SCSS (SASS) as it exports the sass into the most efficiently written CSS possible. The CSS output might not look pretty, but it does enable you to structure and divide your SASS code to your heart's content.

Look into it if you haven't. Treehouse has some excellent courses on it.

Cheers, Max

Kieran Barker
Kieran Barker
15,028 Points

Hi Max,

Thanks for your suggestion! I've been meaning to look into Sass for a while now. I'm not sure I'll be able to implement it at work — the other developers would need to agree — but I'd certainly like to try it in my personal projects.

Thanks again,

Kieran :)