1. Flexbox 和 Grid
  2. align-self

Flexbox 和 Grid

align-self

Utilities for controlling how an individual flex or grid item is positioned along its container's cross axis.

样式
self-auto
align-self: auto;
self-start
align-self: flex-start;
self-end
align-self: flex-end;
self-end-safe
align-self: safe flex-end;
self-center
align-self: center;
self-center-safe
align-self: safe center;
self-stretch
align-self: stretch;
self-baseline
align-self: baseline;
self-baseline
align-self: last baseline;

示例(Examples)

¥Examples

自动(Auto)

¥Auto

使用 self-auto 工具根据容器的 align-items 属性值对齐项目:

¥Use the self-auto utility to align an item based on the value of the container's align-items property:

01
02
03
<div class="flex items-stretch ...">
<div>01</div>
<div class="self-auto ...">02</div>
<div>03</div>
</div>

开始(Start)

¥Start

使用 self-start 工具将项目与容器横轴的起始端对齐,无论容器的 align-items 值如何:

¥Use the self-start utility to align an item to the start of the container's cross axis, despite the container's align-items value:

01
02
03
<div class="flex items-stretch ...">
<div>01</div>
<div class="self-start ...">02</div>
<div>03</div>
</div>

中心(Center)

¥Center

使用 self-center 工具将网格项与容器横轴的中心对齐,无论容器的 align-items 值如何:

¥Use the self-center utility to align an item along the center of the container's cross axis, despite the container's align-items value:

01
02
03
<div class="flex items-stretch ...">
<div>01</div>
<div class="self-center ...">02</div>
<div>03</div>
</div>

结尾(End)

¥End

使用 self-end 工具将项目与容器横轴的末端对齐,无论容器的 align-items 值如何:

¥Use the self-end utility to align an item to the end of the container's cross axis, despite the container's align-items value:

01
02
03
<div class="flex items-stretch ...">
<div>01</div>
<div class="self-end ...">02</div>
<div>03</div>
</div>

拉紧(Stretch)

¥Stretch

使用 self-stretch 工具拉伸网格项以填充容器的横轴,无论容器的 align-items 值如何:

¥Use the self-stretch utility to stretch an item to fill the container's cross axis, despite the container's align-items value:

01
02
03
<div class="flex items-stretch ...">
<div>01</div>
<div class="self-stretch ...">02</div>
<div>03</div>
</div>

基线(Baseline)

¥Baseline

使用 self-baseline 工具将项目对齐,使其基线与弹性容器横轴的基线对齐:

¥Use the self-baseline utility to align an item such that its baseline aligns with the baseline of the flex container's cross axis:

01
02
03
<div class="flex ...">
<div class="self-baseline pt-2 pb-6">01</div>
<div class="self-baseline pt-8 pb-12">02</div>
<div class="self-baseline pt-12 pb-4">03</div>
</div>

最后基线(Last baseline)

¥Last baseline

使用使用 self-baseline-last 工具将项目沿容器的横轴对齐,使其基线与容器中的最后一个基线对齐:

¥Use the self-baseline-last utility to align an item along the container's cross axis such that its baseline aligns with the last baseline in the container:

Spencer Sharp

Working on the future of astronaut recruitment at Space Recruit.

spacerecruit.com
Alex Reed

A multidisciplinary designer.

alex-reed.com
<div class="grid grid-cols-[1fr_auto]">
<div>
<img src="img/spencer-sharp.jpg" />
<h4>Spencer Sharp</h4>
<p class="self-baseline-last">Working on the future of astronaut recruitment at Space Recruit.</p>
</div>
<p class="self-baseline-last">spacerecruit.com</p>
</div>

这对于确保文本项彼此对齐非常有用,即使它们的高度不同。

¥This is useful for ensuring that text items align with each other, even if they have different heights.

响应式设计(Responsive design)

¥Responsive design

align-self 工具前面使用断点变体如 md: 仅在 medium 屏幕尺寸及以上时应用工具:

<div class="self-auto md:self-end ...">
<!-- ... -->
</div>

请参阅 变体文档 详细了解如何使用变体。

TailwindCSS v4.1 中文网 - 粤ICP备13048890号