2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 5a94fe4469fb03452672e460
|
2020-12-16 00:37:30 -07:00
|
|
|
|
title: 使用 minmax 函数限制项目大小
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 0
|
2020-02-11 21:39:15 +08:00
|
|
|
|
videoUrl: 'https://scrimba.com/p/pByETK/cD97RTv'
|
|
|
|
|
forumTopicId: 301131
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: limit-item-size-using-the-minmax-function
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
此外,内置函数 `minmax` 也可用于设置 `grid-template-columns` 和 `grid-template-rows` 的值。它的作用是在网格容器改变大小时限制网格项的大小。为此,你需要指定网格项允许的尺寸范围。例如:
|
2020-02-11 21:39:15 +08:00
|
|
|
|
|
|
|
|
|
```css
|
|
|
|
|
grid-template-columns: 100px minmax(50px, 200px);
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
在上面的代码中,我们通过 `grid-template-columns` 添加了两列,第一列宽度为 100px,第二列宽度最小值是 50px,最大值是 200px。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
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
|
|
|
|
请用 `minmax` 函数替换 `repeat` 函数中的 `1fr`,限定其最小值为 `90px`,最大值为`1fr`。你可以调整最右侧的预览面板查看效果。
|
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
|
|
|
|
class 为 `container` 的元素应使用 `grid-template-columns` 属性设置 3 列,其中,每列最小宽度应为 `90px`,最大宽度应为 `1fr`。
|
2020-02-11 21:39:15 +08:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(
|
|
|
|
|
code.match(
|
|
|
|
|
/.container\s*?{[\s\S]*grid-template-columns\s*?:\s*?repeat\s*?\(\s*?3\s*?,\s*?minmax\s*?\(\s*?90px\s*?,\s*?1fr\s*?\)\s*?\)\s*?;[\s\S]*}/gi
|
|
|
|
|
)
|
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-02-11 21:39:15 +08:00
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
<style>
|
|
|
|
|
.item1{background:LightSkyBlue;}
|
|
|
|
|
.item2{background:LightSalmon;}
|
|
|
|
|
.item3{background:PaleTurquoise;}
|
|
|
|
|
.item4{background:LightPink;}
|
|
|
|
|
.item5{background:PaleGreen;}
|
|
|
|
|
|
|
|
|
|
.container {
|
|
|
|
|
font-size: 40px;
|
|
|
|
|
min-height: 300px;
|
|
|
|
|
width: 100%;
|
|
|
|
|
background: LightGray;
|
|
|
|
|
display: grid;
|
|
|
|
|
/* Only change code below this line */
|
|
|
|
|
|
|
|
|
|
grid-template-columns: repeat(3, 1fr);
|
|
|
|
|
|
|
|
|
|
/* Only change code above this line */
|
|
|
|
|
grid-template-rows: 1fr 1fr 1fr;
|
|
|
|
|
grid-gap: 10px;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
|
|
|
|
|
<div class="container">
|
|
|
|
|
<div class="item1">1</div>
|
|
|
|
|
<div class="item2">2</div>
|
|
|
|
|
<div class="item3">3</div>
|
|
|
|
|
<div class="item4">4</div>
|
|
|
|
|
<div class="item5">5</div>
|
|
|
|
|
</div>
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```html
|
|
|
|
|
<style>.container {grid-template-columns: repeat(3, minmax(90px, 1fr));}</style>
|
|
|
|
|
```
|