Html 覆盖或添加样式到bootstrap 3中的预定义类

Html 覆盖或添加样式到bootstrap 3中的预定义类,html,css,twitter-bootstrap-3,Html,Css,Twitter Bootstrap 3,这是关于bootstrap3的。我想在重新调整浏览器大小时更改预定义引导3列的颜色 以下是index.html的代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" conten

这是关于bootstrap3的。我想在重新调整浏览器大小时更改预定义引导3列的颜色

以下是index.html的代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap Template</title>


<link href="css/bootstrap.css" rel="stylesheet" type="text/css" media="screen"/>
<link href="css/custom.css" rel="stylesheet" type="text/css" media="screen"/>
<!--<link href="css/bootstrap-theme.css" rel="stylesheet" type="text/css" media="screen"/>-->
</head>

<body>
    <div class="container">
        <div class="row">
           <div class="col-lg-9 col-md-4 col-sm-2 col-xs-12 border">one</div>
           <div class="col-lg-2 col-md-4 col-sm-8 col-xs-12 border">two</div>
           <div class="col-lg-1 col-md-4 col-sm-2 col-xs-12 border">three</div>
        </div>
   </div>



<script src="js/jquery.js"></script>
<script src="js/bootstrap.js"></script>
</body>
</html>
重新调整浏览器大小时,第一列的背景色应为红色,然后是黄色,然后是绿色,最后是蓝色。但是,在浏览器中查看此网页并重新调整其大小时,所有列的背景颜色都保持蓝色

我想更改颜色(覆盖或向Bootstrap3中的预定义类添加样式)。如何做到这一点?

试试以下方法:

HTML

  <!DOCTYPE html>
  <html lang="en">
  <head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Bootstrap Template</title>


  <link href="css/bootstrap.css" rel="stylesheet" type="text/css" media="screen"/>
  <link href="css/custom.css" rel="stylesheet" type="text/css" media="screen"/>
  <!--<link href="css/bootstrap-theme.css" rel="stylesheet" type="text/css"  media="screen"/>-->
  </head>

  <body>
  <div class="container">
    <div class="row">
       <div class="col-lg-9 col-md-4 col-sm-2 col-xs-12 border colbg">one</div>
       <div class="col-lg-2 col-md-4 col-sm-8 col-xs-12 border colbg">two</div>
       <div class="col-lg-1 col-md-4 col-sm-2 col-xs-12 border colbg">three</div>
    </div>
   </div>



  <script src="js/jquery.js"></script>
  <script src="js/bootstrap.js"></script>
  </body>
  </html>

希望这有帮助。

您的问题是css不会在某些宽度上应用某些类的样式,它只能通过媒体查询告诉您在不同宽度上应用哪些样式规则。引导通过使用媒体查询来工作

仅仅因为您使用的是bootstrap,并不意味着它将神奇地将媒体查询应用到特定宽度的自己的类。请参阅以查看引导程序内部用于应用这些样式规则的媒体查询。媒体查询非常简单,但可能看起来有点让人望而生畏。你会习惯它们,当你更多地在它们周围时,它们会变得有意义

你的背景是蓝色的,因为它是最底层的样式规则,所以它是唯一被应用的样式规则,只要你能有你想要以不同宽度显示的样式规则。这就是媒体查询的目的。它们仅在指定的宽度处应用样式,并在任何其他宽度处消失

下面是您想要覆盖css类的内容

css

编辑:

问题是您正在覆盖引导类,这意味着您正在覆盖将显示的样式。从现在起,如果您尝试使用引导列,它们将具有这些样式,我建议您改为向元素添加一个类,并为该类添加一个样式规则,以代替覆盖它

HTML


