The hierarchy of CSS styling in React with CSS modules

Having a css module that adds styling to an existing component is great until the default style of the component keeps overriding it.

Imagine having this structure:

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

And applying styles from the css module like so:

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

  > div {
    align-items: center;
  }
}

You'd expect the default background property to be replaced, but instead, the css module's styles get canceled out.

Here's what actually happens:

.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 gets overridden by .ant-layout-header's background
    margin-bottom: 1rem;
}   

I even made sure to check the order of css/scss imports, but the issue persists:

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

Is there a way to successfully overwrite an existing component's style while using antd for UI components?

Answer №1

After testing, it appears that there are no issues with your code. Everything seems to be functioning properly:

https://codesandbox.io/s/morning-currying-hv96y?fontsize=14&hidenavigation=1&theme=dark

When it comes to handling CSS specificity, there are a few options you can consider:

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

  2. Utilize the id property:

    <Header id="header-id">Header</Header>
    (then reference the header using #header-id { ... })

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

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

Furthermore, I recommend avoiding importing the entire CSS library and instead utilizing the babel plugin to import only the necessary components.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Struggling to align Social Media Share Buttons in inline format for a website

I'm having trouble aligning these social media share buttons with my inline list. I tried using vertical-align: top; on the <li> element, but it didn't work in Chrome. You can view the page here: Here is the full HTML/CSS code: <!DOCT ...

Personalize MUI Gridtoolbar to meet your needs

Can anyone advise on how to personalize the gridtoolbar in the MUI Datagrid by adding custom buttons and text? Appreciate any insights. Federico https://i.sstatic.net/hq2et.png ...

What is the best way to eliminate the space between columns?

I have a simple structure on BootStrap. <div class="container"> <div class="row"> <div class="col-md-4 blue-section" ><p>Some content</p></div> <div class="col-md-4 red-section"><p>Some content&l ...

Steps to eliminate the dividers between cells in a Material UI Table

I've been experimenting with various CSS properties and elements within the Material-UI Table and TableCells, but I'm still unable to remove the horizontal lines between rows. Does anyone know how to achieve this in the Table component of Materia ...

Two divs positioned on the left side and one div positioned on the right side

Hey there, I am attempting to align 2 divs on the left side with one div on the right. #div1 #div2 #div1 #div2 #div3 #div2 #div3 #div3 The challenge is to have #div2 positioned between #div1 and #div3 when the browser window is resized smaller. Currentl ...

How can I assign a distinct background color to individual sections in Chart.js?

Wanting to Customize Grid Background Colors in Line Chart using react-chartjs-2 I am looking to have different background colors for each grid in my Line chart. Is this functionality possible with the react-chartjs-2 library? This is the desired appearan ...

How can you assign a function as a property to a Material-UI component in a React application?

I needed to create a custom Appbar with a SearchBar using Material-UI for React. In my component section, I have a file that loads the Appbar. Here's the content of appbar.js: function SearchAppBar(props) { const { classes, placeholder, writeInpu ...

Comparing the benefits of a 3-column flow layout to a traditional

I've been wondering about the popularity of 3 column flow layouts compared to using a table with 3 columns and one row. Can someone explain the advantages and disadvantages of using a flow layout versus a table layout for this scenario? Thank you ...

Adjust the starting color of a mat-input in Angular 9

I am encountering an issue with the styling of a standard matInput field within a mat-form-field, all contained within a [formGroup] div. The dark background of the parent element is causing visibility problems with the default dark text color of the input ...

Tips for including vertical lines within text boxes to distinguish elements

I've been working on a simple form using CSS and HTML, but I'm struggling to add vertical lines to separate my text from the radio buttons. Right now, I'm just using '|' to divide them, but it's not the most visually appealing ...

Register when a user signs up or buys a product from stripe's subscription service

Currently, I am in the process of developing an application that allows public users to access a pricing page and choose a plan (utilizing stripe for subscription payments). Once a user selects a plan and proceeds to checkout, my aim is to have them redi ...

The URL "http://packagist.org/p/provider-3.json" was unable to be retrieved due to a bad request error (HTTP/1.1 400 Bad Request)

Encountering a 400 bad request issue when trying to connect over HTTP, despite the package only being installable via HTTP. Attempting to override in composer.json to force HTTPS as suggested by others for a workaround, but that solution doesn't seem ...

Utilize Jest and React to reset the location.href variable before running each test case

I am currently working with a React component that takes URL parameters into consideration and then performs fetch requests based on those parameters. In the event that no parameter is set, it defaults to a specific value. The problem I have encountered i ...

Is there a way to analyze and contrast information from two different maps?

I encountered a challenge while attempting to compare data from different tables in my database. I am currently still learning React and trying to implement a functionality where I can compare the data from recommendations and customizations, and display t ...

What is the best way to position a div next to a form field?

My current HTML code is shown below. <div class="field"> <label>E-mail address: </label> <input type="text" id="email" name='email' style="width:200px;"></input> < ...

Converting JSON into key-value pairs using ReactJS

Here is a JSON object that I have: [ { "crime": "LARCENY-NON_VEHICLE", "count": "23217" }, { "crime": "AUTO_THEFT", "count": "13675" ...

Next step is to retrieve previous store information

As a newcomer to the world of Javascript and Reactjs, I encountered an issue while attempting to execute multiple actions upon clicking a button. The first action successfully updates the store data, however, the second action seems to be using the old sto ...

Tips for validating user input in AngularJS without using a form tag

Within a popup, I am displaying HTML content that has been copied from another div and shown in the popup. I need to validate this input field to ensure it is required, and display an error message below the input box. <!-- HTML code for changing zip c ...

Issue with CSS wrapper background not displaying

I am currently working with a layout that looks like this: <div class="contentWrapper"> <div class="left_side"></div> <div class="right_side"></div> </div> The contentWrapper class is styled as follows: .contentWrappe ...

Having issues with ReactJS - function not functioning properly when invoked within componentDidMount() and componentDidUpdate() lifecycle methods

I have encountered an issue that has left me puzzled, and I'm hoping someone can provide some guidance on how to solve it. The task at hand involves implementing a function that adds .active classes to li elements within a navigation bar if they cont ...