Home Web Front-end CSS Tutorial How to clear floats using CSS

How to clear floats using CSS

Jun 12, 2018 am 11:33 AM
clear float

This article mainly introduces how to use CSS to clear floating methods. It has certain reference value. Now I share it with you. Friends in need can refer to it.

Display effects in various browsers It may also be different, which makes clearing floats more difficult. Here are 8 ways to clear floats. The test has passed ie chrome firefox opera. Friends in need can refer to the following

Clearing floats is the design of every web front desk A function that a teacher must master. css clear float encyclopedia, a total of 8 methods.
Floating will cause the current label to float upward, and will also affect the position of the front and rear labels, the parent label, and the width height attribute. Moreover, the same code may display differently in various browsers, which makes clearing floats more difficult. There are several ways to solve problems caused by floats, but some have issues with browser compatibility.
The following summarizes 8 methods of clearing floats (the test has passed ie chrome firefox opera, the next three methods are just for understanding):
1, the parent p defines height

1

2

3

4

5

6

7

8

9

10

11

12

13

<style type="text/css">

.p1{background:#000080;border:1px solid red;/*解决代码*/height:200px;}

.p2{background:#800080;border:1px solid red;height:100px;margin-top:10px}

.left{float:left;width:20%;height:200px;background:#DDD}

.right{float:right;width:30%;height:80px;background:#DDD}

</style>

<p class="p1">

<p class="left">Left</p>

<p class="right">Right</p>

</p>

<p class="p2">

p2

</p>

Copy after login

Principle : The parent p manually defines the height, which solves the problem that the parent p cannot automatically obtain the height.
Advantages: simple, less code, easy to master
Disadvantages: only suitable for fixed-height layouts, the precise height must be given, if the height is different from the parent p, problems will occur
Suggestion: No It is recommended to use
2 only for fixed-height layouts, and add an empty p tag clear:both at the end

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<style type="text/css">

.p1{background:#000080;border:1px solid red}

.p2{background:#800080;border:1px solid red;height:100px;margin-top:10px}

.left{float:left;width:20%;height:200px;background:#DDD}

.right{float:right;width:30%;height:80px;background:#DDD}

/*清除浮动代码*/

.clearfloat{clear:both}

</style>

<p class="p1">

<p class="left">Left</p>

<p class="right">Right</p>

<p class="clearfloat"></p>

</p>

<p class="p2">

p2

</p>

Copy after login

Principle: add an empty p, use clear:both improved by css to clear the float, let The parent p can automatically obtain the height
Advantages: simple, less code, good browser support, not prone to strange problems
Disadvantages: many beginners do not understand the principle; if the page has a lot of floating layouts, it must be added There are many empty ps, which makes people feel very bad.
Recommendation: Not recommended, but this method was mainly used in the past to clear floats.
3. The parent p defines pseudo-classes: after and zoom

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<style type="text/css">

.p1{background:#000080;border:1px solid red;}

.p2{background:#800080;border:1px solid red;height:100px;margin-top:10px}

.left{float:left;width:20%;height:200px;background:#DDD}

.right{float:right;width:30%;height:80px;background:#DDD}

/*清除浮动代码*/

.clearfloat:after{display:block;clear:both;content:"";visibility:hidden;height:0}

.clearfloat{zoom:1}

</style>

<p class="p1 clearfloat">

<p class="left">Left</p>

<p class="right">Right</p>

</p>

<p class="p2">

p2

</p>

Copy after login

Principle: Only IE8 and above and non-IE browsers support:after. The principle is somewhat similar to method 2. zoom (IE conversion has attributes) can solve the floating problem of ie6 and ie7
Advantages: good browser support, no problem It is prone to strange problems (currently: used by large websites, such as: Tencent, NetEase, Sina, etc.)
Disadvantages: There are many codes, many beginners do not understand the principle, and two lines of code must be used in combination to allow mainstream browsing All devices support it.
Recommendation: It is recommended to use it. It is recommended to define public classes to reduce CSS code.
4, parent p defines overflow:hidden

1

2

3

4

5

6

7

8

9

10

11

12

13

<style type="text/css">

.p1{background:#000080;border:1px solid red;/*解决代码*/width:98%;overflow:hidden}

.p2{background:#800080;border:1px solid red;height:100px;margin-top:10px;width:98%}

.left{float:left;width:20%;height:200px;background:#DDD}

.right{float:right;width:30%;height:80px;background:#DDD}

</style>

<p class="p1">

<p class="left">Left</p>

<p class="right">Right</p>

</p>

<p class="p2">

p2

</p>

Copy after login

Principle: width or zoom:1 must be defined, and height cannot be defined. When using overflow:hidden, the browser will automatically check the height of the floating area
Advantages: simple, less code, good browser support
Disadvantages: cannot be used with position, because the exceeded size will be hidden.
Recommendation: Only recommended for friends who have not used position or have a deep understanding of overflow:hidden.
5, parent p defines overflow:auto

1

2

3

4

5

6

7

8

9

10

11

12

13

<style type="text/css">

.p1{background:#000080;border:1px solid red;/*解决代码*/width:98%;overflow:auto}

.p2{background:#800080;border:1px solid red;height:100px;margin-top:10px;width:98%}

.left{float:left;width:20%;height:200px;background:#DDD}

.right{float:right;width:30%;height:80px;background:#DDD}

</style>

<p class="p1">

<p class="left">Left</p>

<p class="right">Right</p>

</p>

<p class="p2">

p2

</p>

Copy after login

Principle: width or zoom:1 must be defined, and height cannot be defined. When using overflow:auto, the browser will automatically check the height of the floating area
Advantages: Simple, less code, good browser support
Disadvantages: When the internal width and height exceed the parent p, scroll bars will appear.
Recommendation: Not recommended, use it if you need scroll bars to appear or to ensure that scroll bars do not appear in your code.
6, the parent p also floats together

1

2

3

4

5

6

7

8

9

10

11

12

13

<style type="text/css">

.p1{background:#000080;border:1px solid red;/*解决代码*/width:98%;margin-bottom:10px;float:left}

.p2{background:#800080;border:1px solid red;height:100px;width:98%;/*解决代码*/clear:both}

.left{float:left;width:20%;height:200px;background:#DDD}

.right{float:right;width:30%;height:80px;background:#DDD}

</style>

<p class="p1">

<p class="left">Left</p>

<p class="right">Right</p>

</p>

<p class="p2">

p2

</p>

Copy after login

Principle: All codes float together and become a whole
Advantages: No advantages
Disadvantages: New floating problems will occur.
Suggestion: Not recommended for use, only for understanding.
7, the parent p defines display:table

1

2

3

4

5

6

7

8

9

10

11

12

13

<style type="text/css">

.p1{background:#000080;border:1px solid red;/*解决代码*/width:98%;display:table;margin-bottom:10px;}

.p2{background:#800080;border:1px solid red;height:100px;width:98%;}

.left{float:left;width:20%;height:200px;background:#DDD}

.right{float:right;width:30%;height:80px;background:#DDD}

</style>

<p class="p1">

<p class="left">Left</p>

<p class="right">Right</p>

</p>

<p class="p2">

p2

</p>

Copy after login

Principle: Turn the p attribute into a table
Advantages: No advantages
Disadvantages: New unknown problems will arise.
Suggestion: Not recommended for use, only for understanding.
8, add the br tag clear:both at the end

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<style type="text/css">

.p1{background:#000080;border:1px solid red;margin-bottom:10px;zoom:1}

.p2{background:#800080;border:1px solid red;height:100px}

.left{float:left;width:20%;height:200px;background:#DDD}

.right{float:right;width:30%;height:80px;background:#DDD}

.clearfloat{clear:both}

</style>

<p class="p1">

<p class="left">Left</p>

<p class="right">Right</p>

<br class="clearfloat" />

</p>

<p class="p2">

p2

</p>

Copy after login

Principle: The parent p defines zoom:1 to solve the IE floating problem, add the br tag clear:both at the end
Suggestion: Not recommended Use for understanding only.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

css Explanation of background:transparent

Explanation of css mouse style cursor

The above is the detailed content of How to clear floats using CSS. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap

Video Face Swap

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

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)

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.

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.

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

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.

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.

The role of float in css The role of float in css Apr 28, 2024 pm 01:51 PM

The float property in CSS allows elements to break out of the document flow and line up along the edge of their parent element, and is used to create and align text images, floating menu sidebars, and overlapping elements. The attribute values ​​​​of floating elements include left (left float), right (right float), none (clear float), and inherit (inherit). To prevent a floated element from causing the parent element to overflow, you can use the clearfix technique to add an empty element and clear the float.

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