实现图片响应式

图片随着屏幕的大小而变化,同事又保证图片不失真.

方法一:

注意加视口

img {
 max-width:100%;
 height:auto;
}

方法二:使用背景图片 background-size:contain属性;然而这种方法并不是真正的响应式,可以使用媒体查询实现响应式.

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
	<title>Document</title>
	<style type="text/css">
        div {
            width: 100%;
            height: 400px;
            background-image: url('http://c.hiphotos.baidu.com/image/h%3D300/sign=e3b76ad36081800a71e58f0e813433d6/d50735fae6cd7b89acbea9df032442a7d8330e9f.jpg');
            background-repeat: no-repeat;
            background-size: contain;
            border: 1px solid red;
        }
	</style>
</head>
<body>
	<div></div>			
</body>
</html>

方法三:使用H5中的picture标签

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
	<title>Document</title>
	<style type="text/css">
       
         img {display: block; margin: 0 auto;}
	</style>
</head>
<body>
	<picture>
      <source media="(min-width: 650px)" srcset="http://c.hiphotos.baidu.com/image/h%3D300/sign=e3b76ad36081800a71e58f0e813433d6/d50735fae6cd7b89acbea9df032442a7d8330e9f.jpg">
      <source media="(min-width: 465px)" srcset="http://h.hiphotos.baidu.com/image/h%3D300/sign=15d5dd5200d79123ffe092749d355917/48540923dd54564e4d0d6963bfde9c82d1584f04.jpg">
      <img src="http://f.hiphotos.baidu.com/image/h%3D300/sign=04083ce4fa03738dc14a0a22831bb073/08f790529822720eb1faa1be77cb0a46f21fabba.jpg" alt="a cute kitten">
    </picture>
</body>
</html>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值