10-浮动

128 阅读1分钟

浮动的代码

  • 属性名:float
  • 属性值:
属性名效果
left左浮动
right右浮动

浮动的特点

  1. 浮动元素会脱离标准流,在标准流中不占位置(相当于飘起来了)
  2. 浮动元素比标准流高半个级别,可以覆盖标准流中的元素
  3. 浮动找浮动,下一个浮动元素会在上一个浮动元素后面左右浮动
  4. 浮动元素会受到上面元素边界的影响
  5. 浮动元素有特殊的显示效果

一行可以显示多个 可以设置宽高

  1. 浮动元素不能通过text-align:center或者margin:0 auto,让浮动元素本身水平居中

网页布局浮动案例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        /* 去除标签默认的margin和padding */
        * {
            margin: 0;
            padding: 0;
        }
        .header { 
            height: 40px;
            background-color: #333;
        }
        .nav {
            height: 100px;
            width: 1226px;
            background-color: #ffc0cb;
            /* 盒子居中 */
            margin: 0 auto;
        }
        .banner {
            height: 460px;
            width: 1226px;
            background-color: skyblue;
            /* 盒子居中 */
            margin: 0 auto;
        }
        .left {
            width: 234px;
            height: 460px;
            background-color: #ffa500;
            /* 左浮动, (它向左浮动,右边的盒子才能浮动上来挨着)*/
            float: left;
        }
        .right  {
            width:992px ;
            height: 460px;
            background-color: #87ceeb;
            /* 右浮动,它在大盒子中浮动在右边 */
            float: right;
            
        }
    </style>
</head>
<body>
    <!-- 网页头部 -->
    <div class="header"></div>
    <!-- 网页的导肮 -->
    <div class="nav"></div>
    <!-- 网页轮播图部分 -->
    <div class="banner">
        <div class="left"></div>
        <div class="right"></div>
    </div>
</body>
</html>

在这里插入图片描述


小米模块案例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 1226px;
            height: 614px;
            
            margin: 0 auto;
        }
        .left {
            height: 614px;
            width: 234px;
            background-color: #800080;
            float: left;
        }
        .right1 {
            width:234px ;
            height: 300px;
            background-color: #87ceeb;
            float: left;
            margin:0px 14px 14px 14px;
        }
        .right2 {
            width:234px ;
            height: 300px;
            background-color: #87ceeb;
            float: left;
            margin:0px 14px 14px 0px;
        }
        .right3 {
            width:234px ;
            height: 300px;
            background-color: #87ceeb;
            float: left;
            margin:0px 14px 14px 0px;
        }
        .right4 {
            width:234px ;
            height: 300px;
            background-color: #87ceeb;
            float: left;
            margin:0px 0px 14px 0px;
        }
        .right5 {
            width:234px ;
            height: 300px;
            background-color: #87ceeb;
            float: left;
            margin:0px 14px 0px 14px;
        }
        .right6 {
            width:234px ;
            height: 300px;
            background-color: #87ceeb;
            float: left;
            margin:0px 14px 0px 0px;
        }
        .right7 {
            width:234px ;
            height: 300px;
            background-color: #87ceeb;
            float: left;
            margin:0px 14px 0px 0px;
        }
        .right8 {
            width:234px ;
            height: 300px;
            background-color: #87ceeb;
            float: left;
            margin:0px 0px 0px 0px;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="left"></div>
        <div class="right1"></div>
        <div class="right2"></div>
        <div class="right3"></div>
        <div class="right4"></div>
        <div class="right5"></div>
        <div class="right6"></div>
        <div class="right7"></div>
        <div class="right8"></div>
    </div>
</body>
</html>

在这里插入图片描述


网页导航案例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        /* 清楚默认 */
        * {
            margin: 0;
            padding: 0;
        }
      ul li {
        float: left;
        list-style: none;
      }
      ul li a {
        /* display: inline-block; */
        float: left;
        width: 80px;
        height: 50px;
        background-color: #ffc0cb;
        text-align: center;
        line-height: 50px;
        text-decoration: none;
        color: #fff;
        font-size: 16px;
      }
      ul li a:hover {
        background-color: #008000;
        color: white;
      }
    </style>
</head>
<body>
    <ul>
        <li><a href="#">新闻1</a></li>
        <li><a href="#">新闻2</a></li>
        <li><a href="#">新闻3</a></li>
        <li><a href="#">新闻4</a></li>
        <li><a href="#">新闻5</a></li>
        <li><a href="#">新闻6</a></li>
        <li><a href="#">新闻7</a></li>
        <li><a href="#">新闻8</a></li>
    </ul>
</body>
</html>

在这里插入图片描述