它被称为级联样式表是有原因的……这个解决方案太愚蠢了。他们可以添加一个名为“col sm”“col xs”的类,等等。。。所有其他“col-xs-1,col-xs-2,…”都将继承。网络糟透了。这是如何覆盖引导类的?您如何知道OP在什么屏幕尺寸下需要不同的颜色?@TJ这些是引导程序用来识别设备大小的媒体查询。他希望背景颜色可以改变为不同的大小。因此,我们可以使用媒体查询来实现相同的功能,而不是使用列类名称。
.colbg
由bootstrap使用。@TJ No,这是一个I类分配,非常有用!节省大量时间
  <!DOCTYPE html>
  <html lang="en">
  <head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Bootstrap Template</title>


  <link href="css/bootstrap.css" rel="stylesheet" type="text/css" media="screen"/>
  <link href="css/custom.css" rel="stylesheet" type="text/css" media="screen"/>
  <!--<link href="css/bootstrap-theme.css" rel="stylesheet" type="text/css"  media="screen"/>-->
  </head>

  <body>
  <div class="container">
    <div class="row">
       <div class="col-lg-9 col-md-4 col-sm-2 col-xs-12 border colbg">one</div>
       <div class="col-lg-2 col-md-4 col-sm-8 col-xs-12 border colbg">two</div>
       <div class="col-lg-1 col-md-4 col-sm-2 col-xs-12 border colbg">three</div>
    </div>
   </div>



  <script src="js/jquery.js"></script>
  <script src="js/bootstrap.js"></script>
  </body>
  </html>
 @media (max-width: 767px) {
     .colbg {background-color:blue; }
 }

 @media (min-width: 768px) and (max-width: 991px) {
     .colbg {background-color:green; }
 }

 @media (min-width: 992px) and (max-width: 1199px) {
      .colbg {background-color:yellow; }
 }

 @media (min-width: 1200px) {
      .colbg {background-color:red; }
 }
/* Styles go here */
.border
{
  border:2px solid;       
}
/*screen-xs*/
@media (max-width: 768px) { 
  .col-xs-1,
  .col-xs-2,
  .col-xs-3,
  .col-xs-4,
  .col-xs-5,
  .col-xs-6,
  .col-xs-7,
  .col-xs-8,
  .col-xs-9,
  .col-xs-10,
  .col-xs-11,
  .col-xs-12{background-color:blue;}
}
/*screen-sm*/
@media (min-width: 768px) and (max-width: 992px) { 
  .col-sm-1,
  .col-sm-2,
  .col-sm-3,
  .col-sm-4,
  .col-sm-5,
  .col-sm-6,
  .col-sm-7,
  .col-sm-8,
  .col-sm-9,
  .col-sm-10,
  .col-sm-11,
  .col-sm-12{background-color:green;}
}
/*screen-md*/
@media (min-width: 992px) and (max-width: 1200px) { 
  .col-md-1,
  .col-md-2,
  .col-md-3,
  .col-md-4,
  .col-md-5,
  .col-md-6,
  .col-md-7,
  .col-md-8,
  .col-md-9,
  .col-md-10,
  .col-md-11,
  .col-md-12{background-color:yellow;}
}
/*screen-lg corresponds with col-lg*/
@media (min-width: 1200px) {  
  .col-lg-1,
  .col-lg-2,
  .col-lg-3,
  .col-lg-4,
  .col-lg-5,
  .col-lg-6,
  .col-lg-7,
  .col-lg-8,
  .col-lg-9,
  .col-lg-10,
  .col-lg-11,
  .col-lg-12{background-color:red;}
}
<div class="container">
  <div class="row">
    <div class="col-lg-9 col-md-4 col-sm-2 col-xs-12 border myColoredCol">one</div>
    <div class="col-lg-2 col-md-4 col-sm-8 col-xs-12 border myColoredCol">two</div>
    <div class="col-lg-1 col-md-4 col-sm-2 col-xs-12 border myColoredCol">three</div>
  </div>
</div>
/* Styles go here */
.border
{
  border:2px solid;       
}
/*screen-xs*/
@media (max-width: 768px) { 
  .myColoredCol{background-color:blue;}
}
/*screen-sm*/
@media (min-width: 768px) and (max-width: 992px) { 
  .myColoredCol{background-color:green;}
}
/*screen-md*/
@media (min-width: 992px) and (max-width: 1200px) { 
  .myColoredCol{background-color:yellow;}
}
/*screen-lg corresponds with col-lg*/
@media (min-width: 1200px) {  
  .myColoredCol{background-color:red;}
}