响应式 Web 设计 - 图片
使用 width 属性
如果 width 属性设置为 100%,图片会根据上下范围实现响应式功能:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<style>
img {
width: 100%;
height: auto;
}
</style>
</head>
<body>
<img src="/examples/img_chania.jpg" width="460" height="345">
<p>调整浏览器窗口查看图像是如何扩展的。</p>
</body>
</html>
注意在以上实例中,图片会比它的原始图片大。我们可以使用 max-width 属性很好的解决这个问题。
使用 max-width 属性
如果 max-width 属性设置为 100%, 图片永远不会大于其原始大小:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<style>
img {
max-width: 100%;
height: auto;
}
</style>
</head>
<body>
<img src="/examples/img_chania.jpg" width="460" height="345">
<p>调整浏览器大小,在宽度小于 460px 时查看图片比例变化。</p>
</body>
</html>
网页中添加图片
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<style>
* {
box-sizing: border-box;
}
img {
width: 100%;
height: auto;
}
.row:after {
content: "";
clear: both;
display: block;
}
[class*="col-"] {
float: left;
padding: 15px;
width: 100%;
}
@media only screen and (min-width: 600px) {
.col-s-1 {
width: 8.33%;
}
.col-s-2 {
width: 16.66%;
}
.col-s-3 {
width: 25%;
}
.col-s-4 {
width: 33.33%;
}
.col-s-5 {
width: 41.66%;
}
.col-s-6 {
width: 50%;
}
.col-s-7 {
width: 58.33%;
}
.col-s-8 {
width: 66.66%;
}
.col-s-9 {
width: 75%;
}
.col-s-10 {
width: 83.33%;
}
.col-s-11 {
width: 91.66%;
}
.col-s-12 {
width: 100%;
}
}
@media only screen and (min-width: 768px) {
.col-1 {
width: 8.33%;
}
.col-2 {
width: 16.66%;
}
.col-3 {
width: 25%;
}
.col-4 {
width: 33.33%;
}
.col-5 {
width: 41.66%;
}
.col-6 {
width: 50%;
}
.col-7 {
width: 58.33%;
}
.col-8 {
width: 66.66%;
}
.col-9 {
width: 75%;
}
.col-10 {
width: 83.33%;
}
.col-11 {
width: 91.66%;
}
.col-12 {
width: 100%;
}
}
html {
font-family: "Lucida Sans", sans-serif;
}
.header {
background-color: #9933cc;
color: #ffffff;
padding: 15px;
}
.menu ul {
list-style-type: none;
margin: 0;
padding: 0;
}
.menu li {
padding: 8px;
margin-bottom: 7px;
background-color: #33b5e5;
color: #ffffff;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
}
.menu li:hover {
background-color: #0099cc;
}
.aside {
background-color: #33b5e5;
padding: 15px;
color: #ffffff;
text-align: center;
font-size: 14px;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
}
.footer {
background-color: #0099cc;
color: #ffffff;
text-align: center;
font-size: 12px;
padding: 15px;
}
</style>
</head>
<body>
<div class="header">
<h1>Chania</h1>
</div>
<div class="row">
<div class="col-3 col-s-3 menu">
<ul>
<li>The Flight</li>
<li>The City</li>
<li>The Island</li>
<li>The Food</li>
</ul>
</div>
<div class="col-6 col-s-9">
<h1>The City</h1>
<p>Chania is the capital of the Chania region on the island of Crete. The city can be divided in two parts, the old town and the modern city.</p>
<img src="/examples/img_chania.jpg" width="460" height="345">
</div>
<div class="col-3 col-s-12">
<div class="aside">
<h2>What?</h2>
<p>Chania is a city on the island of Crete.</p>
<h2>Where?</h2>
<p>Crete is a Greek island in the Mediterranean Sea.</p>
<h2>How?</h2>
<p>You can reach Chania airport from all over Europe.</p>
</div>
</div>
</div>
<div class="footer">
<p>调整浏览器窗口大小查看内容变化。</p>
</div>
</body>
</html>
背景图片
背景图片可以响应调整大小或缩放。
以下是三个不同的方法:
1. 如果 background-size 属性设置为 "contain", 背景图片将按比例自适应内容区域。图片保持其比例不变:
这是 CSS 代码:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<style>
div {
width: 100%;
height: 400px;
background-image: url('/wp-content/uploads/2015/06/img_flowers.jpg');
background-repeat: no-repeat;
background-size: contain;
border: 1px solid red;
}
</style>
</head>
<body>
<p>调整浏览器大小查看效果。</p>
<div></div>
</body>
</html>
2. 如果 background-size 属性设置为 "100% 100%" ,背景图片将延展覆盖整个区域:
这是 CSS 代码:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<style>
div {
width: 100%;
height: 400px;
background-image: url('/wp-content/uploads/2015/06/img_flowers.jpg');
background-size: 100% 100%;
border: 1px solid red;
}
</style>
</head>
<body>
<p>调整浏览器大小查看效果。</p>
<div></div>
</body>
</html>
3. 如果 background-size 属性设置为 "cover",则会把背景图像扩展至足够大,以使背景图像完全覆盖背景区域。注意该属性保持了图片的比例因此 背景图像的某些部分无法显示在背景定位区域中。
这是 CSS 代码:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<style>
div {
width: 100%;
height: 400px;
background-image: url('/wp-content/uploads/2015/06/img_flowers.jpg');
background-size: cover;
border: 1px solid red;
}
</style>
</head>
<body>
<p>调整浏览器大小查看效果。</p>
<div></div>
</body>
</html>
不同设备显示不同图片
大尺寸图片可以显示在大屏幕上,但在小屏幕上确不能很好显示。我们没有必要在小屏幕上去加载大图片,这样很影响加载速度。所以我们可以使用媒体查询,根据不同的设备显示不同的图片。
以下大图片和小图片将显示在不同设备上:


