--- title: justify-self slug: Web/CSS/justify-self tags: - CSS - CSS属性 - CSS盒对齐 translation_of: Web/CSS/justify-self ---
CSS justify-self 属性设置单个盒子在其布局容器适当轴中的对其方式。
此属性的效果取决于我们所处的布局模式:
/* 基础关键字 */ justify-self: auto; justify-self: normal; justify-self: stretch; /* 位置对齐 */ justify-self: center; /* 在中间放置元素 */ justify-self: start; /* 在开始处放置元素 */ justify-self: end; /* 在结束处放置元素 */ justify-self: flex-start; /* 与 'start' 等效。注意 justify-self 在 Flexbox 布局中被忽略。 */ justify-self: flex-end; /* 与 'end' 等效。注意 justify-self 在 Flexbox 布局中被忽略。 */ justify-self: self-start; justify-self: self-end; justify-self: left; /* 在左侧放置元素 */ justify-self: right; /* 在右侧放置元素 */ /* 基线对齐 */ justify-self: baseline; justify-self: first baseline; justify-self: last baseline; /* 溢出对齐 (只对位置对齐有效果)*/ justify-self: safe center; justify-self: unsafe center; /* 全局关键字 */ justify-self: inherit; justify-self: initial; justify-self: unset;
这个属性可以有三种不同的形式:
normal, auto,或者 stretch。baseline 关键字,加上可选的 first 或者 last。center, start, end, flex-start, flex-end, self-start, self-end, left,或者 right。safe 或者unsafe。autojustify-items 属性的值,除非它没有父级元素, 或者是绝对定位的,这些情况下,auto 代表了 normal。normalstart 的同义词。start 相似,在其他绝对定位的盒子中,其表现得与 stretch 相似。stretch 的行为,除了具有高宽比或固有大小的盒子,它的行为类似于 start 。startendflex-startstart 。flex-endend.self-startself-endcenterleftstart 。rightstart 。baseline
first baselinelast baselinefirst baseline is start, the one for last baseline is end.stretchauto-sized items have their size increased equally (not proportionally), while still respecting the constraints imposed by {{CSSxRef("max-height")}}/{{CSSxRef("max-width")}} (or equivalent functionality), so that the combined size exactly fills the alignment container.safestart.unsafe{{cssinfo}}
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;
}
{{EmbedLiveSample('Simple_demonstration', '100%', 200)}}
| Specification | Status | Comment |
|---|---|---|
| {{SpecName("CSS3 Box Alignment", "#propdef-justify-self", "justify-self")}} | {{Spec2("CSS3 Box Alignment")}} | Initial definition |
{{Compat("css.properties.justify-self.flex_context")}}
{{Compat("css.properties.justify-self.grid_context")}}