| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | --- | 
					
						
							|  |  |  |  | id: 5a9036ee38fddaf9a66b5d34 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | title: 使用 CSS 网格单位来更改列和行的大小 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | challengeType: 0 | 
					
						
							| 
									
										
										
										
											2020-02-11 21:39:15 +08:00
										 |  |  |  | videoUrl: 'https://scrimba.com/p/pByETK/cvE8phd' | 
					
						
							|  |  |  |  | forumTopicId: 301134 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  |  | dashedName: use-css-grid-units-to-change-the-size-of-columns-and-rows | 
					
						
							| 
									
										
										
										
											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
										 |  |  |  | 在 CSS 网格中,可以使用绝对单位(如 `px`)或相对单位(如 `em`)来定义行或列的大小。 下面的单位也可以使用: | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-08 11:20:48 -08:00
										 |  |  |  | `fr`:设置列或行占剩余空间的比例, | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | `auto`:设置列宽或行高自动等于它的内容的宽度或高度, | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | `%`:将列或行调整为它的容器宽度或高度的百分比, | 
					
						
							| 
									
										
										
										
											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
										 |  |  |  | ```css | 
					
						
							|  |  |  |  | grid-template-columns: auto 50px 10% 2fr 1fr; | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  |  | 这段代码添加了五个列。 第一列的宽与它的内容宽度相等;第二列宽 50px;第三列宽是它容器的 10%;最后两列,将剩余的宽度平均分成三份,第四列占两份,第五列占一份。 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |  | 生成一个包含三列的网格,每列宽度分别为:1fr、100px、2fr。 | 
					
						
							| 
									
										
										
										
											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` 属性且属性值应为 `1fr 100px 2fr`。 | 
					
						
							| 
									
										
										
										
											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*?1fr\s*?100px\s*?2fr\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> | 
					
						
							|  |  |  |  |   .d1{background:LightSkyBlue;} | 
					
						
							|  |  |  |  |   .d2{background:LightSalmon;} | 
					
						
							|  |  |  |  |   .d3{background:PaleTurquoise;} | 
					
						
							|  |  |  |  |   .d4{background:LightPink;} | 
					
						
							|  |  |  |  |   .d5{background:PaleGreen;} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |   .container { | 
					
						
							|  |  |  |  |     font-size: 40px; | 
					
						
							|  |  |  |  |     width: 100%; | 
					
						
							|  |  |  |  |     background: LightGray; | 
					
						
							|  |  |  |  |     display: grid; | 
					
						
							|  |  |  |  |     /* Only change code below this line */ | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     grid-template-columns: auto 50px 10% 2fr 1fr; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /* Only change code above this line */ | 
					
						
							|  |  |  |  |     grid-template-rows: 50px 50px; | 
					
						
							|  |  |  |  |   } | 
					
						
							|  |  |  |  | </style> | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | <div class="container"> | 
					
						
							|  |  |  |  |   <div class="d1">1</div> | 
					
						
							|  |  |  |  |   <div class="d2">2</div> | 
					
						
							|  |  |  |  |   <div class="d3">3</div> | 
					
						
							|  |  |  |  |   <div class="d4">4</div> | 
					
						
							|  |  |  |  |   <div class="d5">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: 1fr 100px 2fr;}</style> | 
					
						
							|  |  |  |  | ``` |