简体   繁体   English

如何在 React bootstrap Styled Component 中覆盖 -btn-primary 类

[英]How to override -btn-primary class in React bootstrap Styled Component

I am new to CSS and react bootstrap and react however I am styling a button using react bootstrap and styled component.我是 CSS 的新手,并且 react bootstrap 和 react 但是我正在使用 react bootstrap 和样式化组件来样式化按钮。 The button works fine but when I right click or click and hold the button color changes to blue.该按钮工作正常,但是当我右键单击或单击并按住按钮时,按钮颜色变为蓝色。 How can I avoid it?我怎样才能避免它? Also I can see the -btn-primary getting append to my styled button.我也可以看到 -btn-primary 附加到我的样式按钮。 I know i can use !important to avoid this issue but I strictly do not want to use "ïmportant" my code我知道我可以使用 !important 来避免这个问题,但我绝对不想使用“重要”我的代码

const AppPrimaryButtonStyle = styled(Button)`
  display: contents;
  .styled-primary-button {
    font: ${props => props.theme.font.family.primary};
    color: ${props => props.theme.color.secondaryText};
    background-color: ${props => props.theme.color.primary};
    border-color: ${props => props.theme.color.primary};
  }
`;
const AppPrimaryButton = ({ children, onClick, block, href }: AppPrimaryButtonProps) => (
  <AppPrimaryButtonStyle>
    <Button className={`styled-primary-button`} onClick={onClick} block={block} href={href}>
      {children}
    </Button>
  </AppPrimaryButtonStyle>
);

export default AppPrimaryButton;

html property html 属性

<button type="button" class="styled-primary-button btn-primary btn btn-primary">Click Me</button>

没有点击

右键单击或单击并按住

CSS work's in a way that it will implement the style's as soon as it get's a class. CSS 的工作方式是,一旦它成为一个类,它就会实现样式。 In that way the class that you are passing at the last will be displayed on the screen and has more preference because it is applied last.这样,您最后传递的class将显示在屏幕上,并且具有更多偏好,因为它是最后应用的。 You can try:你可以试试:

const AppPrimaryButton = ({ children, className, onClick, block, href }: AppPrimaryButtonProps) => (
  <AppPrimaryButtonStyle>
    <Button className={`${className} styled-primary-button`} onClick={onClick} block={block} href={href}>
      {children}
    </Button>
  </AppPrimaryButtonStyle>
);

I think will produce an HTML like this:我认为会产生这样的 HTML:

<button type="button" class="btn btn-primary styled-primary-button">Click Me</button>

Hope this works for you.希望这对你有用。

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

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