移動端布局,div并列布局永遠是正方形,寬度不指定

移動端布局,div并列布局永遠是正方形,寬度不指定

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, inital-scale=1.0">
  <title>Document</title>
    <style>
        .container{
            width: 400px; /*寬度自適應*/
            height: 600px;
            border: 1px solid red;
        }
        .inner{
            position: relative;
            width: 100%;
            padding-bottom: 50%;
        }
        .box{
            position: absolute;
            width: 100%;
            height: 100%;
        }
        .wrapper:nth-child(odd){
            width: 50%;
            height: 100%;
            background-color: blueviolet;
            float: left;
        }
        .wrapper:nth-child(even){
            width: 50%;
            height: 100%;
            background-color: blue;
            float: left;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="inner">
            <div class="box">
                <div class="wrapper"></div>
                <div class="wrapper"></div>
            </div>
        </div>
    </div>
</body>


巧妙使用padding-bottom來撐高度,padding的百分比相對于父級的width來計算的,然后在通過position相對定位撐開高度

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容