美文网首页
图片宽度设为百分比的情况下让图片宽高相等

图片宽度设为百分比的情况下让图片宽高相等

作者: 王远清orz | 来源:发表于2019-05-17 14:26 被阅读0次

html代码

<div class='box'>
    <img src="...">
</div>

CSS代码

父元素设置 
    .box {    
    position: relative;   
    width: 50%;
    }
   .box:before {       
    content: "";    
    display: block;    
    padding-top: 100%;
   }
或者 
.box{
    position: relative;
    width: 50%;
    height: 0;
    padding-bottom: 50%;
}
图片.box img {
    position:  absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

效果图

213.jpg

参考:https://blog.csdn.net/jinwangxp/article/details/80003956

相关文章

网友评论

      本文标题:图片宽度设为百分比的情况下让图片宽高相等

      本文链接:https://www.haomeiwen.com/subject/rjxoaqtx.html