justify-items - CSS(层叠样式表) 编辑
CSS 的 justify-items
属性为所有盒中的项目定义了默认的 justify-self
, 可以使这些项目以默认方式沿适当轴线对齐到每个盒子。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
该属性的作用效果取决于我们使用的布局模式:
- 在块级布局中,会将其包含的项目在其行内轴上对齐;
- 绝对定位的元素中,会将其包含的项目在其行内轴上对齐,同时考虑 top、left、bottom、right 的值;
- 表格单元中,该属性被忽略(块级元素、绝对定位元素和表格布局中对齐的更多信息);
- 弹性盒子布局中,该属性被忽略(弹性盒子中对齐的更多信息);
- 栅格布局中,会将其栅格区域内的项目在其行内轴上对齐(栅格布局中对齐的更多信息);
语法规则
/* Basic keywords */
justify-items: auto;
justify-items: normal;
justify-items: stretch;
/* Positional alignment */
justify-items: center; /* Pack items around the center */
justify-items: start; /* Pack items from the start */
justify-items: end; /* Pack items from the end */
justify-items: flex-start; /* Pack flex items from the start */
justify-items: flex-end; /* Pack flex items from the end */
justify-items: self-start;
justify-items: self-end;
justify-items: left; /* Pack items from the left */
justify-items: right; /* Pack items from the right */
/* Baseline alignment */
justify-items: baseline;
justify-items: first baseline;
justify-items: last baseline;
/* Overflow alignment (for positional alignment only) */
justify-items: safe center;
justify-items: unsafe center;
/* Legacy alignment */
justify-items: legacy right;
justify-items: legacy left;
justify-items: legacy center;
/* Global values */
justify-items: inherit;
justify-items: initial;
justify-items: unset;
此属性可以采用四种不同的形式之一:
- 关键词: 关键字
normal
,auto
,或stretch
任选其一 - 基线对齐:关键词
baseline
, 可选first
或last
之一为前缀 - 位置对其:关键词
center
,start
,end
,flex-start
,flex-end
,self-start
,self-end
,left
或right
任选其一,可选safe
或unsafe
之一为前缀 - Legacy alignment: the
legacy
keyword, followed by one ofleft
orright
.
值
auto
- 使用的值是父元素的justify-items属性的值,除非该方框没有父元素,或者是绝对定位的,在这些情况下,
auto
代表normal
。 normal
- 这个关键字的效果取决于我们所处的布局模式:
- 在块级布局中,相当于是
start
。 - In absolutely-positioned layouts, the keyword behaved like
start
on replaced absolutely-positioned boxes, and asstretch
on all other absolutely-positioned boxes. - In table cell layouts, this keyword has no meaning as this property is ignored.
- In flexbox layouts, this keyword has no meaning as this property is ignored.
- In grid layouts, this keyword leads to a behavior similar to the one of
stretch
, except for boxes with an aspect ratio or an intrinsic sizes where it behaves likestart
.
- 在块级布局中,相当于是
start
- The item is packed flush to each other toward the start edge of the alignment container in the appropriate axis.
end
- The item is packed flush to each other toward the end edge of the alignment container in the appropriate axis.
flex-start
- For items that are not children of a flex container, this value is treated like
start
. flex-end
- For items that are not children of a flex container, this value is treated like
end
. self-start
- The item is packed flush to the edge of the alignment container of the start side of the item, in the appropriate axis.
self-end
- The item is packed flush to the edge of the alignment container of the end side of the item, in the appropriate axis.
center
- The items are packed flush to each other toward the center of the of the alignment container.
left
- The items are packed flush to each other toward the left edge of the alignment container. If the property’s axis is not parallel with the inline axis, this value behaves like
start
. right
- The items are packed flush to each other toward the right edge of the alignment container in the appropriate axis. If the property’s axis is not parallel with the inline axis, this value behaves like
start
. baseline
first baselinelast baseline
- Specifies participation in first- or last-baseline alignment: aligns the alignment baseline of the box’s first or last baseline set with the corresponding baseline in the shared first or last baseline set of all the boxes in its baseline-sharing group.
The fallback alignment forfirst baseline
isstart
, the one forlast baseline
isend
. stretch
- If the combined size of the items is less than the size of the alignment container, any
auto
-sized items have their size increased equally (not proportionally), while still respecting the constraints imposed bymax-height
/max-width
(or equivalent functionality), so that the combined size exactly fills the alignment container. safe
- If the size of the item overflows the alignment container, the item is instead aligned as if the alignment mode were
start
. unsafe
- Regardless of the relative sizes of the item and alignment container, the given alignment value is honored.
legacy
- Makes the value inherited by the box descendants. Note that if a descendant has a
justify-self: auto
value, thelegacy
keyword is not considered by the descend, only theleft
,right
, orcenter
value associated to it.
Formal syntax
normal | stretch | <baseline-position> | <overflow-position>? [ <self-position> | left | right ] | legacy | legacy && [ left | right | center ]where
<baseline-position> = [ first | last ]? baseline
<overflow-position> = unsafe | safe
<self-position> = center | start | end | self-start | self-end | flex-start | flex-end
规范
Specification | Status | Comment |
---|---|---|
CSS Box Alignment Module Level 3 justify-items | Working Draft | Initial definition |
初始值 | legacy |
---|---|
适用元素 | all elements |
是否是继承属性 | 否 |
计算值 | as specified |
Animation type | discrete |
浏览器兼容性
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.Support in Flex layout
BCD tables only load in the browser
Support in Grid layout
BCD tables only load in the browser
另见
- CSS Grid Guide: Box alignment in CSS Grid layouts
- CSS Box Alignment
- The
place-items
shorthand property - The
justify-self
property - The
align-items
property
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论