Table of Contents
一、浮动产生原因   -   TOP
二、浮动产生负作用   -   TOP
三、css解决浮动,清除浮动方法   -   TOP
Home Web Front-end HTML Tutorial CSS清除浮动_清除float浮动_html/css_WEB-ITnose

CSS清除浮动_清除float浮动_html/css_WEB-ITnose

Jun 24, 2016 am 11:21 AM

CSS清除浮动方法集合

一、浮动产生原因   -   TOP

一般浮动是什么情况呢?一般是一个盒子里使用了CSS float浮动属性,导致父级对象盒子不能被撑开,这样CSS float浮动就产生了。


浮动产生样式效果截图

本来两个黑色对象盒子是在红色盒子内,因为对两个黑色盒子使用了float浮动,所以两个黑色盒子产生了浮动,导致红色盒子不能撑开,这样浮动就产生了。

简单地说,浮动是因为使用了float:left或float:right或两者都是有了而产生的浮动。

二、浮动产生负作用   -   TOP

1、背景不能显示
由于浮动产生,如果对父级设置了(CSS background背景)CSS背景颜色或CSS背景图片,而父级不能被撑开,所以导致CSS背景不能显示。

2、边框不能撑开
如上图中,如果父级设置了CSS边框属性(css border),由于子级里使用了float属性,产生浮动,父级不能被撑开,导致边框不能随内容而被撑开。

3、margin padding设置值不能正确显示
由于浮动导致父级子级之间设置了css padding、css margin属性的值不能正确表达。特别是上下边的padding和margin不能正确显示。

三、css解决浮动,清除浮动方法   -   TOP

这里DIVCSS5为了统一讲解浮动解决方法,假设了有三个盒子对象,一个父级里包含了两个子级,子级一个使用了float:left属性,另外一个子级使用float:right属性。同时设置div css border,父级css边框颜色为红色,两个子级边框颜色为蓝色;父级CSS背景样式为黄色,两个子级背景为白色;父级css width宽度为400px,两个子级css宽度均为180px,两个子级再设置相同高度100px,父级css height高度暂不设置(通常为实际css布局时候这样父级都不设置高度,而高度是随内容增加自适应高度)。

父级CSS命名为“.divcss5”对应html标签使用“


两个子级CSS命名分别为“.divcss5-left”“.divcss5-right”

根据以上描述DIVCSS5给出对应CSS代码和HTML代码片段

CSS代码:

               <ol>                                          <li> .divcss5{ width:400px; border:1px solid #F00; background:#FF0}  </li>                         <li> .divcss5-left,.divcss5-right{ width:180px; height:100px;  </li>                         <li>  border:1px solid #00F; background:#FFF}  </li>                         <li> .divcss5-left{ float:left}  </li>                         <li> .divcss5-right{ float:right}  </li>                </ol>
Copy after login

对应html源代码片段:

               <ol>                                          <li> <div class="divcss5">  </li>                         <li>     <div class="divcss5-left">left浮动</div>  </li>                         <li>     <div class="divcss5-right">right浮动</div>  </li>                         <li> </div>  </li>                </ol>
Copy after login


清除浮动前案例截图父级需要清除浮动

以下DIVCSS5总结了几点用于清除浮动的经验教程

1、对父级设置适合CSS高度
对父级设置适合高度样式清除浮动,这里对“.divcss5”设置一定高度即可,一般设置高度需要能确定内容高度才能设置。这里我们知道内容高度是100PX+上下边框为2px,这样具体父级高度为102px

CSS代码:

               <ol>                                          <li> .divcss5{ width:400px;border:1px solid #F00;background:#FF0; height:102px}  </li>                         <li> .divcss5-left,.divcss5-right{width:180px;height:100px;  </li>                         <li> border:1px solid #00F;background:#FFF}  </li>                         <li> .divcss5-left{ float:left}  </li>                         <li> .divcss5-right{ float:right}  </li>                </ol>
Copy after login

Html代码不变。得到截图


使用height高度清除浮动

小结,使用设置高度样式,清除浮动产生,前提是对象内容高度要能确定并能计算好。

2、clear:both清除浮动
为了统一样式,我们新建一个样式选择器CSS命名为“.clear”,并且对应选择器样式为“clear:both”,然后我们在父级“

”结束前加此div引入“class="clear"”样式。这样即可清除浮动。

具体CSS代码:

               <ol>                                          <li> .divcss5{ width:400px;border:1px solid #F00;background:#FF0}  </li>                         <li> .divcss5-left,.divcss5-right{width:180px;height:100px;  </li>                         <li> border:1px solid #00F;background:#FFF}  </li>                         <li> .divcss5-left{ float:left}  </li>                         <li> .divcss5-right{ float:right}  </li>                         <li> .clear{ clear:both}  </li>                </ol>
Copy after login

Html代码:

               <ol>                                          <li> <div class="divcss5">  </li>                         <li>     <div class="divcss5-left">left浮动</div>  </li>                         <li>     <div class="divcss5-right">right浮动</div>  </li>                         <li>     <strong><div class="clear"></div> </strong> </li>                         <li> </div>  </li>                </ol>
Copy after login

clear清除浮动截图


clear清除浮动截图 使用CSS clear清除浮动

这个css clear清除float产生浮动,可以不用对父级设置高度 也无需技术父级高度,方便适用,但会多加CSS和HTML标签。

3、父级div定义 overflow:hidden
对父级CSS选择器加overflow:hidden样式,可以清除父级内使用float产生浮动。优点是可以很少CSS代码即可解决浮动产生。

overflow:hidden解决CSS代码:

               <ol>                                          <li> .divcss5{ width:400px;border:1px solid #F00;background:#FF0; overflow:hidden}  </li>                         <li> .divcss5-left,.divcss5-right{width:180px;height:100px; border:1px solid #00F;background:#FFF}  </li>                         <li> .divcss5-left{ float:left}  </li>                         <li> .divcss5-right{ float:right}  </li>                </ol>
Copy after login

HTML代码不变。

解决清除浮动后截图


overflow清除float产生浮动截图 overflow:hidden清除浮动截图

为什么加入overflow:hidden即可清除浮动呢?那是因为overflow:hidden属性相当于是让父级紧贴内容,这样即可紧贴其对象内内容(包括使用float的div盒子),从而实现了清除浮动。Css overflow:hidden清除浮动方法DIVCSS5推荐使用。

以上三点即是兼容各大浏览器清除浮动的方法,其它有的浏览器不兼容有的不兼容的方法就没必要介绍了,大家记住以上三点解决float浮动清除浮动方法。但这里推荐第三点和第二点解决清除浮动方法。

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)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
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.

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.

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.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 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.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 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 vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

See all articles