2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d78ae367417b2b2512afe
|
2020-12-16 00:37:30 -07:00
|
|
|
title: 使用 flex 短方法属性
|
2018-10-10 18:03:03 -04:00
|
|
|
challengeType: 0
|
2020-02-11 21:32:25 +08:00
|
|
|
videoUrl: 'https://scrimba.com/p/pVaDAv/cbpW2tE'
|
|
|
|
forumTopicId: 301112
|
2021-01-13 03:31:00 +01:00
|
|
|
dashedName: use-the-flex-shorthand-property
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --description--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
上面几个 flex 属性有一个简写方式。`flex-grow`、`flex-shrink` 和 `flex-basis` 属性可以在 `flex` 中一并设置。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
例如,`flex: 1 0 10px;` 会把项目属性设为 `flex-grow: 1;`、`flex-shrink: 0;` 以及 `flex-basis: 10px;`。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
属性的默认设置是 `flex: 0 1 auto;`。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
# --instructions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
请给 `#box-1` 和 `#box-2` 添加 `flex` 属性。设置 `#box-1` 的 `flex-grow` 属性值为 `2`、`flex-shrink` 属性值为 `2`、`flex-basis` 属性值为 `150px`;设置 `#box-2` 的 `flex-grow` 属性值为 `1`、`flex-shrink` 属性值为 `1`、`flex-basis` 属性值为 `150px`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
通过上面的设置,在容器大于 300px 时,`#box-1` 扩大的空间会是 `#box-2` 扩大空间的两倍;在容器小于 300px 时,`#box-1` 缩小的空间会是 `#box-2` 缩小空间的两倍。300px 是两个盒子的 `flex-basis` 属性值之和。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
`#box-1` 元素应具有 `flex` 属性,其属性值应为 `2 2 150px`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
$('#box-1').css('flex-grow') == '2' &&
|
|
|
|
$('#box-1').css('flex-shrink') == '2' &&
|
|
|
|
$('#box-1').css('flex-basis') == '150px'
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
`#box-2` 元素应具有 `flex` 属性,其属性值应为 `1 1 150px`。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
$('#box-2').css('flex-grow') == '1' &&
|
|
|
|
$('#box-2').css('flex-shrink') == '1' &&
|
|
|
|
$('#box-2').css('flex-basis') == '150px'
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
应使用 `flex` 的简写属性为 `#box-1` 和 `#box-2` 添加规则。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert(code.match(/flex:\s*?\d\s+?\d\s+?150px;/g).length == 2);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-02-11 21:32:25 +08:00
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```html
|
|
|
|
<style>
|
|
|
|
#box-container {
|
|
|
|
display: flex;
|
|
|
|
height: 500px;
|
|
|
|
}
|
|
|
|
#box-1 {
|
|
|
|
background-color: dodgerblue;
|
|
|
|
|
|
|
|
height: 200px;
|
|
|
|
}
|
|
|
|
|
|
|
|
#box-2 {
|
|
|
|
background-color: orangered;
|
|
|
|
|
|
|
|
height: 200px;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
|
|
|
|
<div id="box-container">
|
|
|
|
<div id="box-1"></div>
|
|
|
|
<div id="box-2"></div>
|
|
|
|
</div>
|
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
```html
|
|
|
|
<style>
|
|
|
|
#box-container {
|
|
|
|
display: flex;
|
|
|
|
height: 500px;
|
|
|
|
}
|
|
|
|
#box-1 {
|
|
|
|
background-color: dodgerblue;
|
|
|
|
flex: 2 2 150px;
|
|
|
|
height: 200px;
|
|
|
|
}
|
|
|
|
|
|
|
|
#box-2 {
|
|
|
|
background-color: orangered;
|
|
|
|
flex: 1 1 150px;
|
|
|
|
height: 200px;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
|
|
|
|
<div id="box-container">
|
|
|
|
<div id="box-1"></div>
|
|
|
|
<div id="box-2"></div>
|
|
|
|
</div>
|
|
|
|
```
|