question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`@extends` inside `@media` query doesn't extend rule outside query

See original GitHub issue

Which makes sense, because how exactly in the CSS would this work anyway?

But still, perhaps in edge cases like this, there could be a ‘fake’ extends, where instead of cleverly applying another selector onto the rule that’s being extended (current behaviour), it could dump the contents of the one into the other. Ugly, and it bloats the CSS, but as we would have to do the bloating ourselves, maybe it’s forgivable.

Here’s a (real life) use case:

.alpha, .beta, .gamma {
    font-weight: bold;
}
.beta {
    color: #F00;
}

@media only screen and (max-width: 60em) {
    .caption {
        @extend .beta;
    }
}

Presently, of course, CSS doesn’t allow selectors like this:

.alpha, @media only screen and (max-width: 60em) .caption {
    font-weight: bold;
    color: #f00;
}

So the .caption body is empty. With the above suggestion, Stylus might produce something like this instead:

.alpha, .beta, .gamma {
    font-weight: bold;
}
.beta {
    color: #F00;
}

@media only screen and (max-width: 60em) {
    .caption {
        font-weight: bold;
        color: #f00;
    }
}

Yes, rules are duplicated so the output is bloated, but it gives us a convenient way to extend classes into media queries. And if the HTTP server is gzipping CSS files like it should, the issue of bloat should be minimized.

I do realise that I can get something similar with functions, which do create duplicate code in every definition they’re run inside – and therefore they’re permitted inside @media queries. But having @extend intelligently recognise whether it’s inside a @media query seems tidier to me, because in the above example I’d have to create the functions alphabetagamma and beta. Could get quite confusing!

As a corollary, perhaps this modification of @extends could make it work inside of function bodies as well.

No idea what the codebase is like, and whether this would involve a pile of refactoring, so take this suggestion as you will 😃

Issue Analytics

  • State:open
  • Created 11 years ago
  • Reactions:3
  • Comments:11

github_iconTop GitHub Comments

2reactions
pdaoustcommented, Aug 24, 2012

I’m starting to change my mind about this issue; for prior art, SASS has explicitly chosen not to support this due to dangers of stylesheet bloat (every @extended rule being duplicated in every media query that needs it). Take a look: http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#_in_directives

So I no longer think it’s necessary or advisable, but if a clever solution to this problem could be worked out – or perhaps if it were implemented, but the documentation included a caveat about bloat – then that might be okay.

1reaction
shirohanacommented, Feb 16, 2021

face to it again in 2021, here’s my solution:

Failed:

$text-normal
  font-size: 16px

@media (min-width: 768px)
  .text
    @extends $text-normal

>>>
.text {
  font-size: 16px;
}

Succeed:

$text-normal = @block {
  font-size: 16px
}

@media (min-width: 768px)
  .text
    {$text-normal}

>>>
@media (min-width: 768px) {
  .text {
    font-size: 16px;
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Extending selectors from within media queries with Sass
The simple answer is: you can't because Sass can't (or won't) compose the selector for it. You can't be inside of a media...
Read more >
Using media queries - CSS: Cascading Style Sheets | MDN
Media queries allow you to apply CSS styles depending on a device's general type (such as print vs. screen) or other characteristics such...
Read more >
Cross-Media Query @extend Directives in Sass - SitePoint
Sass doesn't allow you to @extend a placeholder inside a media query from another scope. Here is a complex, but easy to use...
Read more >
Media Queries Level 4 - W3C
A media query is a method of testing certain aspects of the user agent or device that the document is being displayed in....
Read more >
CSS Media Queries - W3Schools
The @media rule, introduced in CSS2, made it possible to define different style ... Media queries in CSS3 extended the CSS2 media types...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found