1. 浮动的简介

通过浮动可以使一个元素向其父元素的左侧或右侧移动
使用float属性来设置于元素的浮动

  • none 默认值,元素不浮动
  • left 元素向左浮动
  • right 元素向右浮动
  • 元素设置浮动以后,会完全从文档流中脱离,所以元素下边的还在文档流中的元素会自动向上移动,因此水平布局的等式便不需要强制成立

2. 浮动的特点

  1. 浮动元素会完全脱离文档流,不再占据文档流中的位置
  2. 设置浮动以后,元素会向父元素的左侧或右侧移动 (浮动元素默认不会从父元素中移出 ) ```html

  1. ![](https://gitee.com/vectorx/ImageCloud/raw/master/html5/20210524230844.png#clientId=uee1345c6-9f63-4&crop=0&crop=0&crop=1&crop=1&height=46&id=z08Ri&originHeight=220&originWidth=234&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=u12ce2e60-a6ea-4034-a87a-47ce4112bda&title=&width=49)
  2. 4. 浮动元素向左或向右移动时,不会超过前边的浮动元素(先来后到的顺序)
  3. ```html
  4. <style>
  5. .box1 {
  6. width: 200px;
  7. height: 200px;
  8. background-color: orange;
  9. float: left;
  10. }
  11. .box2 {
  12. width: 200px;
  13. height: 200px;
  14. background-color: red;
  15. float: left;
  16. }
  17. .box3 {
  18. width: 200px;
  19. height: 200px;
  20. background-color: yellow;
  21. float: left;
  22. }
  23. </style>
  24. <div class="box1"></div>
  25. <div class="box2"></div>
  26. <div class="box3"></div>
  1. ![](https://gitee.com/vectorx/ImageCloud/raw/master/html5/20210524200928.png#clientId=uee1345c6-9f63-4&crop=0&crop=0&crop=1&crop=1&height=30&id=Moqdy&originHeight=261&originWidth=769&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=u9570556d-37b7-495f-8453-59d393c7943&title=&width=87)
  1. 浮动元素不会超过浮动的兄弟元素从而上移 ,最多就是和它一样高


07.浮动(相当于新建立了一个浮动层) - 图1

  1. 如果浮动元素的上边是一个没有浮动的块元素,则浮动元素无法上移 ```html

  1. ![](https://gitee.com/vectorx/ImageCloud/raw/master/html5/20210524201107.png#clientId=uee1345c6-9f63-4&crop=0&crop=0&crop=1&crop=1&height=32&id=GIjnX&originHeight=415&originWidth=210&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=ue17446af-025d-4515-b1e1-feab2580f97&title=&width=16)
  2. 7. 浮动元素不会盖住文字,文字会自动环绕在浮动元素的周围,所以我们可以利用浮动来设置文字环绕图片的效果<br />![](https://gitee.com/vectorx/ImageCloud/raw/master/html5/20210524203901.png#clientId=uee1345c6-9f63-4&crop=0&crop=0&crop=1&crop=1&height=72&id=asULf&originHeight=495&originWidth=599&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=uafc628df-5fb9-4c01-8e74-43138fae20d&title=&width=87)
  3. 简单总结:
  4. - 浮动目前来讲它的主要作用就是让页面中的元素可以水平排列,通过浮动可以制作一些水平方向的布局
  5. - 元素设置浮动以后,将会从文档流中脱离,从文档流中脱离后,元素的一些特点也会发生变化
  6. <a name="fbab0858"></a>
  7. ## 3. 脱离文档流的特点
  8. 块元素:(块元素不能的特点 变成可以--> 可以并排在一行)
  9. - 块元素不再独占页面的一行
  10. - 脱离文档流以后,块元素的宽度和高度默认都被内容撑开
  11. ```html
  12. <style>
  13. .box1 {
  14. background-color: orange;
  15. /* float: left; */
  16. }
  17. </style>
  18. <div class="box1">hello</div>

07.浮动(相当于新建立了一个浮动层) - 图2

行内元素:

  • 行内元素脱离文档流以后会,特点和块元素一样 ```html

I am a Span

  1. ![](https://gitee.com/vectorx/ImageCloud/raw/master/html5/20210524204755.gif#clientId=uee1345c6-9f63-4&crop=0&crop=0&crop=1&crop=1&height=357&id=ja56q&originHeight=665&originWidth=1237&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=u9b7510e1-67c0-4498-b531-b8403b81515&title=&width=665)
  2. 脱离文档流之后的特点很像行内块元素,不过存在一些差异
  3. ```html
  4. <style>
  5. span {
  6. width: 200px;
  7. height: 200px;
  8. background-color: orange;
  9. /* display: inline-block; */
  10. float: left;
  11. }
  12. </style>
  13. <span>I am a Span</span>
  14. <span>I am a Span</span>

07.浮动(相当于新建立了一个浮动层) - 图3
总结:变成浮动元素以后(脱离文档流)— 将不再区分块元素和行元素

4. 简单布局

整体样式
07.浮动(相当于新建立了一个浮动层) - 图4

目的

  1. 熟悉布局(块元素、浮动)
  2. 公共css部分复用
  3. 复习语义标签

代码
html代码

  1. <!-- 页眉 -->
  2. <header></header>
  3. <!-- 主体 -->
  4. <main>
  5. <!-- 左边栏 -->
  6. <nav></nav>
  7. <!-- 中心 -->
  8. <article>
  9. <!-- 内容上 -->
  10. <div class="top"></div>
  11. <!-- 内容下 -->
  12. <div class="bottom">
  13. <!-- 内容左 -->
  14. <div class="left"></div>
  15. <!-- 内容中 -->
  16. <div class="middle"></div>
  17. <!-- 内容右 -->
  18. <div class="right"></div>
  19. </div>
  20. </article>
  21. <!-- 右边栏 -->
  22. <aside></aside>
  23. </main>
  24. <!-- 页脚 -->
  25. <footer></footer>

css代码

  1. /* 公共部分 */
  2. header,
  3. main,
  4. footer {
  5. width: 1000px;
  6. margin: 10px auto;
  7. }
  8. main nav,
  9. main article,
  10. main aside {
  11. float: left;
  12. /* 虽然设置浮动了,但整体大小是被内容撑开的,所以设置一个高度 */
  13. height: 100%;
  14. }
  15. .bottom .left,
  16. .bottom .middle,
  17. .bottom .right {
  18. float: left;
  19. width: 220px;
  20. height: 100%;
  21. }
  22. /* ==========整体布局-上========== */
  23. header {
  24. height: 100px;
  25. background-color: silver;
  26. }
  27. /* ==========整体布局-中========== */
  28. main {
  29. height: 400px;
  30. background-color: #bfa;
  31. }
  32. /* ------左边栏------ */
  33. main nav {
  34. width: 150px;
  35. background-color: red;
  36. }
  37. /* ------中心------ */
  38. main article {
  39. width: 680px;
  40. background-color: green;
  41. margin: 0 10px;
  42. }
  43. /* ---上--- */
  44. article .top {
  45. height: 190px;
  46. background-color: yellow;
  47. margin-bottom: 10px;
  48. }
  49. /* ---下--- */
  50. article .bottom {
  51. height: 200px;
  52. background-color: orange;
  53. }
  54. /* 左 */
  55. .bottom .left {
  56. background-color: lightblue;
  57. }
  58. /* 中 */
  59. .bottom .middle {
  60. background-color: gray;
  61. margin: 0 10px;
  62. }
  63. /* 右 */
  64. .bottom .right {
  65. background-color: wheat;
  66. }
  67. /* ------右边栏------ */
  68. main aside {
  69. width: 150px;
  70. background-color: blue;
  71. }
  72. /* ==========整体布局-下========== */
  73. footer {
  74. height: 100px;
  75. background-color: tomato;
  76. }

效果
07.浮动(相当于新建立了一个浮动层) - 图5
总结:

  • 需要竖着排列就写块元素;
  • 需要横着的布局,就写块元素 然后用浮动float达到横向布局;
  • 公共部分需要提炼出来

5. 练习:w3school导航条

07.浮动(相当于新建立了一个浮动层) - 图6

去除默认样式,引入reset.css

  1. <link rel="stylesheet" href="css/reset.css">

css样式

  1. /* 去除默认样式 */
  2. a {
  3. text-decoration: none;
  4. }
  5. /* ul整体布局 */
  6. .menu {
  7. width: 1211px;
  8. height: 48px;
  9. background-color: #E8E7E3;
  10. margin: 100px auto;
  11. }
  12. /* li整体布局 */
  13. .nav {
  14. /* 浮动li元素 */
  15. /*给谁设置浮动就看谁垂直排列*/
  16. float: left;
  17. width: 173px;
  18. /*文字在父元素当中垂直居中*/
  19. line-height: 48px;
  20. }
  21. .nav a {
  22. /* 注意点:升级为块元素,使之继承父类宽高
  23. 否则鼠标悬浮在li元素上时,鼠标“箭头”不会进入a元素变成“小手” */
  24. /*a超链接行元素宽高被内容撑开,转换成块元素以后,宽继承父 高自动匹配*/
  25. display: block;
  26. /* 内容水平居中 */
  27. text-align: center;
  28. /* 字体样式 */
  29. font-size: 14px;
  30. color: #777777;
  31. font-family: Verdana, Arial, '微软雅黑', '宋体';
  32. }
  33. /* 超链接悬浮效果 */
  34. .nav a:hover {
  35. background-color: #3F3F3F;
  36. color: #E8E7E3;
  37. }

html代码

  1. <ul class="menu">
  2. <li class="nav"><a href="#">HTML/CSS</a></li>
  3. <li class="nav"><a href="#">Browser Side</a></li>
  4. <li class="nav"><a href="#">Server Side</a></li>
  5. <li class="nav"><a href="#">Programming</a></li>
  6. <li class="nav"><a href="#">XML</a></li>
  7. <li class="nav"><a href="#">Web Building</a></li>
  8. <li class="nav"><a href="#">Reference</a></li>
  9. </ul>

效果
07.浮动(相当于新建立了一个浮动层) - 图7
总结:

  • /a超链接行元素宽高被内容撑开,转换成块元素以后,宽继承父 高自动匹配/

    1. display: block;
  • / 内容水平居中 / text-align: center;

  • font-family: Verdana, Arial, ‘微软雅黑’, ‘宋体’;
  • /给谁设置浮动就看谁垂直排列/ float: left;