Day_8.《无懈可击的web设计》-巧妙地浮动效果_html/css_WEB-ITnose
本章内容略显陈旧,主要描述如何用浮动替代表格布局,并没有什么出彩的地方。不过其间提到了清楚浮动的几种方法,那么今天就总结一下如何清楚浮动吧。
为什么要清除浮动?
虽说是清除浮动,其实是清除浮动产生的影响。
浮动的元素,高度会塌陷,而高度的塌陷使我们布局中需要清除浮动的最重要的原因之一。
清除浮动的方法:
-
父级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
- 建议:不推荐使用,只作了解。

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

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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.

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.

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

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

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

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 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.

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...
