Table of Contents
为什么要清除浮动?
清除浮动的方法:
Home Web Front-end HTML Tutorial Day_8.《无懈可击的web设计》-巧妙地浮动效果_html/css_WEB-ITnose

Day_8.《无懈可击的web设计》-巧妙地浮动效果_html/css_WEB-ITnose

Jun 24, 2016 am 11:31 AM

本章内容略显陈旧,主要描述如何用浮动替代表格布局,并没有什么出彩的地方。不过其间提到了清楚浮动的几种方法,那么今天就总结一下如何清楚浮动吧。

为什么要清除浮动?

虽说是清除浮动,其实是清除浮动产生的影响。
浮动的元素,高度会塌陷,而高度的塌陷使我们布局中需要清除浮动的最重要的原因之一。

清除浮动的方法:

  1. 父级div定义height

  2. 原理:父级div手动定义height,就解决了父级div无法自动获取到高度的问题。
  3. 优点:简单、代码少、容易掌握
  4. 缺点:只适合高度固定的布局,要给出精确的高度,如果高度和父级div不一样时,会产生问题
  5. 建议:不建议使用,只建议高度固定的布局时使用
  6. 结尾处加空div标签 clear:both

  7. 原理:添加一个空div,利用CSS提供的clear:both清楚浮动,让父级div自动获取高度
  8. 优点:简单、代码少,浏览器支持好,不容易出现怪问题
  9. 缺点:如果页面浮动布局多,就要增加很多空div
  10. 建议:不推荐使用,但此方法是目前使用很频繁的一种方法
  11. 父级div定义伪类:after和zoom

  12. 原理:IE8以上和非IE浏览器才支持,原理类似2,zoom(IE专有属性)可解决ie6,ie7浮动问题
  13. 优点:浏览器支持好、不容易出现怪问题(目前:大型网站都有使用,如:腾迅,网易,新浪等等)
  14. 缺点:代码多、不少初学者不理解原理,要两句代码结合使用才能让主流浏览器都支持。
  15. 建议:推荐使用,建议定义公共类,以减少css代码
  16. 父级div定义overflow:hidden

  17. 原理:必须定义width或zoom:1,同时不能定义height,使用overflow:hidden时,浏览器会自动检查浮动区域高度
  18. 优点:简单、代码少、浏览器支持好
  19. 缺点:不能和position配合使用,因为超出的尺寸会被隐藏
  20. 建议:只推荐没有使用position的朋友
  21. 父级div定义overflow:auto

  22. 原理:必须定义width或zoom:1,同时不能定义height,使用overflow:auto时,浏览器会自动检查浮动区域的高度
  23. 优点:简单、代码少、浏览器支持好
  24. 缺点:内部宽高超过父级div时,会出现滚动条。
  25. 建议:不推荐使用,如果你需要出现滚动条或者确保你的代码不会出现滚动条就使用吧。
  26. 父级div 也一起浮动

  27. 原理:所有代码一起浮动,就变成了一个整体
  28. 优点:没有优点
  29. 缺点:会产生新的浮动问题。
  30. 建议:不推荐使用,只作了解。
  31. 父级div定义 display:table

  32. 原理:将div属性变成表格
  33. 优点:没有优点
  34. 缺点:会产生新的未知问题。
  35. 建议:不推荐使用,只作了解。
  36. 结尾处加 br标签 clear:both

  37. 原理:父级div定义zoom:1来解决IE浮动问题,结尾处加 br标签 clear:both
  38. 建议:不推荐使用,只作了解。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

See all articles