60

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

4 回答 4

138

更新:引导 5。

v5断点参考


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

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

v4断点参考

v4 alpha6断点参考


低于完整的选项和值。

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

@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) { ... }
于 2017-01-17T13:57:24.387 回答
14

在这里回答了一个类似的问题

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

@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 断点演示

于 2017-01-17T14:05:59.990 回答
2

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

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

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

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

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

于 2021-06-24T10:30:06.897 回答
0

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

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

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

例如小文本

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

于 2019-09-22T04:40:16.887 回答