CSS
justify-self
property sets the way a box is justified inside its alignment container along the appropriate axis.
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.
The effect of this property is dependent of the layout mode we are in:
/* Basic keywords */ justify-self: auto; justify-self: normal; justify-self: stretch; /* Positional alignment */ justify-self: center; /* Pack item around the center */ justify-self: start; /* Pack item from the start */ justify-self: end; /* Pack item from the end */ justify-self: flex-start; /* Equivalent to 'start'. Note that justify-self is ignored in Flexbox layouts. */ justify-self: flex-end; /* Equivalent to 'end'. Note that justify-self is ignored in Flexbox layouts. */ justify-self: self-start; justify-self: self-end; justify-self: left; /* Pack item from the left */ justify-self: right; /* Pack item from the right */ /* Baseline alignment */ justify-self: baseline; justify-self: first baseline; justify-self: last baseline; /* Overflow alignment (for positional alignment only) */ justify-self: safe center; justify-self: unsafe center; /* Global values */ justify-self: inherit; justify-self: initial; justify-self: unset;
This property can take one of three different forms:
normal
,
auto
,或
stretch
.
baseline
keyword, plus optionally one of
first
or
last
.
center
,
start
,
end
,
flex-start
,
flex-end
,
self-start
,
self-end
,
left
,或
right
.
safe
or
unsafe
.
auto
justify-items
property of the parents box, unless the box has no parent, or is absolutely positioned, in these cases,
auto
represents
normal
.
normal
start
.
start
on
replaced
absolutely-positioned boxes, and as
stretch
on
all other
absolutely-positioned boxes.
stretch
, except for boxes with an aspect ratio or an intrinsic sizes where it behaves like
start
.
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
start
.
flex-end
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
start
.
right
start
.
baseline
first baseline
last baseline
first baseline
is
start
, the one for
last baseline
is
end
.
stretch
auto
-sized items have their size increased equally (not proportionally), while still respecting the constraints imposed by
max-height
/
max-width
(or equivalent functionality), so that the combined size exactly fills the alignment container.
safe
start
.
unsafe
Regardless of the relative sizes of the item and alignment container, the given alignment value is honored.
| 初始值 |
auto
|
|---|---|
| 适用于 | block-level boxes, absolutely-positioned boxes, and grid items |
| 继承 | no |
| 计算值 | 如指定 |
| 动画类型 | discrete |
auto | normal | stretch | <baseline-position> | <overflow-position>? [ <self-position> | left | right ]where
<baseline-position> = [ first | last ]? baseline
<overflow-position> = unsafe | safe
<self-position> = center | start | end | self-start | self-end | flex-start | flex-end
In the following example we have a simple 2 x 2 grid layout. Initially the grid container is given a
justify-items
value of
stretch
— the default — which causes the grid items to stretch across the entire width of their cells.
The second, third, and fourth grid items are then given different values of
justify-self
, to show how these override the
justify-items
value. These values cause the grid items to span only as wide as their content width, and align in different positions across their cells.
<article class="container"> <span>First child</span> <span>Second child</span> <span>Third child</span> <span>Fourth child</span> </article>
html {
font-family: helvetica, arial, sans-serif;
letter-spacing: 1px;
}
article {
background-color: red;
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-rows: 40px;
grid-gap: 10px;
width: 300px;
justify-items: stretch;
}
span:nth-child(2) {
justify-self: start;
}
span:nth-child(3) {
justify-self: center;
}
span:nth-child(4) {
justify-self: end;
}
article span {
background-color: black;
color: white;
margin: 1px;
text-align: center;
}
article, span {
padding: 10px;
border-radius: 7px;
}
article {
margin: 20px;
}
| 规范 | 状态 | 注释 |
|---|---|---|
|
CSS Box Alignment Module Level 3
The definition of 'justify-self' in that specification. |
工作草案 | 初始定义 |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Supported in Flex Layout | Chrome 完整支持 57 | Edge 完整支持 16 | Firefox 完整支持 45 | IE 不支持 No | Opera 完整支持 44 | Safari 完整支持 10.1 | WebView Android 完整支持 57 | Chrome Android 完整支持 57 | Firefox Android 完整支持 45 | Opera Android 完整支持 43 | Safari iOS 完整支持 10.3 | Samsung Internet Android 完整支持 6.0 |
完整支持
不支持
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Supported in Grid Layout | Chrome 完整支持 57 | Edge 完整支持 16 | Firefox 完整支持 45 |
IE
部分支持
10
Prefixed
注意事项
|
Opera 完整支持 44 | Safari 完整支持 10.1 | WebView Android 完整支持 57 | Chrome Android 完整支持 57 | Firefox Android 完整支持 45 | Opera Android 完整支持 43 | Safari iOS 完整支持 10.3 | Samsung Internet Android 完整支持 6.0 |
完整支持
部分支持
见实现注意事项。
要求使用供应商前缀或不同名称。
justify-items
property