Are there any issues (performance is my primary concern) if instead of defining css selectors within media queries (example 1), you define media queries within css selectors (example 2).
@media (min-width: 600px) {
.foo { ... }
.bar { ... }
.hello { ... }
.world{ ... }
}
@media (min-width: 1000px) {
.foo { ... }
.bar { ... }
.hello { ... }
.world{ ... }
}
@media (min-width: 1500px) {
.foo { ... }
.bar { ... }
.hello { ... }
.world{ ... }
}
.foo {
@media (min-width: 600px) { ... }
@media (min-width: 1000px) { ... }
@media (min-width: 1500px) { ... }
}
.bar {
@media (min-width: 600px) { ... }
@media (min-width: 1000px) { ... }
@media (min-width: 1500px) { ... }
}
.hello {
@media (min-width: 600px) { ... }
@media (min-width: 1000px) { ... }
@media (min-width: 1500px) { ... }
}
.world{
@media (min-width: 600px) { ... }
@media (min-width: 1000px) { ... }
@media (min-width: 1500px) { ... }
}
You may be wondering "why do this?". There are some limitations in LESS around extending classes across inside media queries, and also scoping variables.
Short answer, no. There are no performance issues in defining media queries within CSS selectors.
But let's dive in...
As described in Anselm Hannemann great article Web Performance: One or Thousands of Media Queries there is no performance loss from adding the media queries in the manner you are.
As long as the same set of media queries are being used in each selector there is no major performance hit other than your CSS file might be a bit larger.
.foo {
@media (min-width: 600px) { ... }
@media (min-width: 1000px) { ... }
@media (min-width: 1500px) { ... }
}
.bar {
@media (min-width: 600px) { ... }
@media (min-width: 1000px) { ... }
@media (min-width: 1500px) { ... }
}
However, it does matter how many different media queries you use. Different being different min-widths
, max-widths
and so on.