首页 > Web开发 > 详细

css实现正方形div的3种方式

时间:2017-12-13 01:38:44      阅读:317      评论:0      收藏:0      [点我收藏+]

效果图:

技术分享图片

方法一:

使用vw单位

<div class="demo">viewport</div>
<style>
.demo{
         width: 20%;
         height: 20vw;
         background: #ccc;
       }
</style>

方法二:padding-bottom

 <div class="demo2"></div>
 .demo2{
        width: 100%;
        padding-bottom: 100%;/* padding百分比相对父元素宽度计算 */
        height: 0;//避免被内容撑开多余的高度
    }

方法三:padding-bottom+:after+absolute

<div class="demo3">

</div>
    .demo3{
            width: 30%;
            background: #ccc;
            position: relative;
        }

        .demo3:before {
            content: "";
            display: block;
            padding-bottom: 100%;
        }
        .demo3:after{
            content: "viewport";
            position: absolute;
            top: 50%;
            left: 0;
            width: 100%;
            height: 50%;
            text-align: center;
            display: block;

        }

 

css实现正方形div的3种方式

原文:http://www.cnblogs.com/zjy1017/p/8030412.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!