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.

V4 media queries prioritized higher than extended styles

See original GitHub issue

In v4, MoreStyles in this code will have a background-color of red or green, instead of the expected blue

const Styles = styled.div`
  @media (min-width: 1000px) {
    background-color: red;
  }
  @media (max-width: 1000px) {
    background-color: green;
  }
`;

const MoreStyles = styled(Styles)`
  background-color: blue;
`;

V4 version with unexpected behavior: https://codesandbox.io/s/vq6k94rpz0 V3 version with expected behavior: https://codesandbox.io/s/0z48zxvn (exactly the same code)

From what I can tell this is due to v4 adding styles to an existing class (of lower priority than the media query) rather than to a new class like v3 does:

V3: V3

V4: V4

Only work around I’ve found is something clunky like this

const MoreStyles = styled(Styles)`
  @media screen {
    background-color: blue;
  }
`;

If this is the intended behavior what is the correct way to achieve this functionality? Also if it is intended it should be probably be mentioned in https://www.styled-components.com/docs/faqs#what-do-i-need-to-do-to-migrate-to-v4

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
probablyupcommented, Oct 13, 2018

The way v4 currently works is styles are folded and flattened together in the case of nested styled(StyledComponent), so the background blue is essentially getting grouped with the other static rules (height and width) and the media queries are appended after since they aren’t part of the same style context.

Your v3 example works because before the components were not folded and each had its own generated style class. The blue background class comes after the first class due to runtime insertion order and then due to how CSS places higher precedence on equal-weight styles lower in the sheet, the blue is applied.

In terms of fixes, there are three I can think of:

  1. Use a precedence boost:
&& {
  background-color: blue;
}

This seems like the better solution to me because it makes it more obvious that you intend for this particular style to be applied over other siblings.

  1. Wrap it in a media query like you did; this forces the style to be broken off the main clump since media queries can not be nested inside a class definition and thus are inserted after the main set, becoming implicitly higher precedence

  2. Use something like !important

1reaction
probablyupcommented, Oct 16, 2018

@codeaid definitely a bug, for some reason the second example is generating .cIZrDu + .sc-htpNat:hover which is wrong, should be .cIZrDu + .sc-htpNat. I’ll try and look into this today.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using media queries - CSS: Cascading Style Sheets | MDN
The Media Queries Level 4 specification includes some syntax improvements to make media queries using features that have a "range" type, for ...
Read more >
Why media queries has less priority than no media queries css
Selector specificity is the most important part of this: styles with a more specific selector will override those with a less-specific selector... but...
Read more >
Media Queries Level 4 - W3C
Media queries are (almost) always independent of the contents of the document, its styling, or any other internal aspect; they're only dependent ...
Read more >
Media Queries
Media Queries. Media queries allow you to apply the CSS differently according to the media type (type of device) and media features (viewport...
Read more >
A Complete Guide to CSS Media Queries
CSS Media queries are a way to target browser by certain characteristics, features, and user preferences, then apply styles based on those ...
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