为什么需要浮动

image.png
image.png
image.png

什么是浮动

image.png

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. .one,
  10. .two {
  11. width: 100px;
  12. height: 100px;
  13. background-color: blue;
  14. float: left;
  15. }
  16. .two {
  17. float: right;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div class="one"></div>
  23. <div class="two"></div>
  24. </body>
  25. </html>

image.png

浮动特性(重难点)

image.png

1浮动元素会脱离标准流(脱标)

视频:https://www.bilibili.com/video/BV1pE411q7FU?p=173
image.png

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. .one {
  10. width: 100px;
  11. height: 100px;
  12. background-color: blue;
  13. float: left;
  14. }
  15. .two {
  16. width: 200px;
  17. height: 200px;
  18. background-color: red;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div class="one">浮动</div>
  24. <div class="two">标准流</div>
  25. </body>
  26. </html>

image.png

2.浮动的元素会一行内显示并且元素顶部对齐

image.png

<!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>
    div {
      width: 300px;
      height: 300px;
      float: left;
      background-color: blue;
    }

    .two,
    .four {
      background-color: red;
    }

    .two {
      height: 400px;
    }
  </style>
</head>

<body>
  <div class="one">1</div>
  <div class="two">2</div>
  <div class="three">3</div>
  <div class="four">4</div>
</body>

</html>

image.png
当我缩小浏览器时
image.png
image.png

3.浮动元素会具有行内块元素特性

视频:https://www.bilibili.com/video/BV1pE411q7FU?p=175&spm_id_from=pageDriver
image.png

<!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>
    span {
      width: 100px;
      height: 100px;
      background-color: blue;
      float: left;
    }

    div {
      height: 200px;
      background-color: red;
      float: right;
    }
  </style>
</head>

<body>
  <span></span>
  <div>111111</div>
</body>

</html>

image.png

浮动元素经常和标准流父级搭配使用

视频:https://www.bilibili.com/video/BV1pE411q7FU?p=176
image.png
解决这样的布局。image.png

<!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>
    .father {
      width: 600px;
      height: 100px;
      background-color: rgb(134, 115, 115);
      margin: 100px auto;
    }

    .father div {
      width: 100px;
      height: 100px;
      background-color: blue;
      float: left;
    }
  </style>
</head>

<body>
  <div class="father">
    <div>1</div>
    <div>2</div>
    <div>3</div>
  </div>
</body>

</html>

效果图
image.png