search
HomeWeb Front-endHTML Tutorial关于清除浮动的一些简单理解。 - DD狗

      在我们学习css时,利用浮动可以为我们的的布局样式提供很大的便利,但是随着而来,我们也会遇到清除浮动的问题,关于清除浮动,我谈一谈我的一些简单看法。

       首先,我们为什么要清除浮动,因为浮动会使该标签脱离文档流,对前后的元素,还有父元素(边框,内容)都会造成影响。

下面说一说清除浮动的方法:

1 定义height

 为父元素增加一个固定或最少的height,因为当子元素设置为浮动后,脱离了文档流后,会导致父元素的边框无法撑开。所以我们可以增加一个

   “height:”或者“min-height:“来解决。

这种方法的优点在于 : 上手容易,简单易懂。

              缺点在于 : 要对高度进行详细的确定,且不灵活。


 

2 利用clear:both

  在父元素的结束标签加入clear:both。在父元素内增加一个空白签,来声明其周围忽略浮动。

html中

css中   .clear{

   clear:both

}

这种方法的优点在于 简单方便。

             缺点在于 编程最忌讳添加很多空标签也会对以后的修改造成麻烦。


 

3 利用overflow:hidden

为父元素在css中添加 overflow:hidden,我们现在看看他们的意思是什么,overflow:溢出 ,hidden:清除。也就是清除溢出的部分,不显示其内容。那么很容易明白,利用这个,就是将带有浮动属性的

进行了清除。

."class名称"{

overflow:hidden

}

这种方法的优点在于:代码使用的少,错误少。

              缺点在于:无法与position一起使用,也不能同时定义height,因为是将多余的部分清除掉。


 

 

以上三个方法是我在初学css清除浮动时用到学到的方法,作为一个web小白,能力有限,所解决问题的办法可能也不是最好的,欢迎大家提议,谢谢。

 

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 is the purpose of the <datalist> element?What is the purpose of the <datalist> element?Mar 21, 2025 pm 12:33 PM

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

How do I use HTML5 form validation attributes to validate user input?How do I use HTML5 form validation attributes to validate user input?Mar 17, 2025 pm 12:27 PM

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

What is the purpose of the <iframe> tag? What are the security considerations when using it?What is the purpose of the <iframe> tag? What are the security considerations when using it?Mar 20, 2025 pm 06:05 PM

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.

What is the purpose of the <progress> element?What is the purpose of the <progress> element?Mar 21, 2025 pm 12:34 PM

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

What is the purpose of the <meter> element?What is the purpose of the <meter> element?Mar 21, 2025 pm 12:35 PM

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

What are the best practices for cross-browser compatibility in HTML5?What are the best practices for cross-browser compatibility in HTML5?Mar 17, 2025 pm 12:20 PM

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

What is the viewport meta tag? Why is it important for responsive design?What is the viewport meta tag? Why is it important for responsive design?Mar 20, 2025 pm 05:56 PM

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.

How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!