为了让开发人员实现简单的布局,比如一列文本中浮动图像,文字环绕在它周围(像是在报纸上看到的那样)

简单的浮动

示例-文字环绕

  1. <h1>Simple float example</h1>
  2. <img
  3. src="https://mdn.mozillademos.org/files/13340/butterfly.jpg"
  4. alt="A pretty butterfly with red, white, and brown coloring, sitting on a large leaf"
  5. />
  6. <p>
  7. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla luctus
  8. aliquam dolor, eu lacinia lorem placerat vulputate. Duis felis orci,
  9. pulvinar id metus ut, rutrum luctus orci. Cras porttitor imperdiet nunc,
  10. at ultricies tellus laoreet sit amet. Sed auctor cursus massa at porta.
  11. Integer ligula ipsum, tristique sit amet orci vel, viverra egestas ligula.
  12. Curabitur vehicula tellus neque, ac ornare ex malesuada et. In vitae
  13. convallis lacus. Aliquam erat volutpat. Suspendisse ac imperdiet turpis.
  14. Aenean finibus sollicitudin eros pharetra congue. Duis ornare egestas
  15. augue ut luctus. Proin blandit quam nec lacus varius commodo et a urna. Ut
  16. id ornare felis, eget fermentum sapien.
  17. </p>
  18. <p>
  19. Nam vulputate diam nec tempor bibendum. Donec luctus augue eget malesuada
  20. ultrices. Phasellus turpis est, posuere sit amet dapibus ut, facilisis sed
  21. est. Nam id risus quis ante semper consectetur eget aliquam lorem. Vivamus
  22. tristique elit dolor, sed pretium metus suscipit vel. Mauris ultricies
  23. lectus sed lobortis finibus. Vivamus eu urna eget velit cursus viverra
  24. quis vestibulum sem. Aliquam tincidunt eget purus in interdum. Cum sociis
  25. natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
  26. </p>

正常情况下,段落标签和图片标签是按照瀑布流的方式布局的,可以在body中加入以上代码,并在浏览器进行预览

  1. // 设置宽度并居中
  2. body {
  3. width: 800px;
  4. margin: 0 auto;
  5. }
  6. // 给图片添加浮动
  7. img {
  8. float: left;
  9. }

添加以上样式代码后,可以看到如下图,文字环绕在图片周围,可以尝试将float设置为right看看效果

截屏2022-03-14 22.18.18.png

示例-首字母下沉

  1. <p>
  2. This is my very important paragraph. I am a distinguished gentleman of
  3. such renown that my paragraph needs to be styled in a manner befitting my
  4. majesty. Bow before my splendour, dear students, and go forth and learn
  5. CSS!
  6. </p>
  1. // 首字符选择器,并设置浮动
  2. p::first-letter {
  3. float: left;
  4. font-size: 2em;
  5. margin-right: 10px;
  6. }

截屏2022-03-14 22.26.39.png

多列浮动布局

浮动经常被用于创建多个列的布局,尽管他并不是用来做这个

示例-双列布局

  1. <h1>2 column layout example</h1>
  2. <div>
  3. <h2>First column</h2>
  4. <p>
  5. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla luctus
  6. aliquam dolor, eu lacinia lorem placerat vulputate. Duis felis orci,
  7. pulvinar id metus ut, rutrum luctus orci. Cras porttitor imperdiet nunc,
  8. at ultricies tellus laoreet sit amet. Sed auctor cursus massa at porta.
  9. Integer ligula ipsum, tristique sit amet orci vel, viverra egestas
  10. ligula. Curabitur vehicula tellus neque, ac ornare ex malesuada et. In
  11. vitae convallis lacus. Aliquam erat volutpat. Suspendisse ac imperdiet
  12. turpis. Aenean finibus sollicitudin eros pharetra congue. Duis ornare
  13. egestas augue ut luctus. Proin blandit quam nec lacus varius commodo et
  14. a urna. Ut id ornare felis, eget fermentum sapien.
  15. </p>
  16. </div>
  17. <div>
  18. <h2>Second column</h2>
  19. <p>
  20. Nam vulputate diam nec tempor bibendum. Donec luctus augue eget
  21. malesuada ultrices. Phasellus turpis est, posuere sit amet dapibus ut,
  22. facilisis sed est. Nam id risus quis ante semper consectetur eget
  23. aliquam lorem. Vivamus tristique elit dolor, sed pretium metus suscipit
  24. vel. Mauris ultricies lectus sed lobortis finibus. Vivamus eu urna eget
  25. velit cursus viverra quis vestibulum sem. Aliquam tincidunt eget purus
  26. in interdum. Cum sociis natoque penatibus et magnis dis parturient
  27. montes, nascetur ridiculus mus.
  28. </p>
  29. </div>
  1. body {
  2. width: 800px;
  3. margin: 0 auto;
  4. }
  5. div:nth-of-type(1) {
  6. width: 40%;
  7. float: left;
  8. }
  9. div:nth-of-type(2) {
  10. width: 40%;
  11. float: right;
  12. }

以上代码通过nth-of-type 筛选兄弟元素,并设置浮动属性,运行如下

截屏2022-03-14 22.38.51.png
浮动会影响到瀑布流下面的元素,并会环绕浮动标签,让我们再添加一个段落在html代码内

  1. <footer>
  2. <p>&copy;2016 your imagination. This isn't really copyright, this is a mockery of the very concept. Use as you wish.</p>
  3. </footer>

截屏2022-03-14 22.42.34.png
打开浏览器查看页面,这显然是很糟糕的,footer并不是像预期的那样显示在文档底部,加入以下代码解决这个问题(停止浮动)

  1. footer {
  2. clear:both
  3. }

通过添加clear属性消除浮动,参数有both停止左右浮动,left停止左浮动,right停止右浮动