V4 media queries prioritized higher than extended styles
See original GitHub issueIn 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:
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:
- Created 5 years ago
- Reactions:2
- Comments:13 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
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.
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
Use something like
!important
@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.