css 实现宽高相等

  • demo 1
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>css设置高和宽相等~~</title>
        <style>
            *{
                margin: 0;
                padding: 0;
            }
            .box{
                width: 300px;/* 也可以用20% */
                position:relative;
                background-color: aquamarine;   
            }
            .box:before{
                content: '';
                padding-top: 100%;
                box-sizing: border-box;
                display: block;
                width: 0;  
            }
            .pox{
               position:absolute;
               height:100%;
               width:100%;
               left: 0;
               top: 0;         
               border: 1px solid red;
               box-sizing: border-box;
               /* border-radius: 500px; */
            }
        </style>
    </head>
    <body>
        <div class="box">
            <div class="pox">内容</div>
        </div>
    </body>
</html>

  • demo2
<!DOCTYPE html>
<html>

<head>
    <title>test</title>
</head>
<style>
    .wrapper {
        position: relative;
        height: 0;
        padding-top: 20%;
        background-color: brown;
    }
    .box {
        position: absolute;
        width: 20%;
        height: 100%;
        top: 0;
        border: 1px solid #ccc;
        background-color: aquamarine;
    }
</style>

<body>
    <div class="wrapper">
        <div class="box"></div>
    </div>
</body>
</html>

posted @ 2022-10-21 17:10  盘思动  阅读(1081)  评论(0编辑  收藏  举报