在 Bootstrap 4-alpha 中使用媒体断点

Using media breakpoints in Bootstrap 4-alpha

在 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);

更新: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) { ... }

我回答了一个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

Bootstrap 有一种使用媒体查询为不同站点定义不同任务的方法。 它使用四个断点。

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

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

例如小文本

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

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

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

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

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

注意当文件名前包含“_”时,它不会生成到CSS中,它会被处理为一个部分文件,这意味着仅导入并被编译器忽略。