Home Web Front-end CSS Tutorial Examples of 6 ways to clear floats in html_CSS/HTML

Examples of 6 ways to clear floats in html_CSS/HTML

May 16, 2016 pm 12:03 PM
clear float

What will happen when display: inline-block is used:

1. Make the block element display in one line
2. Make the inline support width and height
3 .Line breaks are parsed
4. When not set, the width is stretched by the content
5. Block tags are supported in IE6 and 7

Due to the inline-block attribute, line breaks are parsed ( There is a gap), so the solution is to use floating float:left/right

Situations that occur when using float:

1. Make the block element display in one line
2. Make the inline element support Width and height
3. When the width and height are not set, the width is supported by the content
4. Line breaks are not parsed (so when using inline elements, you can use floats to clear gaps)
5. Element addition Floating will break away from the document flow and move in a specified direction until it hits the boundary of the parent or stops with another floating element (the document flow is the position occupied by displayable objects in the document when arranged)

Copy Code The code is as follows:





Untitled document



div1

div2

span1n
span2




In the following code, only box1 floats, so box1 and box2 overlap. If both float, they will not overlap

Copy code The code is as follows:





无标题文档







清浮动的方法:
1.给父级也加浮动
(这种情况当父级margin:0 auto;时不居中)

复制代码 代码如下:





无标题文档




   






2.给父级加display:inline-block;(同方法1,不居中。只有IE6,7居中)

复制代码 代码如下:





无标题文档




   






3.在浮动元素下加


  .clear{ height:0px;font-size:0;clear:both;}但是在ie6下,块元素有最小高度,即当height
复制代码 代码如下:





无标题文档

    .clear{ height:0px;font-size:0;clear:both;}
*/




   

       





4. Add

Copy code The code is as follows:





Untitled document

.clear{ height:0px;font-size:0;clear:both;}
4. Add

*/




phpcnlt phpcn/div>






5. Add {zoom to the parent of the floating element :1;}
:after{content:""; display:block;clear:both;}
Copy code The code is as follows:





Untitled document

.clear{ height:0px;font-size:0;clear:both;}
4. Add


under the floating element 5. Give the parent of the floating element Level plus {zoom:1;}
:after{content:""; display:block;clear:both;}

**In IE6 and 7, it is not necessary if the parent of the floating element has a width. Clear float

haslayout recalculates the width and height of the element based on the size of the element content or the size of the parent's parent

display: inline-block
height: (any value except auto)
float: (left or right)
width: (any value except auto)
zoom: (any value except normal)
*/






cngtphpcn


6. Add overflow:auto;
Copy code The code is as follows:





Untitled document




div>



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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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 are the 5 ways to clear floats? What are the 5 ways to clear floats? Nov 20, 2023 pm 04:27 PM

The five ways to clear floats are: 1. Use the clear attribute; 2. Use the overflow attribute; 3. Use the pseudo element clearfix; 4. Use flex layout; 5. Use grid layout. Detailed introduction: 1. Use the clear attribute, which is the most commonly used method to clear floats. You can add an element after the floating element and add the "clear: both;" style to it; 2. Use the overflow attribute to set the parent element Set "overflow: auto;" and so on.

How to align input boxes in html How to align input boxes in html Apr 05, 2024 am 10:18 AM

Methods of using HTML to align input boxes include: using the text-align attribute to specify left, right, or center to align the text of the input box. Use the float property to float the input box to the left or right side of the page to affect its relative alignment.

Is there any way to clear floats? Is there any way to clear floats? Feb 22, 2024 pm 04:00 PM

Is there any method to clear floats? Specific code examples are required. In web page layout, floats are a common layout method that allows elements to break away from the document flow and be positioned relative to other elements. However, a problem often encountered when using floating layout is that the parent element cannot wrap the floating element correctly, causing the page to have a disordered layout. Therefore, we need to take measures to clear the float so that the parent element can wrap the floated element correctly. There are many ways to clear floats. The following will introduce several commonly used methods and give specific code examples.

What is layout layout? What is layout layout? Feb 24, 2024 pm 03:03 PM

Layout refers to a typesetting method adopted in web design to arrange and display web page elements according to certain rules and structures. Through reasonable layout, the webpage can be made more beautiful and neat, and achieve a good user experience. In front-end development, there are many layout methods to choose from, such as traditional table layout, floating layout, positioning layout, etc. However, with the promotion of HTML5 and CSS3, modern responsive layout technologies, such as Flexbox layout and Grid layout, have become

What are the ways to clear floats in css What are the ways to clear floats in css Oct 30, 2023 am 11:57 AM

The ways to clear floats in CSS include clear attribute, overflow attribute, clearfix class, clearfix class of parent element, pseudo-element to clear float, overflow attribute of parent element, and combination of clear attribute and BFC. Detailed introduction: 1. Use the clear attribute, a simple and commonly used method to clear floats. By adding an empty block-level element after the floating element and setting the clear attribute for it, you can clear the previous floating effect and make it The elements below are laid out normally and so on.

Why floated elements cannot be cleared by overflow property Why floated elements cannot be cleared by overflow property Jan 27, 2024 am 08:08 AM

To analyze why floating cannot be cleared using the overflow attribute, specific code examples are required. Introduction: In web page layout, problems with floating elements are often encountered. In order to solve the impact of floating elements, we usually use a method of clearing floats. However, sometimes we find that floats cannot be cleared well using the overflow attribute. This article will delve into this issue and provide specific code examples. 1. Why do we need to clear floats? Floating elements means taking the element out of the document flow by setting the float attribute.

Why does overflow clear float have no effect? Why does overflow clear float have no effect? Oct 17, 2023 pm 02:29 PM

The reason why overflow clearing float is invalid may be that the height of the floating element is not set, the floating element is cleared, the clearing element is before the floating element, the height of the clear element is not set, or the clear element is after the floating element, etc. Detailed introduction: 1. The height of the floating element is not set. When the height of the floating element is not set, it may not be cleared. The height of the floating element is determined by its content, so if the content has no height, the floating element also has no height; 2. Floating The element is cleared, when the floated element is cleared, the overflow property may not clear and so on.

CSS Floats and Clear Floats: Mastering Floats and Clear Floats CSS Floats and Clear Floats: Mastering Floats and Clear Floats Nov 18, 2023 am 10:56 AM

CSS floating and clearing floats: Mastering the skills of floating and clearing floats requires specific code examples. In web design and development, CSS floats (float) are one of the common layout techniques. You can use float to move elements to the left or right to adjust and arrange the elements on the page. However, floating elements can also cause some problems on the page, such as the height of the parent element collapsing, etc. Therefore, it is very important to master the skills of using floats and clearing them. This article will focus on CSS floats and clear float techniques, and provide specific

See all articles