How to override style of Library components while using CSS Modules? [duplicate]
Asked Answered
A

3

10

I'm using NextJS, which use CSS Module, I want to use some CSS Library likes Material UI, ... But how to override style of components that I import from those Library?..

Argal answered 23/11, 2020 at 11:13 Comment(0)
S
5

Building on @DucHT's answer, for people like me who really prefer it spelled out totally:

// MyComponent.tsx:

import LibraryComponent from 'some-library'
import styles from './MyComponent.module.scss'

export default function MyComponent({}) {
  return (
    <div>
      <LibraryComponent className={styles.classNameICanChoose} />
    </div>
  )
}

// MyComponent.module.scss:

.classNameICanChoose :global(.lib-component-classname){
  // Style here apply to component that's imported from libs
  // will overwrite .lib-component-classname
}
Spindlelegs answered 2/9, 2022 at 14:59 Comment(0)
A
4

After read documents from https://github.com/css-modules/css-modules .Seriously, it's quite simple, just use :global scope. Ex:

.className :global(.lib-component-classname){
  // Style here apply to component that's imported from libs
}
Argal answered 24/11, 2020 at 11:28 Comment(0)
M
4

You can always use the !important suffix after your style rule, but thats not really recommended because it goes against the Cascading of your Style Sheets (CSS).

What worked for me is using the :global selector, like DucHT explained, like this:

:global(li.ant-menu-item).siderMenuItem {
  display: flex;
  align-items: center;
  padding-top: 26px;
  padding-bottom: 26px;
}
Metzger answered 6/5, 2021 at 16:31 Comment(1)
Any other new ways to override the css? i tried your solution its not working my case @MetzgerBacchanal

© 2022 - 2024 — McMap. All rights reserved.