Skip to main content Skip to docs navigation

Breakpoints

breakpoints是可自定义的宽度,用于确定Bootstrap中响应布局在设备或视口大小上的行为。

核心概念

  • breakpoints是响应式设计的基石使用它们可以控制何时可以将布局调整为特定视口或设备大小。

  • 使用媒体查询通过breakpoints构建CSS媒体查询是CSS的一个功能,允许你根据一组浏览器和操作系统参数有条件地应用样式。 我们在媒体查询中最常用的是min-width

  • 我们的目标是移动端优先、响应式设计Bootstrap的CSS旨在应用最少量的样式,使布局在最小的breakpoints处工作,然后对样式进行分层,以针对较大的设备调整设计。 这优化了你的CSS,缩短了渲染时间,并为你的访问者提供了良好的体验。

可用breakpoints

Bootstrap包括六个默认breakpoints,有时称为_grid tiers_,用于响应式构建。 如果你使用我们的源Sass文件,可以自定义这些breakpoints。

Breakpoint Class infix Dimensions
Extra small None <576px
Small sm ≥576px
Medium md ≥768px
Large lg ≥992px
Extra large xl ≥1200px
Extra extra large xxl ≥1400px

选择每个breakpoints以舒适地容纳宽度为12的倍数的容器。 breakpoints也代表了常见设备大小和视口尺寸的子集——它们并不是专门针对每个用例或设备的。 相反,这些系列为几乎任何设备提供了强大而一致的基础。

这些breakpoints可以通过Sass进行自定义——你可以在_variables.scss样式表的Sass映射中找到它们。

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

有关如何修改Sass映射和变量的更多信息和示例,请参阅[网格文档的CSS部分](/docs/5.3/layout/grid/#css。

Media queries

由于Bootstrap首先被开发为移动的,我们使用了一些媒体查询为我们的布局和接口创建合理的breakpoints。 这些breakpoints主要基于最小视口宽度,并允许我们在视口更改时放大元素。

Min-width

Bootstrap主要在布局、网格系统和组件的源Sass文件中使用以下媒体查询范围或breakpoints。

// Source mixins

// No media query necessary for xs breakpoint as it's effectively `@media (min-width: 0) { ... }`
@include media-breakpoint-up(sm) { ... }
@include media-breakpoint-up(md) { ... }
@include media-breakpoint-up(lg) { ... }
@include media-breakpoint-up(xl) { ... }
@include media-breakpoint-up(xxl) { ... }

// Usage

// Example: Hide starting at `min-width: 0`, and then show at the `sm` breakpoint
.custom-class {
  display: none;
}
@include media-breakpoint-up(sm) {
  .custom-class {
    display: block;
  }
}

这些Sass mixin在我们编译的CSS中使用Sass变量中声明的值进行翻译。 例如:

// X-Small devices (portrait phones, less than 576px)
// No media query for `xs` 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) { ... }

// X-Large devices (large desktops, 1200px and up)
@media (min-width: 1200px) { ... }

// XX-Large devices (larger desktops, 1400px and up)
@media (min-width: 1400px) { ... }

Max-width

我们偶尔会使用另一个方向的媒体查询(给定的屏幕大小_或者更小_):

// No media query necessary for xs breakpoint as it's effectively `@media (max-width: 0) { ... }`
@include media-breakpoint-down(sm) { ... }
@include media-breakpoint-down(md) { ... }
@include media-breakpoint-down(lg) { ... }
@include media-breakpoint-down(xl) { ... }
@include media-breakpoint-down(xxl) { ... }

// Example: Style from medium breakpoint and down
@include media-breakpoint-down(md) {
  .custom-class {
    display: block;
  }
}

这些mixin采用那些声明的breakpoints,从中减去.02px,并将其用作我们的max-width值。 例如:

// `xs` returns only a ruleset and no media query
// ... { ... }

// `sm` applies to x-small devices (portrait phones, less than 576px)
@media (max-width: 575.98px) { ... }

// `md` applies to small devices (landscape phones, less than 768px)
@media (max-width: 767.98px) { ... }

// `lg` applies to medium devices (tablets, less than 992px)
@media (max-width: 991.98px) { ... }

// `xl` applies to large devices (desktops, less than 1200px)
@media (max-width: 1199.98px) { ... }

// `xxl` applies to x-large devices (large desktops, less than 1400px)
@media (max-width: 1399.98px) { ... }
**为什么要减去.02px?**浏览器当前不支持range context queries,因此我们绕过min-max-前缀 的限制以及具有fractional widths的视口(例如,在高dpi设备上的某些条件下可能发生)。

Single breakpoint

还有媒体查询和mixins,用于使用最小和最大breakpoints宽度针对屏幕大小的单个片段。

@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) { ... }
@include media-breakpoint-only(xxl) { ... }

例如,@include media breakpoint only(md){…}将导致:

@media (min-width: 768px) and (max-width: 991.98px) { ... }

breakpoints之间

类似地,媒体查询可能跨越多个breakpoints宽度:

@include media-breakpoint-between(md, xl) { ... }

Which results in:

// Example
// Apply styles starting from medium devices and up to extra large devices
@media (min-width: 768px) and (max-width: 1199.98px) { ... }