2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
id: 587d78ac367417b2b2512af6
|
2020-12-16 07:37:30 +00:00
|
|
|
|
title: 使用 justify-content 属性对齐元素
|
2018-10-10 22:03:03 +00:00
|
|
|
|
challengeType: 0
|
2020-02-11 13:32:25 +00:00
|
|
|
|
videoUrl: 'https://scrimba.com/p/pVaDAv/c43gnHm'
|
|
|
|
|
forumTopicId: 301102
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: align-elements-using-the-justify-content-property
|
2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --description--
|
2020-02-11 13:32:25 +00:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
flex 子元素有时不能充满整个 flex 容器, 所以我们经常需要告诉 CSS 以什么方式排列 flex 子元素,以及调整它们的间距。 幸运的是,我们可以通过 `justify-content` 属性的不同值来实现。 在介绍属性的可选值之前,我们要先理解一些重要术语。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
[这张图片的元素横着排列,很好地描述了下面的概念。](https://www.w3.org/TR/css-flexbox-1/images/flex-direction-terms.svg)
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
回忆一下,如果把 flex 容器设为一个行,它的子元素会从左到右逐个排列; 如果把 flex 容器设为一个列,它的子元素会从上到下逐个排列。 子元素排列的方向被称为 **main axis(主轴)**。 对于行,主轴水平贯穿每一个项目; 对于列,主轴垂直贯穿每一个项目。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-04 17:49:46 +00:00
|
|
|
|
对于如何沿主轴线排放 flex 项目,有几种选择。 很常用的一种是 `justify-content: center;`:即 flex 子元素在 flex 容器中居中排列。 其他选择包括:
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
<ul><li><code>flex-start</code>:从 flex 容器的起始位置开始排列项目。 对行来说是把项目移至左边, 对于列是把项目移至顶部。 如未设置 <code>justify-content</code> 的值,那么这就是默认值。</li><li><code>flex-end</code>:从 flex 容器的终止位置开始排列项目。 对行来说是把项目移至右边, 对于列是把项目移至底部。</li><li><code>space-between</code>:项目间保留一定间距地沿主轴居中排列。 第一个和最后一个项目被放置在容器边沿。 例如,在行中第一个项目会紧贴着容器左边,最后一个项目会紧贴着容器右边,然后其他项目均匀排布。</li><li><code>space-around</code>:与<code>space-between</code>相似,但头尾两个项目不会紧贴容器边缘,所有项目之间的空间均匀排布。</li><li><code>space-evenly</code>:头尾两个项目不会紧贴容器边缘,所有项目之间的空间均匀排布。</li></ul>
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
这个例子可以帮助你理解这个属性。 请为 `#box-container` 元素添加 CSS 属性 `justify-content`,并将其属性值设置为 `center`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
**提示:**
|
2021-02-06 04:42:36 +00:00
|
|
|
|
在编辑器里试试 `justify-content` 的其他可用值,看看它们之间的区别。 但要通过挑战,你必须把值设为 `center`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
`#box-container` 所选择的元素应有 `justify-content` 属性,且其属性值应为 `center`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert($('#box-container').css('justify-content') == 'center');
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
<style>
|
|
|
|
|
#box-container {
|
|
|
|
|
background: gray;
|
|
|
|
|
display: flex;
|
|
|
|
|
height: 500px;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
#box-1 {
|
|
|
|
|
background-color: dodgerblue;
|
|
|
|
|
width: 25%;
|
|
|
|
|
height: 100%;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#box-2 {
|
|
|
|
|
background-color: orangered;
|
|
|
|
|
width: 25%;
|
|
|
|
|
height: 100%;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
|
|
|
|
|
<div id="box-container">
|
|
|
|
|
<div id="box-1"></div>
|
|
|
|
|
<div id="box-2"></div>
|
|
|
|
|
</div>
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --solutions--
|
2020-02-11 13:32:25 +00:00
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
```html
|
|
|
|
|
<style>
|
|
|
|
|
#box-container {
|
|
|
|
|
background: gray;
|
|
|
|
|
display: flex;
|
|
|
|
|
height: 500px;
|
|
|
|
|
justify-content: center;
|
|
|
|
|
}
|
|
|
|
|
#box-1 {
|
|
|
|
|
background-color: dodgerblue;
|
|
|
|
|
width: 25%;
|
|
|
|
|
height: 100%;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#box-2 {
|
|
|
|
|
background-color: orangered;
|
|
|
|
|
width: 25%;
|
|
|
|
|
height: 100%;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
|
|
|
|
|
<div id="box-container">
|
|
|
|
|
<div id="box-1"></div>
|
|
|
|
|
<div id="box-2"></div>
|
|
|
|
|
</div>
|
|
|
|
|
```
|