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 styles are applied in the wrong order

See original GitHub issue

Current behavior:

When using the ‘@media…’ selector, the styles are always applied after non-media styles, regardless of their order in the styles array.

To reproduce:

Here’s a simple example:

<div css={[
  { '@media screen': { color: 'blue' } },
  { color: 'red' }
]}>Text</div>

I would expect the text to be red, but it comes out blue!

Here’s a live example: https://codesandbox.io/s/emotion-issue-template-l9u7q?file=/src/index.js

Expected behavior:

I’d expect media selectors to be applied based on their order in the array instead of always being applied to the end of the list.

Here’s the equivalent example written with plain css to show what it should work like: https://jsfiddle.net/boe1k3zt/1/

Environment information:

  • react version: 16.13.1
  • emotion version: 10.0.28

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
garronejcommented, Apr 17, 2022

Hi @Andarist,
Just to reiterate on the discussion we have here This strategy is problematic for component libraries that uses emotion (bigger of them being MUI),
because of this behaviour there is no way for users to predictably overwrite the component style.

Take for example this component:

LibButton.tsx

import { css, cx } from "@emotion/css";

type Props = {
	className?: string;
	text: string;
	onClick: () => void;
};

export function LibButton(props: Props) {

    const { className, text, onClick } = props;

    return (
        <button
            className={cx(
                css({
                    "@media screen and (min-width: 800px)": {
                        "width": 200
                    },
                    "width": 100
                }),
                className
            )}
            onClick={onClick}
        >{text}</button>
    );

}

The end user has no easy way to overwrite the button width using @emotion. If he writes:

import { LibButton } from "lib/Button";
import { css, cx } from "@emotion/css";

render(
        <LibButton 
            className={css({ "width": 600 })} 
            onClick={() => { }}
        >
            Hello world
        </LibButton>
)

The button will only take a width of 600px on small screens.

Anyway, thanks for all your work.

0reactions
Andaristcommented, Aug 21, 2020

Closing this because it’s not something that we plan on changing - the current strategy has been proven rather successful given this is the only created issue about this behavior.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Media query problems wrong order? - Stack Overflow
I have like 16 media queries or something and i noticed that if i put every media query portrait 1 different color some...
Read more >
CSS3 Media Queries: Simple Gotchas & Easy Fixes
When a media query is true, the corresponding style rules are applied, following the normal cascading rules. More than one media query can...
Read more >
media - CSS: Cascading Style Sheets - MDN Web Docs
The @media CSS at-rule can be used to apply part of a style sheet based on the result of one or more media...
Read more >
Wrong order of CSS styles | WordPress.org
This will only fix that particular problem. Due to the different order of CSS file inclusion, other styles might not be applied as...
Read more >
Media Queries - Amazon Kindle Direct Publishing
Since CSS is applied in the order that it appears, code that targets multiple devices (such as the device-aspect-ratio code that targets all...
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