如何覆盖Bootstrap CSS样式?

Ale*_*lex 215 html css twitter-bootstrap

我需要修改bootstrap.css以适应我的网站.我觉得创建一个单独的custom.css文件而不是bootstrap.css直接修改是更好的,一个原因是应该bootstrap.css获得更新,我将试图重新包括我的所有修改.我会为这些款式牺牲一些加载时间,但对于我压倒的几种款式来说,它可以忽略不计.

我是否重写,bootstrap.css以便删除锚/类的样式?例如,如果我想删除所有样式规则legend:

legend {
  display: block;
  width: 100%;
  padding: 0;
  margin-bottom: 20px;
  font-size: 21px;
  line-height: inherit;
  color: #333333;
  border: 0;
  border-bottom: 1px solid #e5e5e5;
}
Run Code Online (Sandbox Code Playgroud)

我可以删除所有内容bootstrap.css,但如果我对覆盖CSS的最佳实践的理解是正确的,我该怎么做呢?

要清楚,我想删除所有这些样式的图例并使用父的CSS值.因此结合Pranav的答案,我会做以下几点吗?

legend {
  display: inherit !important;
  width: inherit !important;
  padding: inherit !important;
  margin-bottom: inherit !important;
  font-size: inherit !important;
  line-height: inherit !important;
  color: inherit !important;
  border: inherit !important;
  border-bottom: inherit !important;
}
Run Code Online (Sandbox Code Playgroud)

(我希望有办法做以下事情:)

legend {
  clear: all;
}
Run Code Online (Sandbox Code Playgroud)

smu*_*ynn 454

使用!important不是一个好选择,因为您很可能希望将来覆盖自己的样式.这给我们带来了CSS优先级.

基本上,每个选择器都有自己的数字"权重":

  • ID为100分
  • 类和伪类的10分
  • 标记选择器和伪元素为1分

在两种选择器样式中,浏览器总是会选择权重更大的选择器.样式表的顺序仅在优先级均匀时才有意义 - 这就是为什么覆盖Bootstrap并不容易.

您可以选择检查Bootstrap源,了解如何定义某些特定样式,并复制该选择器以使您的元素具有相同的优先级.但是我们在这个过程中放松了所有的Bootstrap甜味.

解决此问题的最简单方法是为页面上的一个根元素分配其他任意ID,如下所示: <body id="bootstrap-overrides">

这样,您可以使用您的ID为任何CSS选择器添加前缀,立即向该元素添加100个权重点,并覆盖Bootstrap定义:

/* Example selector defined in Bootstrap */
.jumbotron h1 { /* 10+1=11 priority scores */
  line-height: 1;
  color: inherit;
}

/* Your initial take at styling */
h1 { /* 1 priority score, not enough to override Bootstrap jumbotron definition */
  line-height: 1;
  color: inherit;
}

/* New way of prioritization */
#bootstrap-overrides h1 { /* 100+1=101 priority score, yay! */
  line-height: 1;
  color: inherit;
}
Run Code Online (Sandbox Code Playgroud)

  • @chharvey:因为你最终必须更好地计算.如果它是这个例子中的一个类,它将是prio 11,如果css定义将在bootstrap.css之后,那么我们会很高兴.然而,这将非常接近并将其设置为增加prio大时间的id,我们不必担心计算prio并且测试是否我们总是正确的测试.如果您想明确覆盖默认值,则更容易使用id. (2认同)
  • 是您提到的要点(id为100,类/伪类为10,标记选择器/伪元素为1)是文字,还是您在本示例中只是将这些值补齐? (2认同)
  • 您的解释很好,作为向 css 新手解释的一种方式是有道理的,但具有误导性。您提到的正确名称是 css `specificity`,它定义了每个 css 选择器的“权重”。这里有一些关于它的更多细节:https://css-tricks.com/specifics-on-css-specificity/#article-header-id-1 (2认同)

Mar*_*der 81

在你的html的head部分,你的custom.css位于bootstrap.css下面.

<link href="bootstrap.min.css" rel="stylesheet">
<link href="custom.css" rel="stylesheet">
Run Code Online (Sandbox Code Playgroud)

然后在custom.css中,您必须为要覆盖的元素使用完全相同的选择器.如果legend它只是保留legend在你的custom.css中,因为bootstrap没有任何更具体的选择器.

legend {
  display: inline;
  width: auto;
  padding: 0;
  margin: 0;
  font-size: medium;
  line-height: normal;
  color: #000000;
  border: 0;
  border-bottom: none;
}
Run Code Online (Sandbox Code Playgroud)

但是,h1例如,你必须照顾更具体的选择器,如.jumbotron h1因为

h1 {
  line-height: 2;
  color: #f00;
}
Run Code Online (Sandbox Code Playgroud)

不会覆盖

.jumbotron h1,
.jumbotron .h1 {
  line-height: 1;
  color: inherit;
}
Run Code Online (Sandbox Code Playgroud)

以下是对css选择器特异性的有用解释,您需要了解这些解释器以确切了解哪些样式规则将应用于元素. http://css-tricks.com/specifics-on-css-specificity/

