Bootstrap的container和container-fluid

电玩女神 2023-10-05 14:08 132阅读 0赞

Containers容器

容器是Bootstrap中最基本的布局元素,在使用我们的默认网格系统时,容器是必需的。容器用于在其中容纳,填充和(有时)使内容居中。尽管可以嵌套容器,但是大多数布局都不需要嵌套容器。

Bootstrap带有三个不同的容器:

  • .container,它max-width在每个响应断点处设置一个
  • .container-fluid,这是width: 100%所有断点
  • .container-{breakpoint}width: 100%直到指定的断点为止

下表说明了每个容器max-width与原始容器.container以及.container-fluid每个断点之间的比较。

在实际使用中查看它们,并在我们的Grid示例中进行比较。






























































  特小
<576px

≥576px

≥768px

≥992px
特大
≥1200px
.container 100% 540px 720px 960px 1140px
.container-sm 100% 540px 720px 960px 1140px
.container-md 100% 100% 720px 960px 1140px
.container-lg 100% 100% 100% 960px 1140px
.container-xl 100% 100% 100% 100% 1140px
.container-fluid 100% 100% 100% 100% 100%

All-in-one

我们的默认.container类是响应式,固定宽度的容器,这意味着它max-width在每个断点处都会更改。

复制

  1. <div class="container">
  2. <!-- Content here -->
  3. </div>

Fluid

使用.container-fluid了全宽的容器,跨越视口的整个宽度。

复制

  1. <div class="container-fluid">
  2. ...
  3. </div>

反应灵敏

响应容器是Bootstrap v4.4中的新增功能。它们允许您指定一个100%宽的类,直到达到指定的断点为止,此后,我们max-width对每个较高的断点应用。例如,.container-sm100%宽开始直到sm到达断点,在那里将扩大同mdlgxl

复制

  1. <div class="container-sm">100% wide until small breakpoint</div>
  2. <div class="container-md">100% wide until medium breakpoint</div>
  3. <div class="container-lg">100% wide until large breakpoint</div>
  4. <div class="container-xl">100% wide until extra large breakpoint</div>

响应断点

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

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

复制

  1. // Extra small devices (portrait phones, less than 576px)
  2. // No media query for `xs` since this is the default in Bootstrap
  3. // Small devices (landscape phones, 576px and up)
  4. @media (min-width: 576px) { ... }
  5. // Medium devices (tablets, 768px and up)
  6. @media (min-width: 768px) { ... }
  7. // Large devices (desktops, 992px and up)
  8. @media (min-width: 992px) { ... }
  9. // Extra large devices (large desktops, 1200px and up)
  10. @media (min-width: 1200px) { ... }

由于我们使用Sass编写源CSS,因此所有媒体查询都可以通过Sass mixins使用:

复制

  1. // No media query necessary for xs breakpoint as it's effectively `@media (min-width: 0) { ... }`
  2. @include media-breakpoint-up(sm) { ... }
  3. @include media-breakpoint-up(md) { ... }
  4. @include media-breakpoint-up(lg) { ... }
  5. @include media-breakpoint-up(xl) { ... }
  6. // Example: Hide starting at `min-width: 0`, and then show at the `sm` breakpoint
  7. .custom-class {
  8. display: none;
  9. }
  10. @include media-breakpoint-up(sm) {
  11. .custom-class {
  12. display: block;
  13. }
  14. }

有时我们会使用其他方向的媒体查询(给定的屏幕尺寸或更小):

复制

  1. // Extra small devices (portrait phones, less than 576px)
  2. @media (max-width: 575.98px) { ... }
  3. // Small devices (landscape phones, less than 768px)
  4. @media (max-width: 767.98px) { ... }
  5. // Medium devices (tablets, less than 992px)
  6. @media (max-width: 991.98px) { ... }
  7. // Large devices (desktops, less than 1200px)
  8. @media (max-width: 1199.98px) { ... }
  9. // Extra large devices (large desktops)
  10. // No media query since the extra-large breakpoint has no upper bound on its width

注意,由于浏览器目前不支持范围方面的查询,我们解决的局限性min-max-前缀使用值与这些比较高的精度视口,并用分数宽度(可下的高DPI设备一定的条件下发生,例如) 。

同样,这些媒体查询也可以通过Sass mixins使用:

复制

  1. @include media-breakpoint-down(xs) { ... }
  2. @include media-breakpoint-down(sm) { ... }
  3. @include media-breakpoint-down(md) { ... }
  4. @include media-breakpoint-down(lg) { ... }
  5. // No media query necessary for xl breakpoint as it has no upper bound on its width
  6. // Example: Style from medium breakpoint and down
  7. @include media-breakpoint-down(md) {
  8. .custom-class {
  9. display: block;
  10. }
  11. }

也有媒体查询和混合,用于使用最小和最大断点宽度来定位单个屏幕尺寸段。

复制

  1. // Extra small devices (portrait phones, less than 576px)
  2. @media (max-width: 575.98px) { ... }
  3. // Small devices (landscape phones, 576px and up)
  4. @media (min-width: 576px) and (max-width: 767.98px) { ... }
  5. // Medium devices (tablets, 768px and up)
  6. @media (min-width: 768px) and (max-width: 991.98px) { ... }
  7. // Large devices (desktops, 992px and up)
  8. @media (min-width: 992px) and (max-width: 1199.98px) { ... }
  9. // Extra large devices (large desktops, 1200px and up)
  10. @media (min-width: 1200px) { ... }

这些媒体查询也可以通过Sass mixins使用:

复制

  1. @include media-breakpoint-only(xs) { ... }
  2. @include media-breakpoint-only(sm) { ... }
  3. @include media-breakpoint-only(md) { ... }
  4. @include media-breakpoint-only(lg) { ... }
  5. @include media-breakpoint-only(xl) { ... }

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

复制

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

用于定位相同屏幕尺寸范围的Sass mixin为:

复制

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

Z指数

一些Bootstrap组件利用z-indexCSS属性,该CSS属性通过提供第三个轴来安排内容来帮助控制布局。我们在Bootstrap中使用默认的z-index比例尺,该比例尺旨在适当地分层导航,工具提示和弹出窗口,模态等。

这些较高的值以任意数字开始,该数字足够高且特定,足以理想地避免冲突。我们需要在我们的分层组件(工具提示,弹出窗口,导航栏,下拉菜单,模式)中使用一组标准的工具,以便我们在行为上可以保持一致。没有理由我们不能使用100+或500+。

我们不鼓励定制这些个人价值观;如果您要更改一个,则可能需要全部更改。

复制

  1. $zindex-dropdown: 1000 !default;
  2. $zindex-sticky: 1020 !default;
  3. $zindex-fixed: 1030 !default;
  4. $zindex-modal-backdrop: 1040 !default;
  5. $zindex-modal: 1050 !default;
  6. $zindex-popover: 1060 !default;
  7. $zindex-tooltip: 1070 !default;

为了处理重叠的组件(例如,按钮和输入组的输入)内的边界中,我们使用低单数位z-index的值123用于默认,悬停状态和活动状态。在悬停/焦点/活动上,我们将具有较高z-index值的特定元素置于最前列,以显示其在同级元素上的边界。

发表评论

表情:
评论列表 (有 0 条评论,132人围观)

还没有评论,来说两句吧...

相关阅读