【问题标题】:Override bootstrap css only in one react component仅在一个反应​​组件中覆盖引导 css
【发布时间】:2019-10-21 16:52:57
【问题描述】:

所以我在我的 react (CRA) 项目中使用了我在 codepen 上找到的华丽的搜索栏组件。 我已经在默认的 src/index.js 中导入了 css 然后我有我的搜索组件,它由 Search.js 和 Search.module.css 组成。

显然 Bootstrap 样式和 Search 组件样式不能一起使用,当我在 src/index.js 中注释引导文件导入时,Search 组件将正常工作。

那么我怎样才能只在我的搜索组件上覆盖引导程序? 这是 Search.module.css 的 css

@import url("https://fonts.googleapis.com/css?family=Roboto:400,400i,700");

* {
    font-family: Roboto, sans-serif;
    padding: 0;
    margin: 0;
}

.flexbox {
    background: linear-gradient(155deg, #cccccc, #e8ecee, #d4d4d4);
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}

.search {
    margin: 20px;
}

.search>h3 {
    font-weight: normal;
}

.search>h1,
.search>h3 {
    color: white;
    margin-bottom: 15px;
    text-shadow: 0 1px #eaeff1;
}

.search>div {
    display: inline-block;
    position: relative;
}

.search>div:after {
    content: "";
    background: white;
    width: 4px;
    height: 20px;
    position: absolute;
    top: 40px;
    right: 2px;
    transform: rotate(135deg);
    box-shadow: 1px 0 #eaeff1;
}

.search>div>input {
    color: white;
    font-size: 16px;
    background: transparent;
    width: 25px;
    height: 25px;
    padding: 10px;
    border: solid 3px white;
    outline: none;
    border-radius: 35px;
    box-shadow: 0 1px #eaeff1;
    transition: width 0.5s;
}

.search>div>input::placeholder {
    color: #5a5a5a;
    opacity: 1;
}

.search>div>input::-ms-placeholder {
    color: #efefef;
}

.search>div>input::-ms-input-placeholder {
    color: #5a5a5a;
}

.search>div>input:focus,
.search>div>input:valid {
    width: 250px;
}

【问题讨论】:

  • “很明显 Bootstrap 样式和搜索组件样式不能一起工作” - 为什么很明显?没有显示代码。我们怎么知道什么不起作用?
  • 这里是代码。

标签: css reactjs twitter-bootstrap


【解决方案1】:

由于您尚未共享代码 sn-ps。我假设引导搜索将使用:textbutton 标签。现在,它的 CSS 将来自 bootstrap。

您可以执行以下操作:

1) 创建一个搜索组件级别的类,例如“search-module” 2) 现在,在搜索组件和该 css 中创建 css 或 scss 文件导入 通过以下方式覆盖引导 css:

.search-module input[type=search] {...}

3) 你也可以覆盖你的主 style.css 文件。

对于引导程序中所有其他冲突的类、标签和 ID,您需要执行第 2 步与搜索组件。

PS:这会使你的 CSS 膨胀。最好的办法是您可以选择 Bootstrap 中需要的部分,然后编写自己的样式。

谢谢。

【讨论】:

  • 所以我必须重命名类以避免样式错误,谢谢。
猜你喜欢
  • 2021-09-27
  • 2020-01-06
  • 1970-01-01
  • 1970-01-01
  • 2020-10-23
  • 2015-02-27
  • 2019-08-12
  • 2019-02-02
  • 2015-03-15
相关资源
最近更新 更多