【问题标题】:Unable to change rules of React Bootsrap using CSS Modules无法使用 CSS 模块更改 React Bootstrap 的规则
【发布时间】:2020-07-23 20:01:17
【问题描述】:

我正在尝试更改 react-bootstrap 列表组项的背景颜色。

我的 example.js 文件

import {classes} from "./Sidebar.module.css";
import ListGroup from "react-bootstrap/ListGroup";
import React from "react"; 
const Sidebar = () => {
    return (
        <div>
            <ListGroup>
                <ListGroup.Item className = {classes.Color} >Products</ListGroup.Item>
                <ListGroup.Item>Stores</ListGroup.Item>
            </ListGroup>
        </div>
    )
}

Sidebar.module.css 文件有

.Color{
    background-color: red;

即使给定了自定义类名,背景颜色仍然是默认颜色。

【问题讨论】:

  • 如果您删除 css 导入名称周围的花括号 {},那么我相信它本身应该没问题.. 像这样 import classes from "./Sidebar.module.css"; 给你,codesandbox.io/s/css-modules-react-cq6nj .. 而不是 Sidebar.module.css ,您在styles.module.css 中发现了相同的内容,因此您可以在您的 css 中调整这些更改。这对您有帮助吗??
  • 嗨,@ManirajMurugan 谢谢。即使删除了{},它仍然不会改变背景颜色和其他样式,除非我使用!important,这很难用于每种样式。
  • 在上面提供的代码和框链接中,我没有包含任何! important ..你能分叉上面给出的代码和bx并重现你的问题吗?我会尝试解决方案..

标签: css reactjs react-bootstrap react-css-modules


【解决方案1】:

如果您想更改默认颜色,请使用 !important。当您从 Sidebar.module.css 加载样式时,还要删除花括号。像这样

import classes from './Sidebar.module.css';

.Color {
   background-color: red !important; 
}

【讨论】:

  • 谢谢 有没有办法不使用 !important,因为有很多规则都是这样操作的?
  • 如果你只是去掉花括号而不是你不需要使用 !important。
  • 您还需要确保您的 css 选择器具有最高的特异性。
猜你喜欢
  • 2018-05-23
  • 1970-01-01
  • 2017-04-10
  • 2012-04-23
  • 2011-09-01
  • 2012-11-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多