Three ways to clear floating CSS_html/css_WEB-ITnose
Just talk about a few key points of float:
It only floats left and right, not up and down. After the
element is set to float, it will break away from the normal flow (same as position: absolute;), no longer occupy the space of the original layer, and will also cover the elements of the next layer.
Floating has no effect on the element's previous sibling.
After floating, the next sibling element of the element will be immediately behind the element that has not been set to float before the element (it is easy to understand, because the element is out of the ordinary flow, or is not at this level, So of course its next element must fill its position).
If there is an inline element (usually text) in the next sibling element of the element, it will be displayed around the element, creating an effect similar to "text surrounding an image". (Please refer to the explanation in the in-depth study, detailed explanation and expansion of CSS float (1))
If the next sibling element is also set to float in the same direction, it will be displayed immediately after the element.
This element turns into a block-level element , which is equivalent to setting display: block; (the same as position: absolute;) for the element.
What are the methods for clearing floats in CSS ? Floats are often used, and many evil things may be caused by floats. Clearing floats is a must, and clearing floats from parent elements at any time is also considered a good habit in writing CSS. one.
Let’s look at today’s tutorial. This is a floating source code that has not been cleared. The light yellow background of the parent element cannot be seen when running the code.
1 <style type=”text/css”> 2 <!? *{margin:0;padding:0;} 3 body{font:36px bold; color:#F00; text-align:center;} #layout{background:#FF9;} #left{float:left;width:20%;height:200px;background:#DDD;line-height:200px;} #right{float:right;width:30%;height:80px;background:#DDD;line-height:80px;} ?> 4 </style> 5 <div id=”layout”> 6 <div id=”left”>Left</div> 7 <div id=”right”>Right</div>8 </div>
1. Use empty tags Clear floating
A method I have used for a long time, the empty tag can be a div tag or a P tag. I am used to using
, but the border needs to be cleared separately, but in theory it can be any label. This method is to add such a tag to clear float after all floating elements inside the floating parent element need to be cleared, and define the CSS code for it: clear:both. The disadvantage of this approach is the addition of meaningless structural elements.
1 <style type=”text/css”> 2 <!? *{margin:0;padding:0;} 3 body{font:36px bold; color:#F00; text-align:center;} #layout{background:#FF9;} #left{float:left;width:20%;height:200px;background:#DDD;line-height:200px;} #right{float:right;width:30%;height:80px;background:#DDD;line-height:80px;} 4 .clr{clear:both;} 5 ?> 6 </style> 7 <div id=”layout”> 8 <div id=”left”>Left</div> 9 <div id=”right”>Right</div> 10 <div class=”clr”></div> 11 </div>
2. Use the overflow attribute
This method effectively solves the problem The disadvantage of having to add unintentional code by clearing floats via empty tag elements. To use this method, you only need to define the CSS property: overflow:auto in the element that needs to be cleared of floating, and that's it! "zoom:1" is for compatibility with IE6.
1 <style type=”text/css”> 2 <!? *{margin:0;padding:0;} 3 body{font:36px bold; color:#F00; text-align:center;} #layout{background:#FF9;overflow:auto;zoom:1;} #left{float:left;width:20%;height:200px;background:#DDD;line-height:200px;} #right{float:right;width:30%;height:80px;background:#DDD;line-height:80px;} ?> 4 </style> <div id=”layout”> 5 <div id=”left”>Left</div> 6 <div id=”right”>Right</div> <7 /div>
3. Use the after pseudo-object to float clearly
This method only applies to non-IE browsers. Please refer to the following examples for specific writing methods. Please pay attention to the following points during use. 1. In this method, height:0 must be set for the pseudo object that needs to clear the floating element, otherwise the element will be several pixels higher than the actual one; 2. The content attribute is necessary, but its value can be empty, blue ideal discussion In this method, the value of the content attribute is set to ".", but I found that it is also possible to leave it empty.
1 <style type=”text/css”> 2 <!? *{margin:0;padding:0;} 3 body{font:36px bold; color:#F00; text-align:center;} #layout{background:#FF9;} #layout:after{display:block;clear:both;content:”";visibility:hidden;height:0;} #left{float:left;width:20%;height:200px;background:#DDD;line-height:200px;} #right{float:right;width:30%;height:80px;background:#DDD;line-height:80px;} ?> 4 </style> 5 <div id=”layout”> 6 <div id=”left”>Left</div> 7 <div id=”right”>Right</div> 8 </div>
These three methods have their own pros and cons. You should choose the best when using them. Compare the second one method is preferable.

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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.

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

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 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.

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.

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.

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-

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