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.

Allow @extend across media queries

See original GitHub issue

Edit: The current plan here is to allow @extend across media queries by duplicating the queries the current @extend is in and unifying them with any media queries the extendee is in. For example:

.a {w: x}
@media (min-width: 500px) {
  .b {@extend .a}
  .c {y: z}
}

would produce

.a {w: x}
@media (min-width: 500px) {
  .b {w: x}
}

@media (min-width: 500px) {
  .c {y: z}
}

and

@media screen {
  .a {w: x}
}

@media (min-width: 500px) {
  .b {@extend .a}
  .c {y: z}
}

would produce

@media screen {
  .a {w: x}
}
@media screen and (min-width: 500px) {
  .b {w: x}
}

@media (min-width: 500px) {
  .c {y: z}
}

Original issue follows:


As originally brought up in #456, one way to allow extending across media queries would be to have a flag for @extend to explicitly tell Sass that you’re OK with creating a duplicate context in a similar fashion to how the !optional flag currently works.

The syntax as currently proposed would look/work something like the following:

%full {
  width: 100%;
  float: left;
  clear: both;
}

%half {
  width: 50%;
  float: left;
}

.sidebar-1 {
  @extend %full;
  background: blue;
  @media (min-width: 500px) {
    @extend %half !duplicate; // A new extend context will be created here for the all, min-width: 500px media context and extension will work as normal.
    background: red;
  }
}

.sidebar-2 {
  @extend %full;
  background: green;
  @media (min-width: 500px) {
    @extend %half !duplicate; // Because a context for this exact media query already exists, it will extend that one instead of creating a duplicate context
    background: orange;
  }
}

.content {
  @extend %half;
  background: purple;
}

would compile to

.sidebar-1, .sidebar-2 {
  width: 100%;
  float: left;
  clear: both;
}

.content {
  width: 50%;
  float: left;
}

.sidebar-1 {
  background: blue;
}

@media (min-width: 500px) {
  .sidebar-1, .sidebar-2 {
    width: 50%;
    float: left;
  }

  .sidebar-1 {
    background: red;
  }
}

.sidebar-2 {
  background: green;
}

@media (min-width: 500px) {
  .sidebar-2 {
    background: orange;
  }
}

.content {
  background: purple;
}

Of course the optimization around this would be difficult, needing to ensure that the matching only happens for identical @media contexts (but include ones in or chains) and a decision would need to be made as to if the selectors should be moved to the first or last item as the normal @extend pattern of “all” doesn’t quite make sense here, but because it is an explicit call, a user will understand that they’re changing how it works.

Issue Analytics

  • State:open
  • Created 10 years ago
  • Reactions:52
  • Comments:79 (12 by maintainers)

github_iconTop GitHub Comments

34reactions
sospedracommented, Sep 21, 2016

3 years later still not done…

23reactions
brandensilvacommented, Oct 4, 2016

Many of us are still waiting on this feature to land. It seems like with all the discussion previously around this it’d have some more weight in its priority.

The fact that you can’t @extend from within a media query feels awkward. Can you generate mixins and workarounds? Sure, but you just expect it to work no matter where you @extend from as long as whatever you are extending is available to the file you are using it in.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Extending selectors from within media queries with Sass
Extending selectors from within media queries with Sass · Use a mixin · Extend the selector within a media query from the outside...
Read more >
SCSS - Extending Classes Within Media Queries
A limitation of this method is that you have to explicitly create a mixin for every class you want to extend within a...
Read more >
Extending In Sass Without Creating A Mess
The @extend directive in Sass is a powerful directive that facilitates the sharing of rules and relationships between selectors.
Read more >
Cross-Media Query @extend Directives in Sass - SitePoint
You may not @extend an outer selector from within @media. You may only @extend selectors within the same directive. That sucks, doesn't it?...
Read more >
Sharing CSS code between a selector and a media query
There are discussions going on to allow @extend across media queries on GitHub. Posted a response ? — Webmention it. This site uses...
Read more >

github_iconTop Related Medium Post

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