Table of Contents
回复讨论(解决方案)
Home Web Front-end HTML Tutorial 有关清除浮动的问题,求解释~~_html/css_WEB-ITnose

有关清除浮动的问题,求解释~~_html/css_WEB-ITnose

Jun 21, 2016 am 09:47 AM

.clear{clear:both;}
Copy after login


.clear{ clear:both;}.clear:after{	clear: both;    content: "";    display: block;}
Copy after login

第一个我知道什么意思但后面那个是啥意思啊,我发现将后面那个应用去清除ul中li的浮动的时候效果好明显。
看下面我的CSS代码
.bookrack_content_ul { width: 100%; background:url(../images/indexPage/ul_bg.png) repeat;}.bookrack_content_li { float: left; margin-top:36px; margin-left:37px; margin-bottom:11px;}
Copy after login

效果:
如果不清除浮动那给UL添加的背景只会出现一排,li的内容长度超过100%的时候就会换行嘛,那下面那行就木有背景。清除后,浏览器怎么缩放,每个li都是有背景的。


回复讨论(解决方案)

如果不清除浮动,ul不能够完全“包裹住”浮动的子元素li。浮动的li会脱离文档流,而不被ui包裹。
如果ul使用了清除浮动,则ul,可以将浮动的子元素li“包裹住”,所以换行后li还会有背景。

如果不清除浮动,ul不能够完全“包裹住”浮动的子元素li。浮动的li会脱离文档流,而不被ui包裹。
如果ul使用了清除浮动,则ul,可以将浮动的子元素li“包裹住”,所以换行后li还会有背景。
那能不能直接使用clear:both呢,为啥要使用后面那个有伪类的东东

如果不使用伪类,直接使用clear:both的话,你可以在ui中,即li的同级中添加一个空的li,在这个空的li上使用clear:both,可以达到同样的效果。就好像如下代码:

<div class="wrapper">	<ul class="bookrack_content_ul">		<li class="bookrack_content_li">aaa</li>		<li class="bookrack_content_li">aaa</li>		<li class="bookrack_content_li">aaa</li>		<li class="clearfix"></li><!--注意clearfix是用在这个地方的,在去掉伪类的情况下也可以正常使用!!!-->	</ul></div>
Copy after login
Copy after login

在ul上,使用伪类的方式,即相当于在ul中默认添加了一个元素,使用clear:both
<div class="wrapper">	<ul class="bookrack_content_ul clearfix"><!--注意clearfix是用在这个地方的,需要使用伪类才可以使用-->		<li class="bookrack_content_li">aaa</li>		<li class="bookrack_content_li">aaa</li>		<li class="bookrack_content_li">aaa</li>	</ul></div>
Copy after login
Copy after login

如果不使用伪类,直接使用clear:both的话,你可以在ui中,即li的同级中添加一个空的li,在这个空的li上使用clear:both,可以达到同样的效果。就好像如下代码:

<div class="wrapper">	<ul class="bookrack_content_ul">		<li class="bookrack_content_li">aaa</li>		<li class="bookrack_content_li">aaa</li>		<li class="bookrack_content_li">aaa</li>		<li class="clearfix"></li><!--注意clearfix是用在这个地方的,在去掉伪类的情况下也可以正常使用!!!-->	</ul></div>
Copy after login
Copy after login

在ul上,使用伪类的方式,即相当于在ul中默认添加了一个元素,使用clear:both
<div class="wrapper">	<ul class="bookrack_content_ul clearfix"><!--注意clearfix是用在这个地方的,需要使用伪类才可以使用-->		<li class="bookrack_content_li">aaa</li>		<li class="bookrack_content_li">aaa</li>		<li class="bookrack_content_li">aaa</li>	</ul></div>
Copy after login
Copy after login

谢谢

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

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)

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 &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

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 &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

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.

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 &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

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 &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

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.

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.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

See all articles