Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

# Id selector in react css modules

I'm using React Css modules for styling my react app.

I have some html elements with id attribute and I want to style them using the css id selector and not a class selector. Is that possible with react css modules? For example.

This is an example of css, I'm going to call it Button.modules.css

#specialButon {
 margin: 5px;
}

.notSpecialButton {
 margin: 5px;
}

And this is an example of react component

import style from './Button.module.css';

function Button() {

 render(
  <div>
   <button id="specialButon">Special<button>
   <button className={style.notSpecialButton}>Not Special<button>
  </div>
)

export default Button

Could you tell me, How can I use de id selector to apply style elements?

Thanks you.

like image 658
ernesto petit Avatar asked Feb 06 '26 18:02

ernesto petit


2 Answers

It's a bit late, but the accepted answer is actually not a right one. You can definitely use id selectors with CSS Module!

In your case, just changing it to the below will do :)

<button id={style.specialButon}>Special<button>

or

<button id={style['specialButon']>Special<button>
like image 79
cateyes Avatar answered Feb 09 '26 08:02

cateyes


For that, you can use global CSS in React which is generally in App.js. But is general to use className in module CSS.

like image 21
prince yadav Avatar answered Feb 09 '26 07:02

prince yadav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!