2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 587d78b1367417b2b2512b09
|
|
|
|
|
title: Make an Image Responsive
|
|
|
|
|
challengeType: 0
|
2020-02-11 21:30:05 +08:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/cPp7VfD'
|
|
|
|
|
forumTopicId: 1
|
|
|
|
|
localeTitle: 使图片根据设备尺寸自如响应
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
2020-02-11 21:30:05 +08:00
|
|
|
|
<section id='description'>
|
|
|
|
|
用 CSS 来让图片自适应其实很简单。不要使用绝对单位:
|
|
|
|
|
<code>img { width: 720px; }</code>
|
|
|
|
|
你应该使用:
|
|
|
|
|
<blockquote>img {<br> max-width: 100%;<br> display: block;<br> height: auto;<br>}</blockquote>
|
|
|
|
|
<code>max-width</code> 属性能让图片以 100% 的最大宽度适应其父容器的宽度,但图片不会拉伸得比原始宽度还宽。把 <code>display</code> 属性值设置为 block,将 image 标签从内联元素(默认值)更改为块级元素。设置 <code>height</code> 属性为 auto 保持图片的原始宽高比。
|
|
|
|
|
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Instructions
|
2020-02-11 21:30:05 +08:00
|
|
|
|
<section id='instructions'>
|
|
|
|
|
|
|
|
|
|
给 <code>img</code> 标签增加样式规则使它自适应容器尺寸。应声明为块级元素,应适应它容器的宽度,应保持原本的宽高比。
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
|
|
```yml
|
|
|
|
|
tests:
|
2020-02-11 21:30:05 +08:00
|
|
|
|
- text: '<code>img</code> 标签应设置 <code>max-width</code> 为 100%。'
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(code.match(/max-width:\s*?100%;/g));
|
2020-02-11 21:30:05 +08:00
|
|
|
|
- text: '<code>img</code> 标签应设置 <code>display</code> 为 block。'
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert($('img').css('display') == 'block');
|
2020-02-11 21:30:05 +08:00
|
|
|
|
- text: '<code>img</code> 标签应设置 <code>height</code> 为 auto。'
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(code.match(/height:\s*?auto;/g));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
|
|
<div id='html-seed'>
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
<style>
|
2020-02-11 21:30:05 +08:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
</style>
|
|
|
|
|
|
|
|
|
|
<img src="https://s3.amazonaws.com/freecodecamp/FCCStickerPack.jpg" alt="freeCodeCamp stickers set">
|
|
|
|
|
```
|
|
|
|
|
|
2020-02-11 21:30:05 +08:00
|
|
|
|
|
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// solution required
|
|
|
|
|
```
|
2020-02-11 21:30:05 +08:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
</section>
|
2020-02-11 21:30:05 +08:00
|
|
|
|
|