如何覆盖react引导颜色?

IT技术 css reactjs react-bootstrap
2021-05-22 04:30:46

我正在寻找如何更改它,但似乎没有一个解决方案对我有用。

我想覆盖 react-bootstrap 按钮的颜色。

下面的这个解决方案工作得很好,正是我想要做的:

<Button
   block
   style={{backgroundColor: '#0B0C10', borderColor: '#45A293', color: '#45A293', borderRadius: '100px'}}
>
   sample text
</Button>

但我不想每次使用按钮时都重写它,所以我想用 css 解决,我试过使用这个:

.custom-button {
    background-color: #1F2833;
    border-color: #45A293;
    border: 3px solid-transparent;
    color: #45A293;
    border-radius: 100px;
}

然后像 className="custom-button" 一样在 className 中传递它,但它并没有真正起作用。

我正在使用 react-bootstrap 中的 Button

import {Button} from "react-bootstrap";

来自引导程序的样式

import 'bootstrap/dist/css/bootstrap.min.css';

使用版本如下:

"react-bootstrap": "^1.0.0-beta.5",
"bootstrap": "^4.3.1",
4个回答

添加 bg 或 btn

.bg-custom-button {
    background-color: #1F2833;
    border-color: #45A293;
    border: 3px solid-transparent;
    color: #45A293;
    border-radius: 100px;
}

让我的工作像那样

然后在 bg="custom-button"

使用style=""HTML 元素属性应用的样式比通过类应用的样式具体,这就是您的第一个解决方案奏效的原因。!important在样式末尾追加是覆盖比.custom-button更具体的其他样式的一种方式

我想到的一个快速解决方案是将样式存储在一个对象中并从文件中导入它们,这将确保您不会重复自己。

样式.js

const styles = {
    customButton: {
        backgroundColor: '#0B0C10',
        borderColor: '#45A293',
        color: '#45A293',
        borderRadius: '100px'
    }
};

export default styles;

组件.jsx

import { styles }  from './styles.js'

<Button
   block
   style={styles.customButton}
>
   sample text
</Button>

否则,您将不得不尝试附加 ID 或构建更具体的 css 选择器。

我不确定这种效果是否有意,但我发现覆盖 React Bootstrap css 的最简单方法是使用 Material ui withStyles。这是一个例子。

import React from 'react';

import { withStyles } from '@material-ui/styles';
import Container from 'react-bootstrap/Container';
import Row from 'react-bootstrap/Row';
import Col from 'react-bootstrap/Col';
import ButtonGroup from 'react-bootstrap/ButtonGroup';
import Button from 'react-bootstrap/Button';

const styles = {
    logoContainer: {
        position: 'fixed',
    },
    rowStyles: {
        marginBottom: '10px',
    },
    button: {
        border: '3px inset #ffc107',
        borderRadius: '50%',
        width: '55px',
        height: '55px',
        fontFamily: 'fangsong',
        fontSize: '1em', 
        fontWeight: '700',
        color: 'white',
        backgroundColor: 'rgb(0,0,0, 0.5)',
    }
}

const Logo = (props) => {     
    const logoStyles = props.classes;
    return (
    <div>
    <Container container='true' className={logoStyles.logoContainer}>
        <ButtonGroup >            
            <Col>
                <Row className={logoStyles.rowStyles}>
                    <Button onClick={{}} className={logoStyles.button}>BS</Button>
                </Row>
            </Col>            
        </ButtonGroup> 
    </Container>
    </div>
);                           
                    }

export default withStyles(styles)(Logo);

希望这可以帮助...

您可以使用允许覆盖底层组件 CSS 基类名称的“bsPrefix”props。bsPrefix="自定义按钮"