CSS 布局 - 浮动实例

本页提供常见的浮动案例。

网格 / 等宽的框

css_float_boxes.htm

通过使用 float 属性,可以轻松地并排浮动内容框:

实例

  1. * {
  2. box-sizing: border-box;
  3. }
  4. .box {
  5. float: left;
  6. width: 33.33%; /* 三个框(四框使用 25%,两框使用 50%,以此类推) */
  7. padding: 50px; /* 如果需要在图片间增加间距 */
  8. }

什么是 box-sizing?

您可以轻松地并排创建三个浮动框。但是,当您添加一些内容来扩大每个框的宽度(例如,内边距或边框)时,这个框会损坏。 box-sizing 属性允许我们在框的总宽度(和高度)中包括内边距和边框,确保内边距留在框内而不会破裂。

您可以在我们的 CSS Box Sizing 这一章中学习有关 box-sizing 属性的更多知识。

图像并排

css_float_images_side.htm

这种框格(The grid of boxes)也可以用来并排显示图像:

实例

  1. .img-container {
  2. float: left;
  3. width: 33.33%; /* 三个框(四框使用 25%,两框使用 50%,以此类推) */
  4. padding: 5px; /* 如果需要在图片间增加间距 */
  5. }

等宽的框

在上例中,您学习了如何以相等的宽度并排浮动框。但是,创建具有相同高度的浮动框并不容易。不过,快速解决方案是设置一个固定的高度,如下例所示:

css_float_boxes_height.htm

实例

  1. .box {
  2. height: 500px;
  3. }

但是,这么做就失去了弹性。如果可以保证框中始终有相同数量的内容,那是可以的。但是很多时候,内容是不一样的。如果您在手机上尝试上例,则会看到第二个框的内容将显示在框的外部。这是 CSS3 Flexbox 派上用场的地方 - 因为它可以自动拉伸框使其与最长的框一样长:

实例

使用 Flexbox 创建弹性框:

css_float_boxes_flex.htm

Flexbox 的唯一问题是它在 Internet Explorer 10 或更早版本中不起作用。您可以在我们的 CSS Flexbox 章节中学习有关 Flexbox 布局模块的更多知识。

导航菜单

float 与超链接列表一起使用,来创建水平菜单:

实例

css_float_navigation_menu.htm

Web 布局实例

使用 float 属性完成整个 Web 布局也很常见:

css_layout_cols.htm

实例

  1. .header, .footer {
  2. background-color: grey;
  3. color: white;
  4. padding: 15px;
  5. }
  6. .column {
  7. float: left;
  8. padding: 15px;
  9. }
  10. .clearfix::after {
  11. content: "";
  12. clear: both;
  13. display: table;
  14. }
  15. .menu {
  16. width: 25%;
  17. }
  18. .content {
  19. width: 75%;
  20. }

更多实例

带有边框和外边距的图像浮动到段落的右侧

让图像浮动到段落的右侧。在图像上添加边框和外边距。

  1. <head>
  2. <style>
  3. img {
  4. float: right;
  5. border: 1px dotted black;
  6. margin: 0px 0px 15px 20px;
  7. }
  8. </style>
  9. </head>
  10. <body>
  11. <p>
  12. 在下面的段落中,图像将向右浮动。黑色的虚线边框被添加到图像。
  13. 我们还为图像添加了外边距以将文本推离图像:
  14. 图像的上、右外边距为 0px,下外边距为 15px,左外边距为 20px。
  15. </p>
  16. <p><img src="/uploads/i/css/pineapple.jpg" width="188" height="267">
  17. This is some text. This is some text. This is some text.
  18. This is some text. This is some text. This is some text.
  19. This is some text. This is some text. This is some text.
  20. This is some text. This is some text. This is some text.
  21. This is some text. This is some text. This is some text.
  22. This is some text. This is some text. This is some text.
  23. This is some text. This is some text. This is some text.
  24. This is some text. This is some text. This is some text.
  25. This is some text. This is some text. This is some text.
  26. This is some text. This is some text. This is some text.
  27. This is some text. This is some text. This is some text.
  28. This is some text. This is some text. This is some text.
  29. This is some text. This is some text. This is some text.
  30. </p>
  31. </body>

带标题的图像浮动到右侧

