freeCodeCamp/curriculum/challenges/chinese/01-responsive-web-design/css-grid/reduce-repetition-using-the...

1.4 KiB
Raw Blame History

id title challengeType videoUrl forumTopicId
5a94fe3669fb03452672e45f 使用 repeat 函数减少重复 0 https://scrimba.com/p/pByETK/cQvqyHR 301133

--description--

当使用grid-template-columnsgrid-template-rows定义网格结构时,你需要为添加的每一行和每一列都输入一个值。

如果一个网格共有 100 行,每行高度相同,就得输入 100 个值,这不太实际。幸运的是,有一种更好的方法——使用repeat方法指定行或列的重复次数,后面加上逗号以及需要重复的值。

这里有一个添加 100 行网格的例子,使每行高度均为 50px

grid-template-rows: repeat(100, 50px);

你还可以用 repeat 方法重复多个值,并在定义网格结构时与其他值一起使用。举个例子:

grid-template-columns: repeat(2, 1fr 50px) 20px;

效果相当于:

grid-template-columns: 1fr 50px 1fr 50px 20px;

注意:
1fr 50px重复了两次,后面跟着 20px。

--instructions--

repeat代替grid-template-columns属性值中的重复代码。

--hints--

container类应该带有grid-template-columns属性且设置为重复 3 列,宽为1fr

assert(
  code.match(
    /.container\s*?{[\s\S]*grid-template-columns\s*?:\s*?repeat\s*?\(\s*?3\s*?,\s*?1fr\s*?\)\s*?;[\s\S]*}/gi
  )
);

--solutions--