<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<style>
/* For width smaller than 400px: */
body {
background-repeat: no-repeat;
background-image: url('/wp-content/uploads/2015/06/img_smallflower.jpg');
}
/* For width 400px and larger: */
@media only screen and (min-width: 400px) {
body {
background-image: url('/wp-content/uploads/2015/06/img_flowers.jpg');
}
}
</style>
</head>
<body>
<p style="margin-top:360px;">调整浏览器宽度,背景图片在小于 400 px 时将改变。</p>
</body>
</html>
你可以使用媒体查询的 min-device-width 替代 min-width 属性,它将检测的是设备宽度而不是浏览器宽度。浏览器大小重置时,图片大小不会改变。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<style>
/* For device width smaller than 400px: */
body {
background-repeat: no-repeat;
background-image: url('/wp-content/uploads/2015/06/img_smallflower.jpg');
}
/* For device width 400px and larger: */
@media only screen and (min-device-width: 400px) {
body {
background-image: url('/wp-content/uploads/2015/06/img_flowers.jpg');
}
}
</style>
</head>
<body>
</body>
</html>
HTML5 <picture> 元素
HTML5 的 <picture> 元素可以设置多张图片。
浏览器支持
| 元素 | |||||
|---|---|---|---|---|---|
| <picture> | 不支持 | 38.0 | 38.0 | 不支持 | 25.0 |
<picture> 元素类似于 <video> 和
<audio> 元素。可以设备不同的资源,第一个设置的资源为首选使用的:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
</head>
<body>
<picture>
<source srcset="/wp-content/uploads/2015/06/img_smallflower.jpg" media="(max-width: 400px)">
<source srcset="/wp-content/uploads/2015/06/img_flowers.jpg">
<img src="/wp-content/uploads/2015/06/img_flowers.jpg" alt="Flowers" style="width:auto;">
</picture>
<p>调整浏览器宽度和高度,背景在宽度小于 400px 时将改变。 </p>
</body>
</html>
srcset 属性的必须的,定义了图片资源。
media 属性是可选的,可以在媒体查询的
CSS @media 规则 查看详情。
对于不支持 <picture> 元素的浏览器你也可以定义 <img> 元素来替代。