如何覆盖Bootstrap CSS样式?

 静风疾水 发布于 2023-02-08 17:49

我需要修改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;
}

我可以删除所有内容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;
}

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

legend {
  clear: all;
}

smugglerFlyn.. 454

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

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

ID为100分

类和伪类的10分

标记选择器和伪元素为1分

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

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

解决此问题的最简单方法是为页面上的一个根元素分配其他任意ID,如下所示:

这样,您可以使用您的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;
}

@chharvey:因为你最终必须更好地计算.如果它是这个例子中的一个类,它将是prio 11,如果css定义将在bootstrap.css之后,那么我们会很高兴.然而,这将非常接近并将其设置为增加prio大时间的id,我们不必担心计算prio并且测试是否我们总是正确的测试.如果您想明确覆盖默认值,则更容易使用id. (2认同)

是您提到的要点(id为100,类/伪类为10,标记选择器/伪元素为1)是文字,还是您在本示例中只是将这些值补齐? (2认同)


Markus Kottl.. 81

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



然后在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;
}

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

h1 {
  line-height: 2;
  color: #f00;
}

不会覆盖

.jumbotron h1,
.jumbotron .h1 {
  line-height: 1;
  color: inherit;
}

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

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

4 个回答
  • 使用!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;
    }
    

    2023-02-08 17:50 回答
  • 它不应该影响加载时间,因为您要覆盖基本样式表的各个部分.

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

      始终在基本CSS文件之后加载自定义CSS(无响应).

      !important尽可能避免使用.这可以覆盖基本CSS文件中的一些重要样式.

      如果您不想丢失媒体查询,请始终在custom.css之后加载bootstrap-responsive.css. - 必须遵循

      更喜欢修改所需的属性(不是全部).

    2023-02-08 17:51 回答
  • 将custom.css文件链接为bootstrap.css下面的最后一个条目.Custom.css样式定义将覆盖bootstrap.css

    HTML

    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/custom.css" rel="stylesheet">
    

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

    2023-02-08 17:51 回答
  • 在你的html的head部分,你的custom.css位于bootstrap.css下面.

    <link href="bootstrap.min.css" rel="stylesheet">
    <link href="custom.css" rel="stylesheet">
    

    然后在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;
    }
    

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

    h1 {
      line-height: 2;
      color: #f00;
    }
    

    不会覆盖

    .jumbotron h1,
    .jumbotron .h1 {
      line-height: 1;
      color: inherit;
    }
    

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

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

    2023-02-08 17:51 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有