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 @   盘思动  阅读(1130)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2019-10-21 SQL语句 update 字段=字段+字符串 拼接
点击右上角即可分享
微信分享提示