浮动模块布局

基本思路

若宽度和浏览器一样宽,则不需要设置width

一般父盒子使用标准流,然后标准流内使用浮动

一般父盒子需要居中显示,使用 margin: 0 auto;

注意浮动盒子之间的margin值 与 父盒子width、height值之间的相等关系,一定要计算好!

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>浮动布局</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
 
        .top {
            /* width和浏览器默认的一致,不用设置 */
            height: 50px;
            margin-top: 20px;
            background-color: pink;
        }
 
        .banner {
            height: 120px;
            width: 1010px;   /*设置快读*/
            margin: 10px auto;  /*居中*/
            background-color: green;
        }
 
        .box {
            height: 300px;
            width: 1010px;
            margin: 10px auto;
            background-color: lightblue;
        }
 
        .box>.box1 {
            float: left;
            margin: 10px;
        }
 
        .box>div {
            float: left;
            background-color: pink;
            margin: 10px;
            margin-left: 0;
            height: 280px;
            width: 240px;
        }
 
        .footer {
            height: 300px;
            background-color: aquamarine;
        }
    </style>
</head>
<body>
    <div class="top">top</div>
    <div class="banner">banner</div>
    <div class="box">
        <div class="box1">box1</div>
        <div class="box2">box2</div>
        <div class="box3">box3</div>
        <div class="box4">box4</div>
    </div>
    <div class="footer">footer</div>
</body>
</html>
相关推荐
阿星AI工作室22 分钟前
一个简单Demo彻底理解前后端怎么连的丨Figma + Supabase + Vercel
前端·人工智能
aircrushin30 分钟前
一拍即传的平替,完全免费的实时照片墙!
前端
鹏北海2 小时前
JSBridge 原理详解
前端
孟健3 小时前
我的网站被黑了:一天灌入 227 万条垃圾数据,AI 写的代码差点让我社死
前端
anOnion3 小时前
构建无障碍组件之Checkbox pattern
前端·html·交互设计
IT枫斗者4 小时前
IntelliJ IDEA 2025.3史诗级更新:统一发行版+Spring Boot 4支持,这更新太香了!
java·开发语言·前端·javascript·spring boot·后端·intellij-idea
N***p3655 小时前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
NGC_66115 小时前
二分查找算法
java·javascript·算法
享誉霸王6 小时前
15、告别混乱!Vue3复杂项目的规范搭建与基础库封装实战
前端·javascript·vue.js·前端框架·json·firefox·html5
a1117766 小时前
飞机躲避炸弹 网页游戏
前端·开源·html·threejs