1

I have a css module which apply styling to an existing component. The css module works fine but the styling was always overwritten by the default style of component.

Supposed I have this markup.

 <Header className={styles.header}>
      <Row type="flex" justify="space-between">
        <Col>
          <Link to="/">
            <Title className={styles.brand}>Shiritori</Title>
          </Link>
        </Col>
      </Row>
 </Header>

Then uses the styles declare in the css module:

.header {
  background: rgb(75, 191, 107);
  margin-bottom: 1rem;

  > div {
    align-items: center;
  }
}

I expect that the background css property of the default style will be overwritten but instead the styles in my css module was canceled out.

Actual result:

.ant-layout-header {
    height: 64px;
    padding: 0 50px;
    line-height: 64px;
    background: #001529;
}
.ant-layout-header, .ant-layout-footer {
    -ms-flex: 0 0 auto;
    flex: 0 0 auto;
}
.ant-layout, .ant-layout * {
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}
.App_header__37gyT {
    background: #4bbf6b; <- this property was overwritten by the .ant-layout-header background property
    margin-bottom: 1rem;
}   

I also consider the order of css/scss import but same result.

import "../../../node_modules/antd/dist/antd.css";
import styles from "./App.module.scss";

Is there a way to overwrite existing style of a component. I use antd for the UI toolkit and components.

1 Answer 1

2

Unable to replicate your issue. Seems to be working fine:

Edit Ant-Design CSS Specifity

That said, when dealing with CSS specificity, you can do one of several things:

  1. Overwrite a child class style from a parent class style.

  2. Use the id property: <Header id="header-id">Header</Header> (then use the # to refer to header: #header-id { ... })

  3. Overwrite the .ant-layout class name within a separate CSS/SCSS file.

  4. Add an !important declaration after the style property, for example: background: pink !important;

That said, I'd avoid importing the entire CSS library and instead use the babel plugin to import only what's needed.

Sign up to request clarification or add additional context in comments.

3 Comments

It's weird, the sandbox is the same with mine but produces different results. I'll override the default css. Offtopic, how did you extract each class from the module. Got an error when extract classes in typescript.
If you can't destruct class names, the it sounds like you might not be using css modules, but global modules instead. Are you using a custom webpack configuration? If so, does it handle local modules? If you're using the create-react-app, then local modules must be .module.(s)css, otherwise they're interpreted as global (S)CSS. If you're not using the CRA, then you have to manually configure webpack to handle local and global modules.
I'm using CRA and destructure works but lints an error due to typescript. anyways thanks for the response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.