让带有标题的图像向右浮动。

  1. <head>
  2. <style>
  3. div {
  4. float: right;
  5. width: 120px;
  6. margin: 0 0 15px 20px;
  7. padding: 15px;
  8. border: 1px solid black;
  9. text-align: center;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <div>
  15. <img src="/uploads/i/css/pineapple.jpg" width="188" height="267"><br>CSS is fun!
  16. </div>
  17. <p>
  18. This is some text. This is some text. This is some text.
  19. This is some text. This is some text. This is some text.
  20. This is some text. This is some text. This is some text.
  21. This is some text. This is some text. This is some text.
  22. This is some text. This is some text. This is some text.
  23. This is some text. This is some text. This is some text.
  24. This is some text. This is some text. This is some text.
  25. This is some text. This is some text. This is some text.
  26. This is some text. This is some text. This is some text.
  27. This is some text. This is some text. This is some text.
  28. This is some text. This is some text. This is some text.
  29. This is some text. This is some text. This is some text.
  30. This is some text. This is some text. This is some text.
  31. </p>
  32. <p>
  33. 在上面的段落中,div 元素的宽度为 120 像素,其中包含图像。
  34. div 元素将向右浮动。外边距被添加到 div 以将文本推离 div。
  35. 将边框和内边距(填充)添加到 div 中,以凸显图片和标题。
  36. </p>
  37. </body>

让段落的第一个字母向左浮动

让段落的第一个字母向左浮动并设置该字母的样式。

  1. <head>
  2. <style>
  3. span {
  4. float: left;
  5. width: 0.7em;
  6. font-size: 400%;
  7. font-family: algerian, courier;
  8. line-height: 80%;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <p>
  14. <span>T</span>his is some text.
  15. This is some text. This is some text.
  16. This is some text. This is some text. This is some text.
  17. This is some text. This is some text. This is some text.
  18. This is some text. This is some text. This is some text.
  19. This is some text. This is some text. This is some text.
  20. This is some text. This is some text. This is some text.
  21. This is some text. This is some text. This is some text.
  22. </p>
  23. <p>
  24. 在上面的段落中,文本的第一个字母嵌入 span 元素中。
  25. span 元素的宽度是当前字体大小的 0.7 倍。
  26. span 元素的字体大小为 400%(很大),行高为 80%。
  27. span 中字母的字体将是 "Algerian"。
  28. </p>
  29. </body>

用浮动创建一个网站

使用浮动创建带有水平导航栏、页眉、页脚、左侧导航栏和主要内容的首页。

  1. <head>
  2. <style>
  3. * {
  4. box-sizing: border-box;
  5. }
  6. body {
  7. margin: 0;
  8. }
  9. .header {
  10. background-color: #2196F3;
  11. color: white;
  12. text-align: center;
  13. padding: 15px;
  14. }
  15. .footer {
  16. background-color: #444;
  17. color: white;
  18. padding: 15px;
  19. }
  20. .topmenu {
  21. list-style-type: none;
  22. margin: 0;
  23. padding: 0;
  24. overflow: hidden;
  25. background-color: #777;
  26. }
  27. .topmenu li {
  28. float: left;
  29. }
  30. .topmenu li a {
  31. display: inline-block;
  32. color: white;
  33. text-align: center;
  34. padding: 16px;
  35. text-decoration: none;
  36. }
  37. .topmenu li a:hover {
  38. background-color: #222;
  39. }
  40. .topmenu li a.active {
  41. color: white;
  42. background-color: #4CAF50;
  43. }
  44. .column {
  45. float: left;
  46. padding: 15px;
  47. }
  48. .clearfix::after {
  49. content: "";
  50. clear: both;
  51. display: table;
  52. }
  53. .sidemenu {
  54. width: 25%;
  55. }
  56. .content {
  57. width: 75%;
  58. }
  59. .sidemenu ul {
  60. list-style-type: none;
  61. margin: 0;
  62. padding: 0;
  63. }
  64. .sidemenu li a {
  65. margin-bottom: 4px;
  66. display: block;
  67. padding: 8px;
  68. background-color: #eee;
  69. text-decoration: none;
  70. color: #666;
  71. }
  72. .sidemenu li a:hover {
  73. background-color: #555;
  74. color: white;
  75. }
  76. .sidemenu li a.active {
  77. background-color: #008CBA;
  78. color: white;
  79. }
  80. </style>
  81. </head>
  82. <body>
  83. <ul class="topmenu">
  84. <li><a href="#home" class="active">Home</a></li>
  85. <li><a href="#news">News</a></li>
  86. <li><a href="#contact">Contact</a></li>
  87. <li><a href="#about">About</a></li>
  88. </ul>
  89. <div class="clearfix">
  90. <div class="column sidemenu">
  91. <ul>
  92. <li><a href="#flight">The Flight</a></li>
  93. <li><a href="#city" class="active">The City</a></li>
  94. <li><a href="#island">The Island</a></li>
  95. <li><a href="#food">The Food</a></li>
  96. <li><a href="#people">The People</a></li>
  97. <li><a href="#history">The History</a></li>
  98. <li><a href="#oceans">The Oceans</a></li>
  99. </ul>
  100. </div>
  101. <div class="column content">
  102. <div class="header">
  103. <h1>The City</h1>
  104. </div>
  105. <h1>Shanghai</h1>
  106. <p>Shanghai is one of the four direct-administered municipalities of the People's Republic of China. Welcome to Shanghai!
  107. </p>
  108. <p>You will learn more about responsive web pages in a later chapter.</p>
  109. </div>
  110. </div>
  111. <div class="footer">
  112. <p>Footer Text</p>
  113. </div>
  114. </body>

所有 CSS 浮动属性

属性 描述
box-sizing 定义元素的宽度和高度的计算方式:它们是否应包含内边距和边框。
clear 指定哪些元素可以在被清除的元素旁边以及在哪一侧浮动。
float 指定元素应如何浮动。
overflow 指定如果内容溢出元素框会发生什么情况。
overflow-x 指定当溢出元素的内容区域时,如何处理内容的左/右边缘。
overflow-y 指定当溢出元素的内容区域时,如何处理内容的上/下边缘。