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

Iva*_*pić 41 twitter-bootstrap

在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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我如何在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);
Run Code Online (Sandbox Code Playgroud)

Syd*_*den 107

使用这样的断点mixins:

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

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) { ... }
Run Code Online (Sandbox Code Playgroud)

断点和上升值:

// 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) { ... }
Run Code Online (Sandbox Code Playgroud)

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

@include media-breakpoint-down(xs) { ... }
@include media-breakpoint-down(sm) { ... }
@include media-breakpoint-down(md) { ... }
@include media-breakpoint-down(lg) { ... }
Run Code Online (Sandbox Code Playgroud)

断点和下降值:

// 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
Run Code Online (Sandbox Code Playgroud)

仅限断点:

@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) { ... }
Run Code Online (Sandbox Code Playgroud)

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

// 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) { ... }
Run Code Online (Sandbox Code Playgroud)

  • 您的第一个代码示例对我不起作用。mixin 只是渲染其父类中的内容。 (2认同)

Zim*_*Zim 10

在这里回答了类似的问题

正如@Syden所说,mixins将起作用.另一种选择是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;
   }
}
Run Code Online (Sandbox Code Playgroud)

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


ors*_*zky 10

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

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

@import 'bootstrap/scss/_functions';
@import 'bootstrap/scss/_variables';
@import 'bootstrap/scss/mixins/_breakpoints';
Run Code Online (Sandbox Code Playgroud)

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

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