2.0 KiB
2.0 KiB
id, challengeType, videoUrl, forumTopicId, title
id | challengeType | videoUrl | forumTopicId | title |
---|---|---|---|---|
587d78b1367417b2b2512b09 | 0 | https://scrimba.com/c/cPp7VfD | 1 | 使图片根据设备尺寸自如响应 |
Description
img {
max-width: 100%;
height: auto;
}
设置 max-width
值为 100%
可确保图片不超出父容器的范围。设置 height
属性为 auto
保持图片的原始宽高比。
Instructions
给 responsive-img
添加样式规则,使其成为响应式。它不应该超出父容器(在本例中,即预览窗口)的范围,并保持宽高比不变。添加代码后,拖动浏览器窗口,看看图片发生什么变化。
Tests
tests:
- text: <code>responsive-img</code> 类应设置 <code>max-width</code> 为 <code>100%</code>。
testString: assert(getComputedStyle($('.responsive-img')[0]).maxWidth === '100%');
- text: <code>responsive-img</code> 类应设置 <code>height</code> 为 <code>auto</code>。
testString: assert(code.match(/height:\s*?auto;/g));
Challenge Seed
<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">
Solution
<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">