Bootstrap 利用断点(Breakpoints)来确定响应式布局在设备或视口(viewport)大小变化时的行为,该宽度可自定义。
核心概念
-
断点(Breakpoints)时响应式设计的组成部分。 通过断点可以控制何时可以在特定视口(viewport)或设备尺寸下调整布局。
-
Use media queries to architect your CSS by breakpoint. Media queries are a feature of CSS that allow you to conditionally apply styles based on a set of browser and operating system parameters. We most commonly use
min-width
in our media queries. -
移动设备优先、响应式设计是目标。Mobile first, responsive design is the goal. Bootstrap’s CSS aims to apply the bare minimum of styles to make a layout work at the smallest breakpoint, and then layers on styles to adjust that design for larger devices. This optimizes your CSS, improves rendering time, and provides a great experience for your visitors.
Available breakpoints
Bootstrap 内置了 6 个默认断点,有时也被叫做 grid tiers, for building responsively. 如果你使用的是 Bootstrap 的 Sass 源文件,则可以自定义这些断点。
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 |
Each breakpoint was chosen to comfortably hold containers whose widths are multiples of 12. Breakpoints are also representative of a subset of common device sizes and viewport dimensions—they don’t specifically target every use case or device. Instead, the ranges provide a strong and consistent foundation to build on for nearly any device.
这些断点可以通过 Sass 变量来自定义(你可以在 _variables.scss
文件中找到它们)。
$grid-breakpoints: (
xs: 0,
sm: 576px,
md: 768px,
lg: 992px,
xl: 1200px,
xxl: 1400px
);
有关如何修改 Sass 变量的信息和示例,请参阅 栅格文档中关于 Sass 的部分。
媒体查询
由于 Bootstrap 是移动设备优先的,因此我们使用了一些 媒体查询(media queries) 功能来为我们的布局和视觉元素创建合理的断点。这些断点主要以最小的视口(viewport)宽度作为基准,并在视口尺寸改变时放大元素。
最小宽度
在 Bootstrap 的 Sass 源码中主要使用以下媒体查询范围(或者断点)来实现布局】栅格系统和组件的。 media query ranges—or breakpoints—in our source Sass files for our layout, grid system, and components.
// 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;
}
}
These Sass mixins translate in our compiled CSS using the values declared in our Sass variables. For example:
// 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
We occasionally use media queries that go in the other direction (the given screen size or smaller):
// 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;
}
}
These mixins take those declared breakpoints, subtract .02px
from them, and use them as our max-width
values. For example:
// `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) { ... }
min-
and max-
prefixes and viewports with fractional widths (which can occur under certain conditions on high-dpi devices, for instance) by using values with higher precision.
Single breakpoint
There are also media queries and mixins for targeting a single segment of screen sizes using the minimum and maximum breakpoint widths.
@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) { ... }
For example the @include media-breakpoint-only(md) { ... }
will result in :
@media (min-width: 768px) and (max-width: 991.98px) { ... }
Between breakpoints
Similarly, media queries may span multiple breakpoint widths:
@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) { ... }