ReactJS: Including a CSS file in one component affects the styling of the entire application

I have encountered a dilemma with the "antd" dependency. I imported its css file (antd/dist/antd.css) into one of my files, but now it's affecting the styling of the header as well. How can I ensure that only Content.js uses this particular styling?

To illustrate my issue, I have replicated it in the following codesandbox:

https://codesandbox.io/s/elastic-dream-5e6dp

Furthermore, here is the relevant code snippet as well.

Index

import React from "react";
import ReactDOM from "react-dom";

import App from "./App";

const rootElement = document.getElementById("root");
ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  rootElement
);

App

import React from "react";
import Header from "./Header";
import Content from "./Content";

export default function App() {
  return (
    <div className="App">
      <Header />
      <Content />
    </div>
  );
}

Header

import React from "react";

export default function Header() {
  return <div>This is the header, this should not be in antd styling</div>;
}

Content

import React from "react";
import "antd/dist/antd.css";
import { Table, Button, Popconfirm, Row, Col, Icon, Upload } from "antd";

export default function ExcelPage() {
  return (
    <div>
      This should be in antd styling.
      <Button>
        <Icon type="upload" /> Click to Upload
      </Button>
    </div>
  );
}

Answer №1

If you are utilizing a newly created react app, you may have the benefit of using css-modules. This functionality enables you to tie a CSS file specifically to the JSX where it is imported. Learn more about this feature in the documentation for CSS-modules and react (https://create-react-app.dev/docs/adding-a-css-modules-stylesheet/). To implement this, simply name your file as "XXX.module.css". That's how specific it gets.

You can also achieve this with SCSS files. These allow you to import other CSS/SCSS files at the beginning. By combining this with css/scss modules, you can effectively manage your styles.

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

Expanding the width of a list-group with Bootstrap to fill the entire div

I am working with a div that has a height of 200px. How can I make sure the ul items are divided into equal rows within the div, instead of always aligning to the top? To illustrate this, I have colored the div green. As I will be using ngFor in my ul, the ...

Image not found in next.js

Working Environment ・ next.js ・ typescript ・ styled-components I uploaded the image in the folder inside pages, but it is not showing up. Why is that? // package.json   { "name": "nextapp", "version": &qu ...

Encountering the 'dispatcher is null' error when implementing react-dom-router and BrowserRouter, Routers, Route for a navigation bar component in a React application

I am currently working on building a header with multiple nested components, one of which is the NavBar component. I am now focusing on implementing functionality to make the NavBar items functional using BrowserRouter, Routes, and Route. Below is the code ...

Learn the process of invoking componentDidMount each time you switch between ListItems in React

After initializing the widget component to load various charts, it works correctly. However, when switching to another ListItem, the componentDidMount does not load the required data. This data is essential for the component to function properly. I noticed ...

Tips on preventing gaps between the initial div elements in each row

I have DIVs that are positioned next to each other, but only the fourth DIV is in the first row, while the others are shifted to the next row. I want each first div in a row to not take up space from the left side, but this is not happening. Here is my co ...

Having trouble getting jQuery slideToggle to function properly in conjunction with Bootstrap v4

Struggling with getting jquery slideToogle to work properly? I've found that it does the job when using the standard toggle (hide/show) method. Also, no issues arise when utilizing Bootstrap's v4 'collapse' feature. However, if you&apos ...

You cannot remove the bottom border of Nav tabs as demonstrated on the official website

When looking at the official Angular page for navigation tabs, there is no bottom border displayed on the selected tab. official page However, when I implement the same code, a border is showing even on the selected nav item. myTab example Below is my ...

The issue with the React component arises from a failure in hydration due to the initial UI not aligning with what was rendered on the server

I'm encountering an issue with my custom skeleton component that takes in parameters. Whenever I use it, I keep getting a hydration error: "Uncaught Error: Hydration failed because the initial UI does not match what was rendered on the server." skele ...

Error occurs in private NPM library stating "Failure in parsing module"

I'm currently in the process of setting up my React library and utilizing it locally (via my company's git repository). During development, I can test my application with npm run start, and everything functions as expected. However, when attemp ...

When hovering over the background, it will enlarge but the text in front will remain the same size

As the user hovers over the image, the image enlarges. However, there is also some information that needs to be displayed in front of the image. This is my current implementation: .home-about-link { overflow: hidden; width: 100%; } .home-about { ...

MUI Group Selector - Array of Arrays

Struggling to create a customized MUI select group using API data. const data = [ { id: '1', name: 'Category 1', services: [ { id: '1', name: 'Service 1', }, ...

Vue.js - dynamically applying CSS classes based on conditions

Within a VueNative project that utilizes NativeBase as a component library, with tags prefixed by nb-, the code snippet below is found within the <template> tags of the footer.vue file which houses navigation buttons. This piece of logic successfully ...

Issue with Checkbox Element's Margin CSS Not Being Applied

Hello everyone, I'm feeling a bit puzzled as to why the margin in my CSS isn't applying correctly to the checkbox element. Below is an example of the code I'm using: HTML: <div class="panel"> <input id="bodytype-checkbox" ...

Is it possible to create a single button that, upon clicking, fades in one image while simultaneously fading out another?

My goal is to have the blue square fade in on the first button click, then fade out while the red square fades in on the second click. Unfortunately, it seems that my current code is not achieving this effect. I'm open to any suggestions or help on h ...

Using Apache as a reverse proxy to Nginx in order to connect with a ReactJS application running on a

Currently, I am running a server powered by Apache that doubles as a reverse proxy. This setup includes a Nginx docker container on the same server hosting a ReactJS website with a router-enabled ReactJS app. The ultimate objective is to create a reverse p ...

What could be causing certain grid items to display in a disorderly manner?

Currently, I am working on a CSS grid layout that resembles the one showcased in this demo. The challenge I'm facing is related to the ordering of items within the grid. Specifically, every 6th and 7th item seem to be out of order and I would like the ...

Having trouble with Tailwind CSS not functioning correctly once the font is imported?

I am currently working on a next.js project and utilizing tailwind for styling. I have noticed an odd behavior when importing a custom font into my globals.css file. page.jsx "use client"; import React from "react"; const page = () = ...

Encounter an incorrect route in react-router

My current setup involves a router similar to the following: const Routes = (props) => { const { store } = props; const { loggedIn } = props; const checkAuth = (nextState, replace) => { auth.checkAuth(nextState, replace, loggedIn); }; ...

What steps can be taken to expand the axis space in recharts to accommodate an additional label?

I'm struggling to display an additional label below my X-Axis label as it keeps getting clipped off. https://i.sstatic.net/ftI7w.png Below is the code snippet for my XAxis: <XAxis type="number" tick={<CustomizedNumberTick lang={props ...

Does a React component clear its state upon re-rendering?

Within my React functional component, I define the state in the following way: const [myState, setMyState] = useState<boolean>(true); If one of my props changes and triggers a re-render of this function, would myState be reset to true? ...