| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | --- | 
					
						
							|  |  |  |  | id: 5a94fe5469fb03452672e461 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | title: 使用 auto-fill 创建弹性布局 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | challengeType: 0 | 
					
						
							| 
									
										
										
										
											2020-02-11 21:39:15 +08:00
										 |  |  |  | videoUrl: 'https://scrimba.com/p/pByETK/cmzdycW' | 
					
						
							|  |  |  |  | forumTopicId: 301126 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  |  | dashedName: create-flexible-layouts-using-auto-fill | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | --- | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | # --description--
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  |  | repeat 方法带有一个名为<dfn>自动填充(auto-fill)</dfn>的功能。 它的功能是根据容器的大小,尽可能多地放入指定大小的行或列。 你可以通过结合 `auto-fill` 和 `minmax` 来更灵活地布局。 | 
					
						
							| 
									
										
										
										
											2020-02-11 21:39:15 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | ```css | 
					
						
							|  |  |  |  | repeat(auto-fill, minmax(60px, 1fr)); | 
					
						
							|  |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  |  | 上面的代码效果是这样:首先,列的宽度会随容器大小改变。其次,只要容器宽度不足以插入一个宽为 60px 的列,当前行的所有列就都会一直拉伸。请自己调整宽度,动手试一下就不难理解了。 **注意:**如果容器宽度不足以将所有网格项放在同一行,余下的网格项将会移至新的一行。 | 
					
						
							| 
									
										
										
										
											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-02-06 04:42:36 +00:00
										 |  |  |  | 在第一个网格中,请使用 `auto-fill` 和 `repeat` 来填充网格。 其中列宽的最小值为 `60px`,最大值为 `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` 属性,且属性值应使用 `repeat` 和 `auto-fill`,以便将最小宽度为 `60px`、最大宽度为 `1fr` 的列填充至网格。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | ```js | 
					
						
							|  |  |  |  | assert( | 
					
						
							|  |  |  |  |   code.match( | 
					
						
							|  |  |  |  |     /.container\s*?{[\s\S]*grid-template-columns\s*?:\s*?repeat\s*?\(\s*?auto-fill\s*?,\s*?minmax\s*?\(\s*?60px\s*?,\s*?1fr\s*?\)\s*?\)\s*?;[\s\S]*}/gi | 
					
						
							|  |  |  |  |   ) | 
					
						
							|  |  |  |  | ); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04: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: 100px; | 
					
						
							|  |  |  |  |     width: 100%; | 
					
						
							|  |  |  |  |     background: LightGray; | 
					
						
							|  |  |  |  |     display: grid; | 
					
						
							|  |  |  |  |     /* Only change code below this line */ | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     grid-template-columns: repeat(3, minmax(60px, 1fr)); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /* Only change code above this line */ | 
					
						
							|  |  |  |  |     grid-template-rows: 1fr 1fr 1fr; | 
					
						
							|  |  |  |  |     grid-gap: 10px; | 
					
						
							|  |  |  |  |   } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |   .container2 { | 
					
						
							|  |  |  |  |     font-size: 40px; | 
					
						
							|  |  |  |  |     min-height: 100px; | 
					
						
							|  |  |  |  |     width: 100%; | 
					
						
							|  |  |  |  |     background: Silver; | 
					
						
							|  |  |  |  |     display: grid; | 
					
						
							|  |  |  |  |     grid-template-columns: repeat(3, minmax(60px, 1fr)); | 
					
						
							|  |  |  |  |     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> | 
					
						
							|  |  |  |  | <div class="container2"> | 
					
						
							|  |  |  |  |   <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--
 | 
					
						
							| 
									
										
										
										
											2020-02-11 21:39:15 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  |  | ```html | 
					
						
							|  |  |  |  | <style> | 
					
						
							|  |  |  |  |   .item1{background:LightSkyBlue;} | 
					
						
							|  |  |  |  |   .item2{background:LightSalmon;} | 
					
						
							|  |  |  |  |   .item3{background:PaleTurquoise;} | 
					
						
							|  |  |  |  |   .item4{background:LightPink;} | 
					
						
							|  |  |  |  |   .item5{background:PaleGreen;} | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  |  |   .container { | 
					
						
							|  |  |  |  |     font-size: 40px; | 
					
						
							|  |  |  |  |     min-height: 100px; | 
					
						
							|  |  |  |  |     width: 100%; | 
					
						
							|  |  |  |  |     background: LightGray; | 
					
						
							|  |  |  |  |     display: grid; | 
					
						
							|  |  |  |  |     /* Only change code below this line */ | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  |  |     grid-template-columns: repeat(auto-fill, minmax(60px, 1fr)); | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  |  |     /* Only change code above this line */ | 
					
						
							|  |  |  |  |     grid-template-rows: 1fr 1fr 1fr; | 
					
						
							|  |  |  |  |     grid-gap: 10px; | 
					
						
							|  |  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  |  |   .container2 { | 
					
						
							|  |  |  |  |     font-size: 40px; | 
					
						
							|  |  |  |  |     min-height: 100px; | 
					
						
							|  |  |  |  |     width: 100%; | 
					
						
							|  |  |  |  |     background: Silver; | 
					
						
							|  |  |  |  |     display: grid; | 
					
						
							|  |  |  |  |     grid-template-columns: repeat(3, minmax(60px, 1fr)); | 
					
						
							|  |  |  |  |     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> | 
					
						
							|  |  |  |  | <div class="container2"> | 
					
						
							|  |  |  |  |   <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> | 
					
						
							|  |  |  |  | ``` |