Bootstrap:如何为每个屏幕宽度指定不同的背景颜色?

and*_*990 6 bootstrap-4

我是 Bootstrap 世界的新手,但有一个问题如下。

我了解到可以根据屏幕尺寸更改列宽,如下所示:

<div class="col-lg-4 col-md-6 col-sm-8 col-xs-12 bg-warning">
<h1>Hello, world!</h1>
This is some text that I wanted to include here.
</div>
Run Code Online (Sandbox Code Playgroud)

我在那里添加了背景警告,因为我的问题是 - 是否可以随着屏幕宽度的变化而更改背景颜色 - 正如我们能够使用列宽本身一样?

例如,大屏幕 - bg-warning、Medium、bg-success 等

小智 4

Bootstrap 没有提供 html 类来执行您所要求的操作,您必须使用像提到的 ssc-hrep3 这样的媒体查询。

但由于您使用的是 Bootstrap 4,您可以使用 Sass mixins 来创建媒体查询: Using media Breakpoints in Bootstrap 4-alpha

使用上面链接中的答案示例,您可以制作如下内容:

.bg-warning {       
   @include media-breakpoint-up(sm) { 
       background:#fff;
   }
   @include media-breakpoint-up(md) { 
       background:#f6f6f6;
   }
   @include media-breakpoint-up(lg) { 
       background:#fafafa;
   }
}
Run Code Online (Sandbox Code Playgroud)