【问题标题】:Using media breakpoints in Bootstrap 4-alpha在 Bootstrap 4-alpha 中使用媒体断点
【发布时间】:2017-06-01 14:40:18
【问题描述】:

在 Bootstrap 3 中我使用这个:

.something {
    padding: 5px;
    @media screen and (min-width: $screen-sm-min) { 
        padding: 20px;
    }
    @media screen and (min-width: $screen-md-min) { 
        padding: 40px;
    }
}

如何在 Boostrap 4-alpha 中做同样的事情?我在他们的文档中找不到示例。 这是在 variables.scss 中

$grid-breakpoints: (
  xs: 0,
  sm: 576px,
  md: 768px,
  lg: 992px,
  xl: 1200px
) !default;
@include _assert-ascending($grid-breakpoints, "$grid-breakpoints");
@include _assert-starts-at-zero($grid-breakpoints);

【问题讨论】:

    标签: twitter-bootstrap bootstrap-4 bootstrap-5


    【解决方案1】:

    更新:Bootstrap 5.

    v5 breakpoints reference


    原答案:像这样使用断点混合:

    .something {
        padding: 5px;
        @include media-breakpoint-up(sm) { 
            padding: 20px;
        }
        @include media-breakpoint-up(md) { 
            padding: 40px;
        }
    }
    

    v4 breakpoints reference

    v4 alpha6 breakpoints reference


    低于完整的选项和值。

    断点和向上(切换值及以上):

    @include media-breakpoint-up(xs) { ... }
    @include media-breakpoint-up(sm) { ... }
    @include media-breakpoint-up(md) { ... }
    @include media-breakpoint-up(lg) { ... }
    @include media-breakpoint-up(xl) { ... }
    

    断点和向上值:

    // Extra small devices (portrait phones, less than 576px)
    // No media query since this is the default in Bootstrap
    
    // Small devices (landscape phones, 576px and up)
    @media (min-width: 576px) { ... }
    
    // Medium devices (tablets, 768px and up)
    @media (min-width: 768px) { ... }
    
    // Large devices (desktops, 992px and up)
    @media (min-width: 992px) { ... }
    
    // Extra large devices (large desktops, 1200px and up)
    @media (min-width: 1200px) { ... }
    

    断点和向下(在值和向下切换):

    @include media-breakpoint-down(xs) { ... }
    @include media-breakpoint-down(sm) { ... }
    @include media-breakpoint-down(md) { ... }
    @include media-breakpoint-down(lg) { ... }
    

    断点和向下值:

    // Extra small devices (portrait phones, less than 576px)
    @media (max-width: 575px) { ... }
    
    // Small devices (landscape phones, less than 768px)
    @media (max-width: 767px) { ... }
    
    // Medium devices (tablets, less than 992px)
    @media (max-width: 991px) { ... }
    
    // Large devices (desktops, less than 1200px)
    @media (max-width: 1199px) { ... }
    
    // Extra large devices (large desktops)
    // No media query since the extra-large breakpoint has no upper bound on its width
    

    仅断点:

    @include media-breakpoint-only(xs) { ... }
    @include media-breakpoint-only(sm) { ... }
    @include media-breakpoint-only(md) { ... }
    @include media-breakpoint-only(lg) { ... }
    @include media-breakpoint-only(xl) { ... }
    

    仅断点值(仅在值之间切换):

    // Extra small devices (portrait phones, less than 576px)
    @media (max-width: 575px) { ... }
    
    // Small devices (landscape phones, 576px and up)
    @media (min-width: 576px) and (max-width: 767px) { ... }
    
    // Medium devices (tablets, 768px and up)
    @media (min-width: 768px) and (max-width: 991px) { ... }
    
    // Large devices (desktops, 992px and up)
    @media (min-width: 992px) and (max-width: 1199px) { ... }
    
    // Extra large devices (large desktops, 1200px and up)
    @media (min-width: 1200px) { ... }
    

    【讨论】:

    • 您的第一个代码示例对我不起作用。 mixin 只是在它的父类中渲染内容。
    【解决方案2】:

    我回复了similar question here

    正如@Syden 所说,mixin 会起作用。另一种选择是像这样使用 SASS map-get..

    @media (min-width: map-get($grid-breakpoints, sm)){
      .something {
        padding: 10px;
       }
    }
    
    @media (min-width: map-get($grid-breakpoints, md)){
      .something {
        padding: 20px;
       }
    }
    

    http://www.codeply.com/go/0TU586QNlV


    Bootstrap 4 Breakpoints demo

    【讨论】:

      【解决方案3】:

      Bootstrap 可以使用媒体查询为不同的站点定义不同的任务。 它使用四个断点。

      我们有小于 576 像素的超小屏幕尺寸,我的意思是它的尺寸从 576 到 768 像素。

      中等屏幕尺寸占用的屏幕尺寸从 768 像素到 992 像素 大屏幕尺寸从 992 像素到 1200 像素。

      例如小文本

      这意味着在 576px 和 768px 之间的小屏幕上,将文本居中 对于中等屏幕,将“sm”更改为“md”,同样适用于大“lg”

      【讨论】:

        【解决方案4】:

        除了上述之外,如果你想在模块级样式表中使用引导媒体断点(例如在 create-react-app 组件中),默认情况下它们是未定义的,你需要导入 mixins 和将其依赖项分别放入每个模块中。

        您可以在每个文件的顶部导入它们:

        @import 'bootstrap/scss/_functions';
        @import 'bootstrap/scss/_variables';
        @import 'bootstrap/scss/mixins/_breakpoints';
        

        或者您可以在您的 scss 文件夹中创建一个 _mixins.scss 文件,包含上面的行,然后将该文件导入到模块中。

        请注意,当您在文件名前包含“_”时,它不会生成到 CSS 中,它将作为部分文件处理,仅用于导入并被编译器忽略。

        【讨论】:

          猜你喜欢
          • 2017-11-13
          • 2017-11-21
          • 1970-01-01
          • 2020-02-21
          • 1970-01-01
          • 2013-11-04
          • 1970-01-01
          • 1970-01-01
          • 2014-07-22
          相关资源
          最近更新 更多