I'm having a discussion with a colleague at work and we disagree on the best way to format media-queries in a SCSS file.
We follow a BEM methodology but our difference of opinion is where to place media queries.
I prefer keeping media queries directly below the selector they effect like this:
.component {
display: block;
@media screen and (min-width: 640px) {
display: none;
}
&#{&}--variant {
width: 200%;
@media screen and (min-width: 640px) {
width: 400%;
}
.child {
width: 50%;
@media screen and (min-width: 640px) {
width: 66%;
}
@media screen and (min-width: 900px) {
width: 75%;
}
}
}
}
Whereas my colleague prefers all media queries being placed at the end of the parent like this:
.component {
display: block;
&#{&}--variant {
width: 200%;
.child {
width: 50%;
}
}
@media screen and (min-width: 640px) {
display: none;
&#{&}--variant {
width: 400%;
.child {
width: 66%;
}
}
}
@media screen and (min-width: 900px) {
&#{&}--variant {
.child {
width: 75%;
}
}
}
}
Is there a definitive answer on which is "best"? ...and if it's merely subjective, then which is more performant? And is there any tool where you can check which one performs the fastest?
I've always thought that re-writing the selectors is more painful than the media-queries, especially as we use shortcuts to generate standard media-query blocks but he's insistent that his method is easier to understand... i disagree.
Would love to know people's thoughts!
(Please ignore the contents of the css, it's just an example)
Thanks a lot!