使用 width 属性
如果 width 属性设置为 100%,图片会根据上下范围实现响应式功能:
实例
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta charset="utf-8"> <title>php 中文网</title> <style> img { width: 100%; height: auto; } </style> </head> <body> <img src="/uploads/images/201712120957521200.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"> <title>php 中文网</title> <style> img { max-width: 100%; height: auto; } </style> </head> <body> <img src="/uploads/images/201712120957521200.jpg" width="460" height="345"> <p>调整浏览器大小,在宽度小于 460px 时查看图片比例变化。</p> </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"> <title>php 中文网</title> <style> div { width: 100%; height: 400px; background-image: url('/uploads/images/201712120957521200.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%" ,背景图片将延展覆盖整个区域:
实例
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta charset="utf-8"> <title>php 中文网</title> <style> div { width: 100%; height: 400px; background-image: url('/uploads/images/201712120957521200.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"> <title>php 中文网</title> <style> div { width: 100%; height: 400px; background-image: url('/uploads/images/201712120957521200.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"> <title>php 中文网</title> <style> /* For width smaller than 400px: */ body { background-repeat: no-repeat; background-image: url('/uploads/images/201712120957521200.jpg'); } /* For width 400px and larger: */ @media only screen and (min-width: 400px) { body { background-image: url('/uploads/images/201712120957521200.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"> <title>php 中文网</title> <style> /* For device width smaller than 400px: */ body { background-repeat: no-repeat; background-image: url('/uploads/images/201712120957521200.jpg'); } /* For device width 400px and larger: */ @media only screen and (min-device-width: 400px) { body { background-image: url('/uploads/images/201712120957521200.jpg'); } } </style> </head> <body> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
HTML5 <picture> 元素
HTML5 的 <picture>
元素可以设置多张图片。
浏览器支持

<picture>
元素类似于 <video>
和<audio>
元素。可以设备不同的资源,第一个设置的资源为首选使用的:
实例
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta charset="utf-8"> <title>php 中文网</title> </head> <body> <picture> <source srcset="/uploads/images/201712120957521200.jpg" media="(max-width: 400px)"> <source srcset="/uploads/images/201712120957521200.jpg"> <img src="/uploads/images/201712120957521200.jpg" alt="Flowers" style="width:auto;"> </picture> <p>调整浏览器宽度和高度,背景在宽度小于 400px 时将改变。 </p> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
srcset
属性的必须的,定义了图片资源。
media
属性是可选的,可以在媒体查询的CSS @media 规则 查看详情。
对于不支持 <picture>
元素的浏览器你也可以定义 <img>
元素来替代。