好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

用css实现正方形div 的两种方法

目标:实现一个正方形,这个正方形边长等于

方法一:使用单位vw, (ps我 觉得 这个是最 简单 的方法)

ht ML 结构也很简单,只有一个div即可

<html>
<body>
    <div class="square">
   </div>
</body>
</html>

.square{
  width: 50vw;
  h ei ght: 50vw;
  background: blue;  
}

方法二: 使用padding-bottom

要点:
&nbs p;

1.height: 0, 内容会溢出到padding里, 不用 担心~~
 

2.padding-bottom 值设置为百分比时候,相对于包含块的 宽 度。
 

3.需要设置包含块

html结构:

<html>
<body>
    <div class="cont ai ner">
       <div class="square">
       </div>
   </div>
</body>
</html>

css:

*{
        m arg in: 0;
    }
    /* 设置撑满页面可见区域 */
    .container{
        height: 100vh; 
        width: 100vw;
        background: palegoldenrod;
    }
    .square{
        width: 50%; /* 相对与container的width */
        padding-bottom: 50%;  /* 相对与container的width */
        background: palegreen;
    }

行了吧,两种就可以了,你也可以使 用M ar gin , 不过 会有塌陷的危险,所以,就这两个够用啦~~
 

总结

以上所述是小编给大家介绍的用css实现正方形div 的两种方法, 希望对大家有所帮助 ,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

总结

以上是 为你收集整理的 用css实现正方形div 的两种方法 全部内容,希望文章能够帮你解决 用css实现正方形div 的两种方法 所遇到的问题。

如果觉得 网站内容还不错, 推荐好友。

查看更多关于用css实现正方形div 的两种方法的详细内容...

  阅读:29次