Skip to content

Instantly share code, notes, and snippets.

@vasilisvg
Created October 12, 2011 13:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vasilisvg/1281180 to your computer and use it in GitHub Desktop.
Save vasilisvg/1281180 to your computer and use it in GitHub Desktop.
Group media queries on component level, not on query level

Group media queries on component level

The common way to use media queries is to group all styling for a specific query. Leenull, a colleague of mine, came with the idea to use media queries on a component level. This might make make complex stylesheets easier to maintain.

I wonder what you think of it. Please let me know in the comments. Here's a link to a working example.

Grouping on a component level looks like this

.item1 {
    background: red;
}
@media (max-width:480px){
.item1 {
    background: green;
}
}

.item2 {
    background: blue;
}
@media (max-width:480px){
.item2 {
    background: yellow;
}
}

.item3 {
    background: purple;
}
@media (max-width:480px){
.item3 {
    background: papayawhip;
}
}

The classic way looks like this

.item1 {
    background: red;
}
.item2 {
    background: blue;
}
.item3 {
    background: purple;
}
@media (max-width:480px){
.item1 {
    background: green;
}
.item2 {
    background: yellow;
}
.item3 {
    background: papayawhip;
}
}
@roytomeij
Copy link

Here I am promoting my usual pet peeve, but: http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#media

Basically what you do here, but nested (clear indication of relationship) and compiled to one single query. I know it's by far not the two cents you're looking for, but I think it illustrates how you have a fine idea which just can't be executed in plain CSS :-)

@vasilisvg
Copy link
Author

@stijnj I asked Peter Beverloo on IRC if this would be a performance issue. He said that there is a performance cost but that it's too small to worry about.
@davidhund Isn't this the same discussion we used to have about conditional IE stylesheets vs. IE hacks?

@davidhund
Copy link

@vasilisvg exactly: except in this case I disagree ;)

@vasilisvg
Copy link
Author

@lekkerduidelijk I'm not sure why maintenance would be a problem with a mobile-first approach. Can you explain me what the difference is?

@roytomeij Interesting to see that this solution is seen as a best practice by the professionals!

@roytomeij
Copy link

@vasilisvg Aren't you a professional alike? I'm not putting myself above anyone else, so no need to call me a professional when I know we all are :-) I'm not telling you what a best practice is or what others in some circles have agreed on. It was a mere illustration that some things are shitty with CSS and will remain so until the syntax evolves, however you group them.

@vasilisvg
Copy link
Author

@roytomeij I consider the people who make Sass to be people who have thought really well about these issues. That's all I meant.

@roytomeij
Copy link

@vasilisvg Sorry, apparently I figured you meant something else than you did. Bad day I guess. Sorry again, highly value your insights.

@vasilisvg
Copy link
Author

@roytomeij No problem! (-:

@rutger1140
Copy link

@vasilisvg When building a stylesheet when you use the mobile first approach, all the mobile styles normally go on top. The other versioned websites are nicely grouped on the bottom. When they are scattered through the entire file it is harder to maintain it when stuff gets updated imo...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment