| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | --- | 
					
						
							|  |  |  |  | id: 587d78a6367417b2b2512ade | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | title: 使用 CSS 和 HTML 创建更复杂的形状 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | challengeType: 0 | 
					
						
							| 
									
										
										
										
											2020-02-11 15:46:34 +08:00
										 |  |  |  | videoUrl: 'https://scrimba.com/c/cPpz4fr' | 
					
						
							|  |  |  |  | forumTopicId: 301050 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  |  | dashedName: create-a-more-complex-shape-using-css-and-html | 
					
						
							| 
									
										
										
										
											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
										 |  |  |  | 世界上最流行的形状非心形莫属了,在本挑战中我们将用纯 CSS 创建一个心形。 但是首先你需要了解伪元素 `::before` 和 `::after`。 伪元素可以在所选元素之前或之后添加一些内容。 在下面的代码中,`::before` 伪元素用来给 class 为 `heart` 的元素添加一个正方形: | 
					
						
							| 
									
										
										
										
											2020-02-11 15:46:34 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | ```css | 
					
						
							|  |  |  |  | .heart::before { | 
					
						
							|  |  |  |  |   content: ""; | 
					
						
							|  |  |  |  |   background-color: yellow; | 
					
						
							|  |  |  |  |   border-radius: 25%; | 
					
						
							|  |  |  |  |   position: absolute; | 
					
						
							|  |  |  |  |   height: 50px; | 
					
						
							|  |  |  |  |   width: 70px; | 
					
						
							|  |  |  |  |   top: -50px; | 
					
						
							|  |  |  |  |   left: 5px; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  |  | `::before` 和 `::after` 必须配合 `content` 来使用。 这个属性通常用来给元素添加内容诸如图片或者文字。 尽管有时 `::before` 和 `::after` 是用来实现形状而非文字,但 `content` 属性仍然是必需的,此时它的值可以是空字符串。 在上面的例子里,class 为 `heart` 元素的 `::before` 伪类添加了一个黄色的长方形,长方形的高和宽分别为 `50px` 和 `70px`。 这个矩形有圆角,因为它的 `border-radius` 为 25%,它的位置是绝对位置,位于离元素左边和顶部分别是 `5px`、`50px` 的位置。 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |  | 把屏幕里的元素变成心形。 在 `heart::after` 选择器里,把 `background-color` 改成 `pink`,把 `border-radius` 改成 50%。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  |  | 接下来,用类选择器选取 class 为 `heart`(只是 `heart`)的元素,为它添加 `transform` 属性。 使用 `rotate()` 函数并设置角度为 -45 度。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-08 11:20:48 -08:00
										 |  |  |  | 最后,在 `heart::before` 选择器里面,设置 `content` 属性值为空字符串。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | # --hints--
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  |  | `heart::after` 选择器的 `background-color` 属性值应为 `pink`。 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | ```js | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  |  | const heartAfter = code.match(/\.heart::after\s*{[\s\S]+?[^\}]}/g)[0]; | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | assert( | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  |  |   /({|;)\s*background-color\s*:\s*pink\s*(;|})/g.test(heartAfter) | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | ); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-08 11:20:48 -08:00
										 |  |  |  | `heart::after` 伪元素的 `border-radius` 属性值应为 50%。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | ```js | 
					
						
							|  |  |  |  | assert(code.match(/border-radius\s*?:\s*?50%/gi).length == 2); | 
					
						
							|  |  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  |  | class 为 `heart` 的元素的 `transform` 属性应使用 `rotate()` 函数并传入参数 -45 度。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | ```js | 
					
						
							|  |  |  |  | assert(code.match(/transform\s*?:\s*?rotate\(\s*?-45deg\s*?\)/gi)); | 
					
						
							|  |  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-08 11:20:48 -08:00
										 |  |  |  | `heart::before` 伪元素的 `content` 应为空字符串。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | ```js | 
					
						
							|  |  |  |  | assert(code.match(/\.heart::before\s*?{\s*?content\s*?:\s*?("|')\1\s*?;/gi)); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2020-02-11 15:46:34 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  |  | # --seed--
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ## --seed-contents--
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ```html | 
					
						
							|  |  |  |  | <style> | 
					
						
							|  |  |  |  |   .heart { | 
					
						
							|  |  |  |  |     position: absolute; | 
					
						
							|  |  |  |  |     margin: auto; | 
					
						
							|  |  |  |  |     top: 0; | 
					
						
							|  |  |  |  |     right: 0; | 
					
						
							|  |  |  |  |     bottom: 0; | 
					
						
							|  |  |  |  |     left: 0; | 
					
						
							|  |  |  |  |     background-color: pink; | 
					
						
							|  |  |  |  |     height: 50px; | 
					
						
							|  |  |  |  |     width: 50px; | 
					
						
							|  |  |  |  |     transform: ; | 
					
						
							|  |  |  |  |   } | 
					
						
							|  |  |  |  |   .heart::after { | 
					
						
							|  |  |  |  |     background-color: blue; | 
					
						
							|  |  |  |  |     content: ""; | 
					
						
							|  |  |  |  |     border-radius: 25%; | 
					
						
							|  |  |  |  |     position: absolute; | 
					
						
							|  |  |  |  |     width: 50px; | 
					
						
							|  |  |  |  |     height: 50px; | 
					
						
							|  |  |  |  |     top: 0px; | 
					
						
							|  |  |  |  |     left: 25px; | 
					
						
							|  |  |  |  |   } | 
					
						
							|  |  |  |  |   .heart::before { | 
					
						
							|  |  |  |  |     content: ; | 
					
						
							|  |  |  |  |     background-color: pink; | 
					
						
							|  |  |  |  |     border-radius: 50%; | 
					
						
							|  |  |  |  |     position: absolute; | 
					
						
							|  |  |  |  |     width: 50px; | 
					
						
							|  |  |  |  |     height: 50px; | 
					
						
							|  |  |  |  |     top: -25px; | 
					
						
							|  |  |  |  |     left: 0px; | 
					
						
							|  |  |  |  |   } | 
					
						
							|  |  |  |  | </style> | 
					
						
							|  |  |  |  | <div class="heart"></div> | 
					
						
							|  |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | # --solutions--
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  |  | ```html | 
					
						
							|  |  |  |  | <style> | 
					
						
							|  |  |  |  |   .heart { | 
					
						
							|  |  |  |  |     position: absolute; | 
					
						
							|  |  |  |  |     margin: auto; | 
					
						
							|  |  |  |  |     top: 0; | 
					
						
							|  |  |  |  |     right: 0; | 
					
						
							|  |  |  |  |     bottom: 0; | 
					
						
							|  |  |  |  |     left: 0; | 
					
						
							|  |  |  |  |     background-color: pink; | 
					
						
							|  |  |  |  |     height: 50px; | 
					
						
							|  |  |  |  |     width: 50px; | 
					
						
							|  |  |  |  |     transform: rotate(-45deg); | 
					
						
							|  |  |  |  |   } | 
					
						
							|  |  |  |  |   .heart::after { | 
					
						
							|  |  |  |  |     background-color: pink; | 
					
						
							|  |  |  |  |     content: ""; | 
					
						
							|  |  |  |  |     border-radius: 50%; | 
					
						
							|  |  |  |  |     position: absolute; | 
					
						
							|  |  |  |  |     width: 50px; | 
					
						
							|  |  |  |  |     height: 50px; | 
					
						
							|  |  |  |  |     top: 0px; | 
					
						
							|  |  |  |  |     left: 25px; | 
					
						
							|  |  |  |  |   } | 
					
						
							|  |  |  |  |   .heart::before { | 
					
						
							|  |  |  |  |     content: ""; | 
					
						
							|  |  |  |  |     background-color: pink; | 
					
						
							|  |  |  |  |     border-radius: 50%; | 
					
						
							|  |  |  |  |     position: absolute; | 
					
						
							|  |  |  |  |     width: 50px; | 
					
						
							|  |  |  |  |     height: 50px; | 
					
						
							|  |  |  |  |     top: -25px; | 
					
						
							|  |  |  |  |     left: 0px; | 
					
						
							|  |  |  |  |   } | 
					
						
							|  |  |  |  | </style> | 
					
						
							|  |  |  |  | <div class="heart"></div> | 
					
						
							|  |  |  |  | ``` |