断点
断点-Bootstrap 5和Material Design 2.0
断点是Bootstrap中的触发器,用于触发布局如何响应设备上的更改 或视口大小。
核心概念
-
突破点是响应式设计的基础。 使用它们来控制何时可以在特定视口或设备上调整布局 尺寸。
-
使用媒体查询按断点构建CSS。 媒体查询是CSS的一项功能,可让您根据条件有条件地应用样式 浏览器和操作系统参数集。我们最常用的
min-width
在我们的媒体查询中。 -
移动优先,响应式设计是目标。 Bootstrap的CSS旨在 应用最少的样式以使布局在最小的断点处工作,并且 然后在样式上分层以针对较大的设备调整该设计。这样可以优化您的CSS, 改善了渲染时间,并为访问者提供了绝佳的体验。
可用断点
Bootstrap包括六个默认断点,有时称为网格层,用于构建 反应迅速。如果您使用我们的源Sass文件,则可以自定义这些断点。
Breakpoint | Class infix | Dimensions |
---|---|---|
X-Small | None | 0–576px |
Small | sm |
≥576px |
Medium | md |
≥768px |
Large | lg |
≥992px |
Extra large | xl |
≥1200px |
Extra extra large | xxl |
≥1400px |
每个断点的大小选择为12的倍数,并代表 常见的设备尺寸和视口尺寸。他们没有专门针对每个用例或 设备,但提供范围为任何产品提供了坚实而一致的基础 几乎是设备。
这些断点可通过Sass进行自定义-您可以在我们的Sass地图中找到它们
_variables.scss
样式表。
$grid-breakpoints: ( xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px, xxl:
1400px );
媒体查询
由于Bootstrap首先开发为移动设备,因此我们使用了一些 媒体查询 为我们的布局和界面创建合理的断点。这些断点主要是 基于最小视口宽度,并允许我们随着视口的变化按比例放大元素。
最小宽度
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 mixins使用我们在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) { ... }
最大宽度
有时我们会使用其他方向的媒体查询(给定的屏幕尺寸 或更小):
// 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,并将其用作我们的 最大宽度值。例如:
// 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? 浏览器当前不支持
范围上下文查询s,所以我们解决了
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) { ... }