Skip to main content Skip to docs navigation

位置

使用这些简写工具可以快速配置元素的位置。

位置值

¥Position values

可以使用快速定位类,但它们没有响应。

¥Quick positioning classes are available, though they are not responsive.

<div class="position-static">...</div>
<div class="position-relative">...</div>
<div class="position-absolute">...</div>
<div class="position-fixed">...</div>
<div class="position-sticky">...</div>

排列元素

¥Arrange elements

使用边缘定位工具轻松排列元素。格式为 {property}-{position}

¥Arrange elements easily with the edge positioning utilities. The format is {property}-{position}.

其中属性是以下之一:

¥Where property is one of:

  • top - 对于垂直 top 位置

    ¥top - for the vertical top position

  • start - 对于水平 left 位置(LTR 中)

    ¥start - for the horizontal left position (in LTR)

  • bottom - 对于垂直 bottom 位置

    ¥bottom - for the vertical bottom position

  • end - 对于水平 right 位置(LTR 中)

    ¥end - for the horizontal right position (in LTR)

其中位置是以下之一:

¥Where position is one of:

  • 0 - 对于 0 边缘位置

    ¥0 - for 0 edge position

  • 50 - 对于 50% 边缘位置

    ¥50 - for 50% edge position

  • 100 - 对于 100% 边缘位置

    ¥100 - for 100% edge position

(你可以通过向 $position-values Sass 映射变量添加条目来添加更多位置值。)

¥(You can add more position values by adding entries to the $position-values Sass map variable.)

html
<div class="position-relative">
  <div class="position-absolute top-0 start-0"></div>
  <div class="position-absolute top-0 end-0"></div>
  <div class="position-absolute top-50 start-50"></div>
  <div class="position-absolute bottom-50 end-50"></div>
  <div class="position-absolute bottom-0 start-0"></div>
  <div class="position-absolute bottom-0 end-0"></div>
</div>

中心元素

¥Center elements

此外,你还可以使用变换工具类 .translate-middle 将元素居中。

¥In addition, you can also center the elements with the transform utility class .translate-middle.

此类将转换 translateX(-50%)translateY(-50%) 应用于元素,与边缘定位工具相结合,允许你将元素绝对居中。

¥This class applies the transformations translateX(-50%) and translateY(-50%) to the element which, in combination with the edge positioning utilities, allows you to absolute center an element.

html
<div class="position-relative">
  <div class="position-absolute top-0 start-0 translate-middle"></div>
  <div class="position-absolute top-0 start-50 translate-middle"></div>
  <div class="position-absolute top-0 start-100 translate-middle"></div>
  <div class="position-absolute top-50 start-0 translate-middle"></div>
  <div class="position-absolute top-50 start-50 translate-middle"></div>
  <div class="position-absolute top-50 start-100 translate-middle"></div>
  <div class="position-absolute top-100 start-0 translate-middle"></div>
  <div class="position-absolute top-100 start-50 translate-middle"></div>
  <div class="position-absolute top-100 start-100 translate-middle"></div>
</div>

通过添加 .translate-middle-x.translate-middle-y 类,元素只能在水平或垂直方向上定位。

¥By adding .translate-middle-x or .translate-middle-y classes, elements can be positioned only in horizontal or vertical direction.

html
<div class="position-relative">
  <div class="position-absolute top-0 start-0"></div>
  <div class="position-absolute top-0 start-50 translate-middle-x"></div>
  <div class="position-absolute top-0 end-0"></div>
  <div class="position-absolute top-50 start-0 translate-middle-y"></div>
  <div class="position-absolute top-50 start-50 translate-middle"></div>
  <div class="position-absolute top-50 end-0 translate-middle-y"></div>
  <div class="position-absolute bottom-0 start-0"></div>
  <div class="position-absolute bottom-0 start-50 translate-middle-x"></div>
  <div class="position-absolute bottom-0 end-0"></div>
</div>

示例

¥Examples

以下是这些类的一些现实生活示例:

¥Here are some real life examples of these classes:

Marker
html
<button type="button" class="btn btn-primary position-relative">
  Mails <span class="position-absolute top-0 start-100 translate-middle badge rounded-pill text-bg-secondary">+99 <span class="visually-hidden">unread messages</span></span>
</button>

<div class="position-relative py-2 px-4 text-bg-secondary border border-secondary rounded-pill">
  Marker <svg width="1em" height="1em" viewBox="0 0 16 16" class="position-absolute top-100 start-50 translate-middle mt-1" fill="var(--bs-secondary)" xmlns="http://www.w3.org/2000/svg"><path d="M7.247 11.14L2.451 5.658C1.885 5.013 2.345 4 3.204 4h9.592a1 1 0 0 1 .753 1.659l-4.796 5.48a1 1 0 0 1-1.506 0z"/></svg>
</div>

<button type="button" class="btn btn-primary position-relative">
  Alerts <span class="position-absolute top-0 start-100 translate-middle badge border border-light rounded-circle bg-danger p-2"><span class="visually-hidden">unread messages</span></span>
</button>

你可以将这些类与现有组件一起使用来创建新组件。请记住,你可以通过向 $position-values 变量添加条目来扩展其功能。

¥You can use these classes with existing components to create new ones. Remember that you can extend its functionality by adding entries to the $position-values variable.

html
<div class="position-relative m-4">
  <div class="progress" role="progressbar" aria-label="Progress" aria-valuenow="50" aria-valuemin="0" aria-valuemax="100" style="height: 1px;">
    <div class="progress-bar" style="width: 50%"></div>
  </div>
  <button type="button" class="position-absolute top-0 start-0 translate-middle btn btn-sm btn-primary rounded-pill" style="width: 2rem; height:2rem;">1</button>
  <button type="button" class="position-absolute top-0 start-50 translate-middle btn btn-sm btn-primary rounded-pill" style="width: 2rem; height:2rem;">2</button>
  <button type="button" class="position-absolute top-0 start-100 translate-middle btn btn-sm btn-secondary rounded-pill" style="width: 2rem; height:2rem;">3</button>
</div>

CSS

Sass 映射

¥Sass maps

默认位置工具值在 Sass 映射中声明,然后用于生成我们的工具。

¥Default position utility values are declared in a Sass map, then used to generate our utilities.

$position-values: (
  0: 0,
  50: 50%,
  100: 100%
);

Sass 工具 API

¥Sass utilities API

头寸工具在 scss/_utilities.scss 中的工具 API 中声明。Learn how to use the utilities API.

¥Position utilities are declared in our utilities API in scss/_utilities.scss. Learn how to use the utilities API.

"position": (
  property: position,
  values: static relative absolute fixed sticky
),
"top": (
  property: top,
  values: $position-values
),
"bottom": (
  property: bottom,
  values: $position-values
),
"start": (
  property: left,
  class: start,
  values: $position-values
),
"end": (
  property: right,
  class: end,
  values: $position-values
),
"translate-middle": (
  property: transform,
  class: translate-middle,
  values: (
    null: translate(-50%, -50%),
    x: translateX(-50%),
    y: translateY(-50%),
  )
),