页面布局 4个盒子间隔10px水平平均分布

在pc端中,要写4个盒子,每个盒子间隔10px,水平平均发布,,试了多种方法都达不到效果,求助

阅读 10.8k
5 个回答

仿bootstrap

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>

    <style>
        body{
            margin: 0;
            overflow: hidden;
        }
        
        .row{
            margin-left: -5px;
            margin-right: -5px;
            overflow: hidden;
        }

        .col{
            width: 25%;
            padding: 0 5px;
            float: left;
            box-sizing: border-box;
        }

        .box{
            height: 100px;
            background: red;
        }
    </style>
</head>
<body>
    <div class="row">
        <div class="col">
            <div class="box"></div>
        </div>
        <div class="col">
            <div class="box"></div>
        </div>
        <div class="col">
            <div class="box"></div>
        </div>
        <div class="col">
            <div class="box"></div>
        </div>
    </div>
</body>
</html>

.wrap{ovverflow:hidden;width:100%}
.wrap div{width:calu(100% - 30px)/4; float:left;height:100px}
.wrap div+div{margin-left:10px;}
标签结构自己写吧

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <style>
            *{
                margin:0;
                padding:0;
            }
            
            html,body{
                width:100%;
                height:100%;
            }
            
            .wrap{ovverflow:hidden;width:100%}
            .wrap div{ background-color:yellow;
                        width:calc( (100% - 30px)/4); 
                        float:left;height:100px}
            .wrap div+div{margin-left:10px;}
            
        </style>
    </head>
    <body>
        <div class="wrap">
            <div></div>
            <div></div>
            <div></div>
            <div></div>
        </div>
    </body>
</html>

使用弹性盒模型应该可以解决

在PC端不要求兼容性的情况下可以使用弹性盒子模型flex,或者栅格布局grid;在要求兼容性的情况下可以设置外层div margin:0 auto,内层四个div display:inline-block并margin:10px

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题