Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CSS specificity #1456

Closed
mimarz opened this issue Feb 1, 2024 · 3 comments · Fixed by #1805
Closed

Fix CSS specificity #1456

mimarz opened this issue Feb 1, 2024 · 3 comments · Fixed by #1805
Assignees
Labels
react @digdir/designsystemet-react

Comments

@mimarz
Copy link
Collaborator

mimarz commented Feb 1, 2024

As we discovered during fix for #1448 we found out that some of our css has to high specificity due to combination of selectors. We should aim our css to have no higher specificity than 0,1,0 because thats what passed className normally have.

This is due to CSS in the component using double classname selectors such as .primary.first which has a specificity of 0,2,0 . This overrides passed classes on className which have specificity 0,1,0.

@Barsnes
Copy link
Member

Barsnes commented Mar 20, 2024

I found that using @layer in our css might fix all of our problems.
From the MDN docs, it shows us that we can create this style:

p {
  color: rebeccapurple;
}

@layer type {
  .box p {
    font-weight: bold;
    font-size: 1.3em;
    color: green;
  }
}

and the text will be purple, and not green - since layers come first.

Looking at the spec, it also seems like we can fix this with a one-liner, by updating our import statements:
https://drafts.csswg.org/css-cascade-5/#layer-declaration

  • using an @import rule with the layer keyword or layer() function, assigning the contents of the imported file into that layer.

We will probably still have to do this for every component, since people can use single files.

This means our index.css will look like this:

@import url('./react-css-modules.css') layer;
@import url('./alert.css') layer;

@Barsnes
Copy link
Member

Barsnes commented Mar 22, 2024

After testing css layers in #1721, we have decided to implement it - but after #1696, to avoid merge conflicts

@Barsnes
Copy link
Member

Barsnes commented Apr 2, 2024

After reading though an article from css tricks, I suggest we use nested layers:

@layer fds.button { ... }

This makes sure everything is under a layer named fds, but still keeps components on separate layers, nested inside of fds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
react @digdir/designsystemet-react
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants