在GitHub上查看

响应式的断点

断点是你在Bootstrap中布局如何响应跨设备或屏幕大小变化的触发器。

核心概念

  • 断点是响应式设计的构建块。使用它们来控制何时可以在特定的屏幕或设备大小调整布局。

  • 使用媒体查询按断点构建CSS。 媒体查询是CSS的一项功能,可让您根据一组浏览器和操作系统参数有条件地应用样式。 我们最常在媒体查询中使用 min-width

  • 移动优先,响应式设计是我们的目标。 Bootstrap的CSS旨在使用最少的样式来使布局在最小的断点处工作,然后在样式上分层以针对较大的设备调整该设计。 这样可以优化CSS,缩短渲染时间,并为用户提供出色的体验。

可用的断点

Bootstrap包括六个默认断点(有时称为 网格层),用于进行响应式构建。 如果你使用我们的源Sass文件,则可以自定义这些断点。

断点 规格 屏幕尺寸大小
超小(X-Small) <576px
小(Small) sm ≥576px
中(Medium) md ≥768px
大(Large) lg ≥992px
超大(Extra large) xl ≥1200px
超大型(Extra extra large) xxl ≥1400px

每个断点都被选择来容纳宽度为12倍的容器。断点也是常见设备大小和视区尺寸子集的代表,它们不是针对每个用例或设备的。相反,这些范围为几乎任何设备提供了坚固和一致的基础。

这些断点可以通过Sass自定义,你可以在我们的Sass map中找到它们 _variables.scss 样式表。

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

有关如何修改Sass样式表和变量的更多信息和示例,请参阅 网格文档的Sass 部分。

媒体查询

由于Bootstrap最初是为移动设备而开发的,因此我们使用了一些 媒体查询来为布局和界面创建合理的断点。 这些断点主要基于最小屏幕宽度,并允许我们随着视口的变化按比例放大元素。

最小宽度 (Min-width)

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

// 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使用Sass变量中声明的值在我们编译的CSS中进行转换。例如:

// 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获取那些声明的断点,从中减去 .02px ,并将它们用作我们的最大宽度(Max-width)。例如:

// X-Small devices (portrait phones, less than 576px)
@media (max-width: 575.98px) { ... }

// Small devices (landscape phones, less than 768px)
@media (max-width: 767.98px) { ... }

// Medium devices (tablets, less than 992px)
@media (max-width: 991.98px) { ... }

// Large devices (desktops, less than 1200px)
@media (max-width: 1199.98px) { ... }

// X-Large devices (large desktops, less than 1400px)
@media (max-width: 1399.98px) { ... }

// XX-Large devices (larger desktops)
// No media query since the xxl breakpoint has no upper bound on its width
为什么要减去.02px ? 浏览器目前不支持 范围上下文查询,因此我们使用精度更高的值来解决 min-max- 前缀 以及视口宽度的限制(例如在某些条件下在高dpi的设备上可能会发生这种情况)。

单个断点

还有媒体查询和混合,用于最小和最大断点宽度来定位屏幕大小的单一段。

@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) { ... }

断点之间

同样,媒体查询可能跨越多个断点宽度:

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

结果是:

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