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.

Append custom class names to styled component

See original GitHub issue

Is it possible to have classes added via the className prop of a styled component be appended to the end of the class list instead of the start.

Take this button component as an example, which has a custom class added.

const Button = styled('button')(...);

<Button className="my-class">Click Me</Button>

Currently results in:

<button class="my-class css-6r523">Click Me</button>

I would like the my-class to be AFTER the css-6r523 class, like so:

<button class="css-6r523 my-class">Click Me</button>

Is there any way to achieve this, either through React or Babel or something else?

Thanks.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
Andaristcommented, Feb 25, 2021

Should be possible with CacheProvider and prepend option: https://emotion.sh/docs/@emotion/cache#prepend . Works pretty much similar to @emtion/css/create-instance (which you have mentioned by referring to create-emotion)

1reaction
Andaristcommented, Feb 24, 2021

This is a common misconception but the order in which you put your class names onto an element doesn’t matter. What matters is the order in which those classes are put into a stylesheet. This means that given this CSS and the following HTML you will always have a red background for both elements:

.blue { background-color: blue; }
.red { background-color: red; }
<div class="blue red">I'm red</div>
<div class="red blue">and I'm red too</div>

Check out the live demo: https://codesandbox.io/s/dank-https-gy6v0?file=/src/index.js

This is mentioned, among other things, in one of our documentation pages: https://emotion.sh/docs/composition

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add ClassNames to Styled Components - Medium
Same code but with dynamic classNames added. In the code above, we would like to target the element separately by using dynamic classNames....
Read more >
Add Classes to Styled Component - Stack Overflow
I am trying to add class names to a React Component to make it easier for me to customize that component using Styled...
Read more >
Advanced Usage - styled-components
styled -components generates an actual stylesheet with classes, and attaches those classes to the DOM nodes of styled components via the className prop....
Read more >
Applying correctly className with Styled components
Applying classes. The problem is that styled(Comp) is effective only if Comp is already a styled component. To achieve this, you must add...
Read more >
How to Control Class Names in Styled Components
Internally, styled-components generates two classes for each component— one is static and one is dynamic. The class name is generated by ...
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