css - 在 Bootstrap 4 中使用容器宽度

标签 css twitter-bootstrap sass

我们最近更改为 Bootstrap 4,我正在研究设置宽度、断点等的所有新方法。

我想让一个 div 具有当前断点容器宽度的 max-width,但我找不到该特定宽度的标准 SASS 变量名。

请注意,我不能为此使用类(例如 .col-xl-),因为我无权访问 HTML 的这个特定部分。

最佳答案

看看这个内置的 mixin:

// For each breakpoint, define the maximum width of the container in a media query
@mixin make-container-max-widths($max-widths: $container-max-widths, $breakpoints: $grid-breakpoints) {
    @each $breakpoint, $container-max-width in $max-widths {
        @include media-breakpoint-up($breakpoint, $breakpoints) {
            width: $container-max-width;
            max-width: 100%;
        }
    }
}

您可以根据自己的需要创建一个类似的,例如:

@mixin make-max-widths-container-width($max-widths: $container-max-widths, $breakpoints: $grid-breakpoints) {
    @each $breakpoint, $container-max-width in $max-widths {
        @include media-breakpoint-up($breakpoint, $breakpoints) {
            max-width: $container-max-width;
        }
    }
}

并使用它:

.my-custom-class{
   @include make-max-widths-container-width();
}

生成的“已编译”CSS(断点和容器宽度具有默认值):

@media (min-width: 576px) {
    .my-custom-class {
        max-width: 540px;
    }
}

@media (min-width: 768px) {
    .my-custom-class {
        max-width: 720px;
    }
}

@media (min-width: 992px) {
    .my-custom-class {
        max-width: 960px;
    }
}

@media (min-width: 1200px) {
    .my-custom-class {
        max-width: 1140px;
    }
}

关于css - 在 Bootstrap 4 中使用容器宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41280235/

相关文章:

css - 使用流体行和列的 Shiny 应用程序存在溢出问题

html - Margin-Top < 0 会导致缩放错误

html - Bootstrap 选项卡式导航/拆分下拉按钮问题

html - 尽管将其设置为 scale=1,但 Bootstrap 模型未在 iPhone 5s 的全视口(viewport)中显示

html - CSS3 媒体查询未加载

css - 如何在 Rails 4 中缩小 CSS?

html - 不影响视口(viewport)大小的全宽横幅

javascript - CSS 动画错误

html - 设备上的 CSS 与浏览器不同

ruby - oily_png ruby​​ gem 安装错误