Home Web Front-end HTML Tutorial 关于清除浮动的一些简单理解。 - DD狗

关于清除浮动的一些简单理解。 - DD狗

May 21, 2016 am 08:42 AM

      在我们学习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 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 Article Tags

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)

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Mar 04, 2025 pm 12:32 PM

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?

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

How do I use HTML5 form validation attributes to validate user input?

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

What is the purpose of the <iframe> tag? What are the security considerations when using it?

How to efficiently add stroke effects to PNG images on web pages? How to efficiently add stroke effects to PNG images on web pages? Mar 04, 2025 pm 02:39 PM

How to efficiently add stroke effects to PNG images on web pages?

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

What is the purpose of the <meter> element?

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

What is the purpose of the <datalist> element?

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

What is the purpose of the <progress> element?

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

What are the best practices for cross-browser compatibility in HTML5?

See all articles