--- title: justify-content slug: Web/CSS/justify-content tags: - CSS - CSS 属性 - CSS 弹性框 - Reference translation_of: Web/CSS/justify-content ---
CSS justify-content 属性定义了浏览器之间,如何分配顺着弹性容器主轴(或者网格行轴) 的元素之间及其周围的空间。
/* Positional alignment */ justify-content: center;/* 居中排列 */justify-content: start; /* Pack items from the start */ justify-content: end; /* Pack items from the end */ justify-content: flex-start;/* 从行首起始位置开始排列 */justify-content: flex-end;/* 从行尾位置开始排列 */justify-content: left; /* Pack items from the left */ justify-content: right; /* Pack items from the right *//* Baseline alignment */ justify-content: baseline; justify-content: first baseline; justify-content: last baseline; /* Distributed alignment */ justify-content: space-between; /* 均匀排列每个元素 首个元素放置于起点,末尾元素放置于终点 */ justify-content: space-around; /* 均匀排列每个元素 每个元素周围分配相同的空间 */ justify-content: space-evenly; /* 均匀排列每个元素 每个元素之间的间隔相等 */ justify-content: stretch; /* 均匀排列每个元素 'auto'-sized 的元素会被拉伸以适应容器的大小 */ /* Overflow alignment */ justify-content: safe center; justify-content: unsafe center; /* Global values */ justify-content: inherit; justify-content: initial; justify-content: unset;
当 length 属性和自动外边距属性(margin: auto)生效之后,对齐已经完成了。也就是说,如果存在至少一个弹性元素,而且这个元素的 {{cssxref("flex-grow")}} 属性不等于 0,那么对齐方式不会生效,就像没有多余空间的情况。
可以参考 使用 CSS 弹性框获取更多信息。
startflex-startflex-endcenterleftleft的行为类似于start。rightright的行为类似于end。baseline
first baselinelast baselinefirst baseline is start, the one for last baseline is end.space-betweenspace-aroundspace-evenlystretchauto-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 along the main axis.safestart 代替它。unsafe#container {
display: flex;
justify-content: space-between; /* Can be changed in the live sample */
}
#container > div {
width: 100px;
height: 100px;
background: linear-gradient(-45deg, #788cff, #b4c8ff);
}
<div id="container"> <div></div> <div></div> <div></div> </div> <select id="justifyContent"> <option value="start">start</option> <option value="end">end</option> <option value="flex-start">flex-start</option> <option value="flex-end">flex-end</option> <option value="center">center</option> <option value="left">left</option> <option value="right">right</option> <option value="baseline">baseline</option> <option value="first baseline">first baseline</option> <option value="last baseline">last baseline</option> <option value="space-between" selected>space-between</option> <option value="space-around">space-around</option> <option value="space-evenly">space-evenly</option> <option value="stretch">stretch</option> </select>
var justifyContent = document.getElementById("justifyContent");
justifyContent.addEventListener("change", function (evt) {
document.getElementById("container").style.justifyContent =
evt.target.value;
});
{{EmbedLiveSample("Example", "100%", 140)}}
| Specification | Status | Comment |
|---|---|---|
| {{SpecName('CSS3 Box Alignment', '#propdef-justify-content', 'justify-content')}} | {{Spec2('CSS3 Box Alignment')}} | Adds the [ first | last ]? baseline, self-start, self-end, start, end, left, right, unsafe | safe values. |
| {{SpecName('CSS3 Flexbox', '#propdef-justify-content', 'justify-content')}} | {{Spec2('CSS3 Flexbox')}} | Initial definition |