Bootstrap5 断点

Bootstrap 利用断点(Breakpoints)来确定响应式布局在设备或视口(viewport)大小变化时的行为,该宽度可自定义。

核心概念 

  • 断点是响应式设计的构建基块。 使用断点来控制何时可以在特定视口或设备大小上调整布局。
  • 使用媒体查询按断点构建 CSS。 媒体查询是 CSS 的一项功能,允许您根据一组浏览器和操作系统参数有条件地应用样式。我们最常在媒体查询中使用min-width
  • 移动优先,响应式设计是目标。 Bootstrap的CSS旨在应用最少的样式,使布局在最小的断点处工作,然后在样式上分层以针对更大的设备调整该设计。这可以优化您的 CSS,缩短渲染时间,并为您的访问者提供出色的体验。

可用断点 

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

BreakpointClass infixDimensions
Extra smallNone<576px
Smallsm≥576px
Mediummd≥768px
Largelg≥992px
Extra largexl≥1200px
Extra extra largexxl≥1400px

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

这些断点可以通过 Sass 进行自定义 - 您可以在我们的_variables.scss样式表的 Sass 映射中找到它们。

// scss/_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 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) { ... }

最大宽度(Max-width) 

我们偶尔会使用另一个方向的媒体查询(给定的屏幕尺寸_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;
  }
}

这些 mixins 采用那些声明的断点,从中减去.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? 浏览器目前不支持 范围上下文查询,因此我们通过使用更高精度的值来解决 ‘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) { ... }

Captcha Code

0 笔记

分享笔记

Inline Feedbacks
View all notes