堆叠

基于我们的 Flex 工具类构建的简写辅助类,让组件布局比以往更快、更简单。

Container queries

堆叠提供了一种快捷方式,可应用多个 Flex 布局属性,从而在 Bootstrap 中快速轻松地创建布局。该概念和实现的全部功劳归属于开源 Pylon 项目。

垂直

使用 .vstack 创建垂直布局。堆叠项默认占满整个宽度。使用 .gap-* 工具类在项之间添加间距。

First item
Second item
Third item
HTML
<div class="vstack gap-3">
  <div class="p-2">First item</div>
  <div class="p-2">Second item</div>
  <div class="p-2">Third item</div>
</div>

水平

使用 .hstack 实现水平布局。堆叠项默认垂直居中,且仅占据必要的宽度。使用 .gap-* 工具类在项之间添加间距。

First item
Second item
Third item
HTML
<div class="hstack gap-3">
  <div class="p-2">First item</div>
  <div class="p-2">Second item</div>
  <div class="p-2">Third item</div>
</div>

使用 .ms-auto 之类的水平外边距工具类作为间隔:

First item
Second item
Third item
HTML
<div class="hstack gap-3">
  <div class="p-2">First item</div>
  <div class="p-2 ms-auto">Second item</div>
  <div class="p-2">Third item</div>
</div>

以及配合垂直线:

First item
Second item
Third item
HTML
<div class="hstack gap-3">
  <div class="p-2">First item</div>
  <div class="p-2 ms-auto">Second item</div>
  <div class="vr"></div>
  <div class="p-2">Third item</div>
</div>

响应式

考虑这个示例:在较窄的移动设备上堆叠按钮,而在较大的设备上让它们水平排列。

HTML
<div class="vstack gap-2 md:col-5 mx-auto">
  <button type="button" class="btn-solid theme-primary">Save changes</button>
  <button type="button" class="btn-outline theme-secondary">Cancel</button>
</div>

以下是使用响应式堆叠的实现方式,它基于容器的媒体查询。将堆叠包裹在一个额外元素中,或为现有父元素添加 .stack-container,以便为堆叠提供一个可自适应其布局的“容器”。

HTML
<div class="stack-container">
  <div class="vstack gap-2 sm:hstack">
    <button type="button" class="btn-solid theme-primary">Save changes</button>
    <button type="button" class="btn-outline theme-secondary">Cancel</button>
  </div>
</div>

CSS

.stack-container {
  @include set-container();
}

[class*="hstack"],
[class*="vstack"] {
  display: flex;
  flex: var(--stack-flex, 1 1 auto);
  flex-direction: var(--stack-direction, row);
  align-items: var(--stack-align-items, center);
  align-self: var(--stack-align-self, stretch);
}

@include loop-breakpoints-up() using ($breakpoint, $prefix) {
  .#{$prefix}vstack {
    @include container-breakpoint-up($breakpoint) {
      --stack-direction: column;
      --stack-align-items: stretch;
    }
  }
  .#{$prefix}hstack {
    @include container-breakpoint-up($breakpoint) {
      --stack-direction: row;
      --stack-align-items: flex-start;
    }
  }
}