1.float原理

所谓float就是元素相对于页面漂浮起来
问题:对后面的元素造成了影响 解决:给受到影响的元素添加clear:both;

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. /*
  9. float原理:所谓float就是元素相对于页面漂浮起来
  10. 问题:对后面的元素造成了影响 解决:给受到影响的元素添加clear:both;
  11. */
  12. .one{
  13. width:100px;
  14. height: 100px;
  15. background-color: red;
  16. float:left;
  17. }
  18. .two{
  19. width:200px;
  20. height: 200px;
  21. background-color:yellow;
  22. float: left;
  23. }
  24. .three{
  25. width:300px;
  26. height: 300px;
  27. background-color: green;
  28. clear: both;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <div class="one">
  34. </div>
  35. <div class="two">
  36. </div>
  37. <div class="three">
  38. </div>
  39. </body>
  40. </html>

2.float目的

并排显示元素

<!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}
        li{
            float:left;
            list-style:none;
            width:100px;
            text-align: center;
        }
        ul{
            overflow: hidden;
            line-height: 50px;
            background-color: pink;
        }
    </style>
</head>
<body>
    <!-- 为了让元素并排显示 -->
    <ul>
        <li>新闻</li>
        <li>地图</li>
        <li>hao123</li>
    </ul>
</body>
</html>

3.float关系

1影响后面的元素 解决方案:clear:both
2.子元素float,父元素的高度坍塌了

<!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>
        /*1影响后面的元素 解决方案:clear:both
          2.子元素float,父元素的高度坍塌了
        */
        * {
            margin: 0;
            padding: 0
        }    

        .parent {
            width: 200px;
            background-color: red;
        }

        .child {
            width: 100px;
            height: 100px;
            background-color: yellow;
            float: left;
        }
    </style>
</head>

<body>
    <div class="parent">
        <div class="child"></div>
    </div>
</body>

</html>

4.float 宽度自适应布局

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    body{
        min-width: 550px;
    }
    .parent{
        padding-left: 100px;
        padding-right: 200px;
        overflow: hidden;
    }
    .parent div{
        box-sizing: border-box;
        border:1px solid #333;
        float: left;
    }
    .left{
        margin-left: -100px;
        width: 100px;
    }
    .center{
        width: 100%;
    }
    .right{
        margin-right: -200px;
        width: 200px;
    }
</style>
<body>
    <div class="parent">
        <div class="left">left</div>
        <div class="center">center</div>
        <div class="right">right</div>
    </div>
</body>
</html>

1.PNG5.float隐藏

<!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>
        .nav::after {
            content: "";
            display: table;
            clear: both;
        }

        .nav {
            width: 1080px;
            margin: 0 auto;
        }

        ul {
            list-style: none;
            background-color: pink;
            line-height: 50px;
        }

        li {
            float: left;
            width: 100px;
            text-align: center;
        }

        li:hover {
            background-color: rgb(133, 127, 130);
        }

        .drop-menu {
            position: relative;
        }

        .drop-content {
            display: none;
            width: 100px;
            position: absolute;
            background-color: deeppink;
        }

        .drop-menu:hover .drop-content {
            display: block;
        }
    </style>
</head>

<body>
    <ul class="nav">
        <li class="drop-menu">
            收藏夹
            <div class="drop-content">
                <p>收藏宝贝</p>
                <P>收藏店铺</p>
            </div>
        </li>
        <li>卖家中心</li>
        <li>联系客服</li>
    </ul>

</body>

</html>

6.float伪元素

1.给爹overflow:hidden;
2.用伪元素解决 .parent::after{content:””;display:table;clear:both}

<!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>
        /*
        1.给爹overflow:hidden;
        2.用伪元素解决 .parent::after{content:"";display:table;clear:both}
        */
        .parent{
            width:200px;
            background-color: yellow;
        }
        .child{
            width: 100px;
            height: 100px;
            background-color: red;
            float:left;
        }
        .parent::after{
            content:"";
            display:table;
            clear:both;
        }
    </style>
</head>

<body>
    <div class="parent">
        <div class="child"></div>
    </div>
</body>

</html>