本章内容略显陈旧,主要描述如何用浮动替代表格布局,并没有什么出彩的地方。不过其间提到了清楚浮动的几种方法,那么今天就总结一下如何清楚浮动吧。
为什么要清除浮动?
虽说是清除浮动,其实是清除浮动产生的影响。
浮动的元素,高度会塌陷,而高度的塌陷使我们布局中需要清除浮动的最重要的原因之一。
清除浮动的方法:
-
父级div定义height
- 原理:父级div手动定义height,就解决了父级div无法自动获取到高度的问题。
- 优点:简单、代码少、容易掌握
- 缺点:只适合高度固定的布局,要给出精确的高度,如果高度和父级div不一样时,会产生问题
- 建议:不建议使用,只建议高度固定的布局时使用
-
结尾处加空div标签 clear:both
- 原理:添加一个空div,利用CSS提供的clear:both清楚浮动,让父级div自动获取高度
- 优点:简单、代码少,浏览器支持好,不容易出现怪问题
- 缺点:如果页面浮动布局多,就要增加很多空div
- 建议:不推荐使用,但此方法是目前使用很频繁的一种方法
-
父级div定义伪类:after和zoom
- 原理:IE8以上和非IE浏览器才支持,原理类似2,zoom(IE专有属性)可解决ie6,ie7浮动问题
- 优点:浏览器支持好、不容易出现怪问题(目前:大型网站都有使用,如:腾迅,网易,新浪等等)
- 缺点:代码多、不少初学者不理解原理,要两句代码结合使用才能让主流浏览器都支持。
- 建议:推荐使用,建议定义公共类,以减少css代码
-
父级div定义overflow:hidden
- 原理:必须定义width或zoom:1,同时不能定义height,使用overflow:hidden时,浏览器会自动检查浮动区域高度
- 优点:简单、代码少、浏览器支持好
- 缺点:不能和position配合使用,因为超出的尺寸会被隐藏
- 建议:只推荐没有使用position的朋友
-
父级div定义overflow:auto
- 原理:必须定义width或zoom:1,同时不能定义height,使用overflow:auto时,浏览器会自动检查浮动区域的高度
- 优点:简单、代码少、浏览器支持好
- 缺点:内部宽高超过父级div时,会出现滚动条。
- 建议:不推荐使用,如果你需要出现滚动条或者确保你的代码不会出现滚动条就使用吧。
-
父级div 也一起浮动
- 原理:所有代码一起浮动,就变成了一个整体
- 优点:没有优点
- 缺点:会产生新的浮动问题。
- 建议:不推荐使用,只作了解。
-
父级div定义 display:table
- 原理:将div属性变成表格
- 优点:没有优点
- 缺点:会产生新的未知问题。
- 建议:不推荐使用,只作了解。
-
结尾处加 br标签 clear:both
- 原理:父级div定义zoom:1来解决IE浮动问题,结尾处加 br标签 clear:both
- 建议:不推荐使用,只作了解。

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
