<!DOCTYPE html>
<html>
<head>
<title>div或img图片高度随宽度自适应</title>
<style type="text/css">
.box{
width: 50%;
margin: 50px auto;
}
.img-box{
width: 100%;
position:relative;
z-index:1;
}
.img-box img{
position:absolute;
top:0;
bottom:0;
left:0;
right:0;
width:100%;
margin:auto;
z-index: -1;
*zoom:1;
}
.img-box:before {
content: "";
display: inline-block;
padding-bottom: 100%;
width: 0.1px; /*必须要有数值,否则无法把高度撑起来*/
vertical-align: middle;
}
</style>
</head>
<body>
<div class="box">
<span>行内元素垂直居中</span>
<div class="img-box">
<img src="http://www.desktopwallpaperhd.net/wallpapers/10/d/dongwushijie-animal-tupian-107405.jpg"/>
</div>
</div>
</body>
</html>
div或img图片高度随宽度自适应
最新推荐文章于 2024-06-11 16:46:17 发布