2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
id: 587d78ae367417b2b2512afd
|
2021-01-08 19:20:48 +00:00
|
|
|
|
title: 使用 flex-basis 属性设置元素的初始大小
|
2018-10-10 22:03:03 +00:00
|
|
|
|
challengeType: 0
|
2020-02-11 13:32:25 +00:00
|
|
|
|
videoUrl: 'https://scrimba.com/p/pVaDAv/c3d9nCa'
|
|
|
|
|
forumTopicId: 301108
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: use-the-flex-basis-property-to-set-the-initial-size-of-an-item
|
2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --description--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
`flex-basis` 属性定义了在使用 CSS 的 `flex-shrink` 或 `flex-grow` 属性对元素进行调整前,元素的初始大小。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
`flex-basis` 属性的单位与其他表示尺寸的属性的单位一致(`px`、`em`、`%` 等)。 如果值为 `auto`,则项目的尺寸随内容调整。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
使用 `flex-basis` 为盒子设置初始值。 请给 `#box-1` 和 `#box-2` 添加 CSS 属性 `flex-basis`。 设置 `#box-1` 的尺寸初始值为 `10em`,`#box-2` 的尺寸初始值为 `20em`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --hints--
|
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
`#box-1` 元素应具有 `flex-basis` 属性。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert($('#box-1').css('flex-basis') != 'auto');
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
`#box-1` 的 `flex-basis` 属性值应为 `10em`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(code.match(/#box-1\s*?{\s*?.*?\s*?.*?\s*?flex-basis:\s*?10em;/g));
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
`#box-2` 元素应具有 `flex-basis` 属性。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert($('#box-2').css('flex-basis') != 'auto');
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
`#box-2` 的 `flex-basis` 属性值应为 `20em`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(code.match(/#box-2\s*?{\s*?.*?\s*?.*?\s*?flex-basis:\s*?20em;/g));
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
2020-02-11 13:32:25 +00:00
|
|
|
|
|
2021-01-13 02:31:00 +00: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 07:37:30 +00:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
```html
|
|
|
|
|
<style>
|
|
|
|
|
#box-container {
|
|
|
|
|
display: flex;
|
|
|
|
|
height: 500px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#box-1 {
|
|
|
|
|
background-color: dodgerblue;
|
|
|
|
|
height: 200px;
|
|
|
|
|
flex-basis: 10em;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#box-2 {
|
|
|
|
|
background-color: orangered;
|
|
|
|
|
height: 200px;
|
|
|
|
|
flex-basis: 20em;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
|
|
|
|
|
<div id="box-container">
|
|
|
|
|
<div id="box-1"></div>
|
|
|
|
|
<div id="box-2"></div>
|
|
|
|
|
</div>
|
|
|
|
|
```
|