【问题标题】:Bootstrap CSS overwriting my custom CSSBootstrap CSS 覆盖我的自定义 CSS
【发布时间】:2014-10-01 13:29:54
【问题描述】:

我正在使用 Twitter Bootstrap 的 CSS,由于某些原因它覆盖了我的一些类。

首先我在我的自定义 CSS 之前加载它(我使用的是 jam 和 stylus 模板引擎): HTML 标头:

doctype html
html
  head
    title= title
    link(rel='stylesheet', href='/css/vendor/bootstrap.min.css')
    link(rel='stylesheet', href='/css/style.css')

在我的 HTML 正文中:

table.table.table-striped
  thead
    th some items
    th some items
    th some items
  tbody
    tr
      td.table__td 
        a.table__link(href='/') some items
    tr
      td.table__td 
        a.table__link(href='/') some items
    tr
      td.table__td 
        a.table__link(href='/') some items

CSS:

.table__td
  padding 0

但是我在我的表 td 上得到了原始的 8px 填充:

怎么会这样?

非常感谢

【问题讨论】:

标签: html css twitter-bootstrap pug stylus


【解决方案1】:

因为bootstrap的css声明不仅仅是

.table td /*specificity = 0 1 1*/

但是

.table > tbody> tr > td /*specificity = 0 1 3*/

所以 Bootstrap 的 CSS 选择器特异性比你的要大。

如果您希望应用您的规则,则必须具有更大的特异性(或相同级别,但在引导程序之后声明)才能覆盖引导程序样式。

例如:

.table > tbody> tr > td.table__td{ /*specificity = 0 2 3*/
  padding:0;
}

或者简单地说:

.table > td.table__td{  /*specificity = 0 2 1*/
  padding:0;
}

【讨论】:

    【解决方案2】:

    因为 bootstrap 具有更高的 CSS 优先级,因为它的特殊性。

    http://css-tricks.com/specifics-on-css-specificity/

    http://www.alternategateways.com/tutorials/css/css-101/part-four-the-css-order-of-precedence

    解决方案

    将您的 CSS 规则更改为:

    .table > tbody > tr > td.table__td
    

    【讨论】:

    • 此规则不起作用,因为单元格不在thead 中,而是在tbody 中!
    【解决方案3】:

    这是因为 bootstrap 有更具体的声明。 使用这个:.table>....>td。如果你不能这样做,请使用!important.table td{padding:0!important}

    【讨论】:

    • !important 在这种情况下是一个黑客,它将在未来咬住 OP,IMO。
    【解决方案4】:

    试试

    .table > tbody > tr > td.table__td { 填充 0}

    【讨论】:

      猜你喜欢
      • 2021-11-12
      • 2017-12-23
      • 1970-01-01
      • 1970-01-01
      • 2017-03-20
      • 2014-11-25
      • 2017-03-05
      • 1970-01-01
      • 2020-08-10
      相关资源
      最近更新 更多