CSS浮动(常用的页面布局和浮动布局注意点)

常见页面

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        .top {
            height: 50px;
            background-color: gray;
        }
        
        .banner {
            width: 980px;
            height: 150px;
            background-color: gray;
            margin: 10px auto;
        }
        
        .box {
            width: 980px;
            height: 300px;
            margin: 0 auto;
            background-color: white;
        }
        
        li {
            list-style: none;
        }
        
        .box li {
            float: left;
            width: 237px;
            height: 300px;
            margin-right: 10px;
            background-color: gray;
        }
        
        .box .last {
            margin-right: 0;
        }
        
        .footer {
            height: 200px;
            background-color: gray;
            margin-top: 10px;
        }
    </style>
</head>

<body>
    <div class="top"></div>
    <div class="banner"></div>
    <div class="box">
        <ul>
            <li></li>
            <li></li>
            <li></li>
            <li class="last"></li>
        </ul>
    </div>
    <div class="footer"></div>
</body>

</html>

浮动布局注意点

1. 浮动和标准流的父盒子搭配 : 先用标准流的父元素排列上下位置, 之后内部子元素采取浮动排列左右位置
2. 浮动的盒子只会影响浮动盒子后面的标准流, 不会影响前面的标准流

如果第一个盒子不浮动, 第二个盒子浮动, 会发生什么情况

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .d1 {
            height: 200px;
            width: 200px;
            background-color: pink;
        }
        
        .d2 {
            float: left;
            height: 200px;
            width: 200px;
            background-color: navajowhite;
        }
    </style>
</head>

<body>
    <div class="d1"></div>
    <div class="d2"></div>
</body>

</html>

在这里插入图片描述

因为第一个盒子没有浮动. 是标准流 , 独占一行, 所以第二个盒子会在第二行显示

三级目录

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值