Summary of CSS clear float methods
使用xhtml+css布局经常性地会使用到float,很多邪门的事儿都有可能是浮动在作怪,那么清除浮动就是必须要做的,而且随时性地对父级元素清除浮动的做法也被认为是书写CSS的良好习惯之一。
此为未清除浮动源代码,运行代码无法查看到父级元素浅黄色背景。
<style> body { font-size:24px; color:red; } #layout { background:#FF0; } #left { float:left; width:20%; height:200px; background:#CCC; } #right { float:right; width:30%; height:80px; background:#CCC; } </style> <p> </p><p>Left</p> <p>Right</p>
未清除浮动前如图所示:
三种清除浮动方法如下:
1、使用空标签清除浮动。
我用了很久的一种方法,空标签可以是p标签,也可以是p/span/br等标签,理论上可以是任何标签。这种方式是在需要清除浮动的父级元素内部的所有浮动元素后添加这样一个标签清除浮动,并为其定义CSS代码:clear:both。此方法的弊端在于增加了无意义的结构元素。
ps:
也可以实现效果。
<style> body { font-size:24px; color:red; } #layout { background:#FF0; } #left { float:left; width:20%; height:200px; background:#CCC; } #right { float:right; width:30%; height:80px; background:#CCC; }.clr { clear:both; }</style> <p> </p><p>Left</p> <p>Right</p><p></p>
2、使用overflow属性。
此方法有效地解决了通过空标签元素清除浮动而不得不增加无意代码的弊端。使用该方法是只需在需要清除浮动的元素中定义CSS属性:overflow:auto,即可!”zoom:1″用于兼容IE6。
<style> body { font-size:24px; color:red; } #layout { background:#FF0; overflow:auto; zoom:1; } #left { float:left; width:20%; height:200px; background:#CCC; } #right { float:right; width:30%; height:80px; background:#CCC; } </style> <p> </p><p>Left</p> <p>Right</p>
3、使用after伪对象清除浮动。
该方法只适用于非IE浏览器。具体写法可参照以下示例。使用中需注意以下几点。
一、该方法中必须为需要清除浮动元素的伪对象中设置height:0,否则该元素会比实际高出若干像素;
二、content属性是必须的,但其值可以为空,网上讨论该方法的时候content属性的值设为”.”,但我发现为空也是可以的。
<style> body { font-size:24px; color:red; } #layout { background:#FF0; }#layout:after{display:block;clear:both; content:""; visibility:hidden;height:0;}#left { float:left; width:20%; height:200px; background:#CCC; } #right { float:right; width:30%; height:80px; background:#CCC; } </style> <p> </p><p>Left</p> <p>Right</p>
清除浮动后如图所示:
此三种方法各有利弊,使用时应择优选择,比较之下第二种方法更为可取。
The above is the detailed content of Summary of CSS clear float methods. For more information, please follow other related articles on the PHP Chinese website!

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

AI Hentai Generator
Generate AI Hentai for free.

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



Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

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.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

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

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text
