简体   繁体   English

Bootstrap cdn 在反应 js 中覆盖了我的自定义 css styles

[英]Bootstrap cdn overrides my custom css styles in react js

I have designed a web page in ReactJS using CSS.我使用 CSS 在 ReactJS 中设计了一个 web 页面。 For one of the component I have used Bootstrap for styling.对于其中一个组件,我使用 Bootstrap 进行样式设置。 I garbed the latest bootstrap cdn link and pasted it in "index.html" file.我使用了最新的引导 cdn 链接并将其粘贴到“index.html”文件中。 When I use that bootstrap link, it overrides few of my custom styles.当我使用该引导链接时,它会覆盖我的一些自定义 styles。 Each component of my project has a separate CSS and JS file.我项目的每个组件都有一个单独的 CSS 和 JS 文件。 Please suggest any solution, where should I put that bootstrap link in my project so that it doesn't override my custom css styles.请提出任何解决方案,我应该将该引导链接放在我的项目中的哪个位置,以便它不会覆盖我的自定义 css styles。 Thanks.谢谢。

It's smell like wrong css include order.它闻起来像错误的 css 包括订单。 You may check it in browser.您可以在浏览器中查看。 To resolve this, you can go through few ways:要解决此问题,您可以通过以下几种方式 go:

  1. Try to include bootstrap css as very first link in head section (after meta ).尝试将引导程序 css 作为head部分的第一个link (在meta之后)。
  2. Try to include your css via import in your index.js file.尝试通过在index.js文件中import来包含您的 css。

Second way should be look like this:第二种方式应该是这样的:

import "https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css";
import "./styles.css";

It's very simple.这很简单。 Link your custom CSS file just below the bootstrap.css and override the class in your custom.css file.将您的自定义 CSS 文件链接到 bootstrap.css 下方,并在您的 custom.ZC7A628CBA22E2A2EB61DZ 文件中覆盖 class 文件。

You can rely on descendant selectors, ID and '.important' notation together to override the styles but that is against the CSS best practices.您可以同时依赖后代选择器、ID 和“.important”符号来覆盖 styles,但这违反了 CSS 最佳实践。 You can use them only when it is absolutely necessary.只有在绝对必要时才能使用它们。

You can simply use this method.您可以简单地使用此方法。 Follow the following articles.关注以下文章。 I hope you help this.我希望你能帮助这个。

Override Bootstrap CSS styles 覆盖引导程序 CSS styles

CSS Overrides CSS 覆盖

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM