| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | --- | 
					
						
							|  |  |  | id: 5a94fe6269fb03452672e462 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | title: 使用 auto-fit 创建弹性布局 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | challengeType: 0 | 
					
						
							| 
									
										
										
										
											2020-02-11 21:39:15 +08:00
										 |  |  | videoUrl: 'https://scrimba.com/p/pByETK/c3dPph8' | 
					
						
							|  |  |  | forumTopicId: 301127 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | dashedName: create-flexible-layouts-using-auto-fit | 
					
						
							| 
									
										
										
										
											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-02-06 04:42:36 +00:00
										 |  |  | `auto-fit` 效果几乎和 `auto-fill` 一样。 不同点仅在于,当容器的大小大于各网格项之和时,`auto-fill` 会持续地在一端放入空行或空列,这样就会使所有网格项挤到另一边;而 `auto-fit` 则不会在一端放入空行或空列,而是会将所有网格项拉伸至合适的大小。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-08 11:20:48 -08:00
										 |  |  | **注意:**如果容器宽度不足以将所有网格项放在同一行,余下的网格项将会移至新的一行。 | 
					
						
							| 
									
										
										
										
											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-fit` 和 `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-03-05 08:43:24 -07:00
										 |  |  | `container2` class 应该有一个 `grid-template-columns` 属性,通过 `repeat` 和 `auto-fit` 将网格中的列的宽度设置为最小 `60px`,最大 `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( | 
					
						
							|  |  |  |     /.container2\s*?{[\s\S]*grid-template-columns\s*?:\s*?repeat\s*?\(\s*?auto-fit\s*?,\s*?minmax\s*?\(\s*?60px\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: 100px; | 
					
						
							|  |  |  |     width: 100%; | 
					
						
							|  |  |  |     background: LightGray; | 
					
						
							|  |  |  |     display: grid; | 
					
						
							|  |  |  |     grid-template-columns: repeat(auto-fill, minmax(60px, 1fr)); | 
					
						
							|  |  |  |     grid-template-rows: 1fr 1fr 1fr; | 
					
						
							|  |  |  |     grid-gap: 10px; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   .container2 { | 
					
						
							|  |  |  |     font-size: 40px; | 
					
						
							|  |  |  |     min-height: 100px; | 
					
						
							|  |  |  |     width: 100%; | 
					
						
							|  |  |  |     background: Silver; | 
					
						
							|  |  |  |     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; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | </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--
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | ```html | 
					
						
							|  |  |  | <style>.container {grid-template-columns: repeat( auto-fill, minmax(60px, 1fr));} .container2 {grid-template-columns: repeat(auto-fit, minmax(60px, 1fr));}</style> | 
					
						
							|  |  |  | ``` |