【问题标题】:How can I add custom breakpoint in Bootstrap 4 and use it for both up and down?如何在 Bootstrap 4 中添加自定义断点并将其用于上下?
【发布时间】:2019-05-18 20:50:42
【问题描述】:

如何在 Bootstrap 4 中添加自定义变量并将其用作上下断点?

断点是这样使用的

@include media-breakpoint-up(md) {
   // 
}

我可以添加自定义大小(它们需要排序,否则会出错)

$grid-breakpoints: (
    xs: 0,
    sm: 576px,
    md: 768px,
    mycustomvar: 850px,
    lg: 992px,
    xl: 1200px
);

我尝试使用它们

.current-menu-item {
   @include media-breakpoint-up(mycustomvar) {
       font-size: 2222px;
   }
   @include media-breakpoint-down(mycustomvar) {
       font-size: 1111px;
   }
}

这似乎工作正常,我明白了

@media (min-width: 850px) {
    .current-menu-item {
        font-size: 2222px;
    }
}

但是对于down,它会因错误的断点而失败

@media (max-width: 991.98px) {
    .current-menu-item {
        font-size: 11111px;
    }
}

【问题讨论】:

    标签: twitter-bootstrap sass bootstrap-4


    【解决方案1】:

    当下面有另一个断点时,Bootstrap 在构造媒体查询时会减去 .02px。否则你会有重叠这样的@media查询......

    // Large devices (desktops, 992px and up)
    @media (min-width: 992px) { ... }
    
    // Medium devices (tablets, less than 992px)
    @media (max-width: 992px) { ... }
    

    记住,任何断点开始在给定的px宽度,所以下一个较小的断点必须小于较大断点的最小px宽度. Read more in the docs

    这是 Bootstrap 4 breakpoint-max @function:

    @function breakpoint-max($name, $breakpoints: $grid-breakpoints) {
      $next: breakpoint-next($name, $breakpoints);
      @return if($next, breakpoint-min($next, $breakpoints) - .02px, null);
    }
    

    如果您想继续对自定义断点进行重叠的媒体查询,请覆盖 bootstrap-max 函数(删除 .02px 减法)...

    @function breakpoint-max($name, $breakpoints: $grid-breakpoints) {
      $next: breakpoint-next($name, $breakpoints);
      @return if($next, breakpoint-min($next, $breakpoints), null);
    }
    

    Demo

    【讨论】:

    • 您好,谢谢您的回复。嗯,我不确定,这似乎是一个 hack。为什么 Bootstrap 不根据 $grid-breakpoints 计算断点?此外,如果需要在网格断点之外进行自定义,只需编写断点的常用语法会更简洁,目前我在 SCSS 文件中有 Bootstrap 和常用的 @media 定义......
    • 我不太明白这个问题。 Bootstrap does 使用网格断点计算断点,如您在函数中所见。如上所述,它只是对 .02px 进行调整以防止断点范围重叠。我认为您的问题是为什么“由于错误的断点而失败?”我认为已经回答了。
    猜你喜欢
    • 2018-07-21
    • 2015-09-25
    • 2021-10-05
    • 2020-10-15
    • 2020-08-08
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 2019-03-02
    相关资源
    最近更新 更多