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.

Media query in a base component not properly overridden

See original GitHub issue

Current behavior:

Take this BaseBox definition that provides yellow background for mobile (width below 768px) and red background for desktop (width of 768px and above):

const BaseBox = styled('div')`
  ${({ theme }) => `
    background-color: yellow;
    @media screen and (min-width: 768px) {
      background-color: red;
    }
  `}
`;

Then define BlueBox with the following background-color override, which should apply to all viewport widths:

const BlueBox = styled(BaseBox)`
  background-color: blue;
`;

We’d expect BlueBox to always have a blue background regardless of viewport width, and never yellow or red. However, we get a blue background for mobile (expected) and red background for desktop (not expected).

To reproduce:

See https://codesandbox.io/s/emotion-issue-template-91sn9

Try adjusting the viewport width below 768px and above 768px.

Expected behavior:

Component background is blue for all viewport widths.

Actual behavior:

Component background is blue below 768px viewport width; red otherwise.

Environment information:

  • react version: 16.8.6
  • emotion version: 10.0.14

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Andaristcommented, Oct 29, 2020

Right, I understand that with SCSS you can do that because you have control over the class name but in the case of Emotion you just don’t have that, styles that you compose are always assumed to be part of a single class definition so the mixins comparison is the best one because it’s how this works. We just don’t expose other composition mechanisms.

1reaction
bukowskiadamcommented, Oct 28, 2020

👋 hello,

I’ve landed here from google search. I have the same issue as @slopes321 I think that this behavior of emotion breaks “Cascading” word from CSS acronym.

I’ve prepared similar plain CSS codesandbox with that case, where it works properly: https://codesandbox.io/s/css-overrides-media-query-h4k6x

And the bit simplified case from the original report: https://codesandbox.io/s/emotion-issue-template-forked-2qe42?file=/src/index.js

const BaseBox = styled("div")`
  background-color: yellow;
  @media screen and (min-width: 768px) {
    background-color: red;
  }
  background-color: lightblue;
`;

where I think background should be lightblue, but it’s not.

From the generated code I see that above example is generated as

.css-1qgp6uu-BaseBox{background-color:yellow;background-color:lightblue;}

@media screen and (min-width:768px){.css-1qgp6uu-BaseBox{background-color:red;}}

so the order of rules is in fact changed. To keep cascading work properly we should divide it into three parts:

.css-1qgp6uu-BaseBox{background-color:yellow}

@media screen and (min-width:768px){.css-1qgp6uu-BaseBox{background-color:red;}}

.css-1qgp6uu-BaseBox{background-color:lightblue;}

Could you please provide some more details why it can’t work like in the CSS? Are there some code limitations? Thanks in advance!

Read more comments on GitHub >

github_iconTop Results From Across the Web

css - Media Query Styles Not Overriding Original Styles
This was the problem for me. I had my core styles, then page specific mobile styles, then page normal styles, and this was...
Read more >
media - CSS: Cascading Style Sheets - MDN Web Docs
Applies a style only if an entire query matches. It is useful for preventing older browsers from applying selected styles. When not using...
Read more >
Media Queries Level 4 - W3C
Abstract. Media Queries allow authors to test and query values or features of the user agent or display device, independent of the document ......
Read more >
Logic in CSS Media Queries (If / Else / And / Or / Not)
To ensure that only one media query is in effect at a time, make the numbers (or whatever) such that that is possible....
Read more >
How To Fix Media Query Not Working In WordPress
Sometimes, your media query does not work because it is wrongly positioned. Ideally, media queries should be placed at the end of the...
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