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.

What's the order of application of classes?

See original GitHub issue

I’m using CSS modules, Is there a guaranteed order of application of CSS classes? In the following example, can I expect the resultant style to always have a green background since .bar is last? I’m getting inconsistent results in my app. Is there a way to enforce order of application then?

// main.css
.foo {  background: red; }
.bar { background: green; }
// main.js
import styles from './main.css';
const style = classNames(styles.foo, styles.bar);

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8

github_iconTop GitHub Comments

8reactions
charkourcommented, Sep 27, 2022

This is happening when using classnames with postcss modules.

I have something like this.

import s from './Button.module.css';
import cn from 'classnames';

    const rootClassName = cn(
        s.root,
        {
            [s.primary]: variant === 'primary',
            [s.secondary]: variant === 'secondary',
        },
        className
    );

If className is a string, then it is applied before the css modules with posts are applied, even though I specify that className should be the last thing applied in the function.


UPDATE

After using classnames and clsx for a year now, I believe the reason that className is applied after the CSS module is due to specificity. The CSS module has higher specificity than the className string passed to the cn().

0reactions
samerset-rmncommented, Oct 28, 2022

Hi there! Have you guys found any solution yet? I’ve just stumbled upon the same issue. Thinking how to prioritize the string classname over CSS module.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How order of classes work in CSS ? - GeeksforGeeks
“The order of the classes in which they would work does not depend upon the order, in which they are written in the...
Read more >
In what order are CSS classes applied? - Quora
It apparently depends on the order that CSS is defined in your source files, not the order that the classes are listed in...
Read more >
What High School Classes Do Colleges Look for on ...
Are you wondering what high school classes do colleges look for on applications? Learn more about the classes you should take to round...
Read more >
A Guide to the Education Section of the Common App
From grades to what courses you took, colleges will want to get a detailed look at how you did in high school. Read...
Read more >
Classes (OOP) | Brilliant Math & Science Wiki
A class can define types of operations, or methods, that can be performed on a Car object. For example, the Car class might...
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