| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | --- | 
					
						
							|  |  |  | id: 587d78b1367417b2b2512b09 | 
					
						
							| 
									
										
										
										
											2021-01-08 11:20:48 -08:00
										 |  |  | title: 使图片自适应设备尺寸 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | challengeType: 0 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | videoUrl: 'https://scrimba.com/p/pzrPu4/cz763UD' | 
					
						
							|  |  |  | forumTopicId: 301140 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | dashedName: make-an-image-responsive | 
					
						
							| 
									
										
										
										
											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 来让图片自适应其实很简单。 你只需要给图片添加这些属性: | 
					
						
							| 
									
										
										
										
											2020-02-11 21:30:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-03 22:41:15 +09:00
										 |  |  | ```css | 
					
						
							|  |  |  | img { | 
					
						
							|  |  |  |   max-width: 100%; | 
					
						
							|  |  |  |   height: auto; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-08 11:20:48 -08:00
										 |  |  | 设置 `max-width` 值为 `100%` 可确保图片不超出父容器的范围;设置 `height` 属性为 `auto` 可以保持图片的原始宽高比。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --instructions--
 | 
					
						
							| 
									
										
										
										
											2020-02-11 21:30:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | 给 `responsive-img` 添加样式规则,使其成为响应式的图片。 它不应该超出父容器(在本例中,即预览窗口)的范围,并保持宽高比不变。 添加代码后,拖动浏览器窗口,看看图片发生什么变化。 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | `responsive-img` 类应将 `max-width` 设置为 `100%`。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | ```js | 
					
						
							|  |  |  | assert(getComputedStyle($('.responsive-img')[0]).maxWidth === '100%'); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-08 11:20:48 -08:00
										 |  |  | `responsive-img` 类应将 `height` 设置为 `auto`。 | 
					
						
							| 
									
										
										
										
											2020-03-03 22:41:15 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | ```js | 
					
						
							|  |  |  | assert(code.match(/height:\s*?auto;/g)); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | # --seed--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## --seed-contents--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```html | 
					
						
							|  |  |  | <style> | 
					
						
							|  |  |  | .responsive-img { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | img { | 
					
						
							|  |  |  |   width: 600px; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | </style> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <img class="responsive-img" src="https://s3.amazonaws.com/freecodecamp/FCCStickerPack.jpg" alt="freeCodeCamp stickers set"> | 
					
						
							|  |  |  | <img src="https://s3.amazonaws.com/freecodecamp/FCCStickerPack.jpg" alt="freeCodeCamp stickers set"> | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --solutions--
 | 
					
						
							| 
									
										
										
										
											2020-02-11 21:30:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | ```html | 
					
						
							|  |  |  | <style> | 
					
						
							|  |  |  | .responsive-img { | 
					
						
							|  |  |  |   max-width: 100%; | 
					
						
							|  |  |  |   height: auto; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | img { | 
					
						
							|  |  |  |   width: 600px; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | </style> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <img class="responsive-img" src="https://s3.amazonaws.com/freecodecamp/FCCStickerPack.jpg" alt="freeCodeCamp stickers set"> | 
					
						
							|  |  |  | <img src="https://s3.amazonaws.com/freecodecamp/FCCStickerPack.jpg" alt="freeCodeCamp stickers set"> | 
					
						
							|  |  |  | ``` |