search
HomeWeb Front-endHTML TutorialDay_8.《无懈可击的web设计》-巧妙地浮动效果_html/css_WEB-ITnose

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

为什么要清除浮动?

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

清除浮动的方法:

  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
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
What are self-closing tags? Give an example.What are self-closing tags? Give an example.Apr 27, 2025 am 12:04 AM

Self-closingtagsinHTMLandXMLaretagsthatclosethemselveswithoutneedingaseparateclosingtag,simplifyingmarkupstructureandenhancingcodingefficiency.1)TheyareessentialinXMLforelementswithoutcontent,ensuringwell-formeddocuments.2)InHTML5,usageisflexiblebutr

Beyond HTML: Essential Technologies for Web DevelopmentBeyond HTML: Essential Technologies for Web DevelopmentApr 26, 2025 am 12:04 AM

To build a website with powerful functions and good user experience, HTML alone is not enough. The following technology is also required: JavaScript gives web page dynamic and interactiveness, and real-time changes are achieved by operating DOM. CSS is responsible for the style and layout of the web page to improve aesthetics and user experience. Modern frameworks and libraries such as React, Vue.js and Angular improve development efficiency and code organization structure.

What are boolean attributes in HTML? Give some examples.What are boolean attributes in HTML? Give some examples.Apr 25, 2025 am 12:01 AM

Boolean attributes are special attributes in HTML that are activated without a value. 1. The Boolean attribute controls the behavior of the element by whether it exists or not, such as disabled disable the input box. 2.Their working principle is to change element behavior according to the existence of attributes when the browser parses. 3. The basic usage is to directly add attributes, and the advanced usage can be dynamically controlled through JavaScript. 4. Common mistakes are mistakenly thinking that values ​​need to be set, and the correct writing method should be concise. 5. The best practice is to keep the code concise and use Boolean properties reasonably to optimize web page performance and user experience.

How can you validate your HTML code?How can you validate your HTML code?Apr 24, 2025 am 12:04 AM

HTML code can be cleaner with online validators, integrated tools and automated processes. 1) Use W3CMarkupValidationService to verify HTML code online. 2) Install and configure HTMLHint extension in VisualStudioCode for real-time verification. 3) Use HTMLTidy to automatically verify and clean HTML files in the construction process.

HTML vs. CSS and JavaScript: Comparing Web TechnologiesHTML vs. CSS and JavaScript: Comparing Web TechnologiesApr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTML as a Markup Language: Its Function and PurposeHTML as a Markup Language: Its Function and PurposeApr 22, 2025 am 12:02 AM

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

See all articles

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

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.