其他一切只是复制/粘贴和编辑样式的问题.


Rah*_*til 26

它不应该影响加载时间,因为您要覆盖基本样式表的各个部分.

以下是我个人遵循的一些最佳做法:

  1. 始终在基本CSS文件之后加载自定义CSS(无响应).
  2. !important尽可能避免使用.这可以覆盖基本CSS文件中的一些重要样式.
  3. 如果您不想丢失媒体查询,请始终在custom.css之后加载bootstrap-responsive.css. - 必须遵循
  4. 更喜欢修改所需的属性(不是全部).

  • 既然`bootstrap-responsive.css`不再存在,这仍然是真的,还是不再相关? (6认同)

Par*_*van 17

将custom.css文件链接为bootstrap.css下面的最后一个条目.Custom.css样式定义将覆盖bootstrap.css

HTML

<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/custom.css" rel="stylesheet">
Run Code Online (Sandbox Code Playgroud)

复制custom.css中图例的所有样式定义并对其进行更改(如margin-bottom:5px; - 这将覆盖margin-bottom:20px;)


Viv*_*lye 9

要重置legendbootstrap 中定义的样式,您可以在 css 文件中执行以下操作:

legend {
  all: unset;
}
Run Code Online (Sandbox Code Playgroud)

参考:https : //css-tricks.com/almanac/properties/a/all/

CSS 中的 all 属性重置所有选定元素的属性,除了控制文本方向的方向和 unicode-bidi 属性。

可能的值是:initial, inherit& unset

旁注:clear属性与floathttps://css-tricks.com/almanac/properties/c/clear/)相关使用


kon*_*yak 7

请参阅https://bootstrap.themes.guide/how-to-customize-bootstrap.html

\n\n
    \n
  1. 对于简单的 CSS 覆盖,您可以在 bootstrap.css 下方添加 custom.css

    \n\n
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">\n<link rel="stylesheet" type="text/css" href="css/custom.css">\n
    Run Code Online (Sandbox Code Playgroud)
  2. \n
  3. 对于更广泛的更改,推荐使用 SASS 方法。

    \n\n
      \n
    • 创建您自己的 custom.scss
    • \n
    • 更改custom.scss后导入Bootstrap
    • \n
    • 例如,让\xe2\x80\x99s 将正文背景颜色更改为浅灰色#eeeeee,并将蓝色主要上下文颜色更改为 Bootstrap 的 $purple 变量...

      \n\n
      /* custom.scss */    \n\n/* import the necessary Bootstrap files */\n@import "bootstrap/functions";\n@import "bootstrap/variables";\n\n/* -------begin customization-------- */   \n\n/* simply assign the value */ \n$body-bg: #eeeeee;\n\n/* or, use an existing variable */\n$theme-colors: (\n  primary: $purple\n);\n/* -------end customization-------- */  \n\n/* finally, import Bootstrap to set the changes! */\n@import "bootstrap";\n
      Run Code Online (Sandbox Code Playgroud)
    • \n
  4. \n
\n


Zim*_*Zim 5

2020 年更新 - Bootstrap 4、Bootstrap 5 测试版

覆盖 Bootstrap CSS 时需要遵循 3 条规则。

  1. bootstrap.css在 CSS 规则之前导入/包含(覆盖)
  2. 为您的 CSS 选择器添加更多特异性(或等同于)
  3. 如果任何规则被覆盖,请使用 !important 属性来强制您的规则。如果您遵循规则 1 和 2,这应该不是必需的,除非使用通常包含 !important 的 Bootstrap 实用程序类,如解释here

是的,覆盖应该放在一个单独的styles.css(或custom.css)文件中,以便bootstrap.css保持不变。这使得在不影响覆盖的情况下更容易升级 Bootstrap 版本。该参考styles.css如下之后bootstrap.css的覆盖工作。

<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/styles.css">
Run Code Online (Sandbox Code Playgroud)

只需在自定义 CSS 中添加任何需要的更改。例如:

legend {
  display: block;
  width: inherit;
  padding: 0;
  margin-bottom: 0;
  font-size: inherit;
  line-height: inherit;
  color: inherit;
  white-space: initial;
}
Run Code Online (Sandbox Code Playgroud)

注:这是不是一个很好的做法,用 !important在覆盖CSS,除非你要替换的一个引导实用工具类CSS 特异性 始终适用于一个 CSS 类来覆盖另一个。只要确保您使用的 CSS 选择器与 bootstrap.css相同或更具体

例如,考虑 Bootstrap 4 深色导航栏链接颜色。这是bootstrap.css...

.navbar-dark .navbar-nav .nav-link {
    color: rgba(255,255,255,.5);
}
Run Code Online (Sandbox Code Playgroud)

因此,要覆盖导航栏链接颜色,您可以使用相同的选择器,或更具体的选择器,例如:

#mynavbar .navbar-nav .nav-link {
    color: #ffcc00;
}
Run Code Online (Sandbox Code Playgroud)

当CSS选择器是一样的,最后一个优先,它为什么styles.css应该遵循bootstrap.css