search
HomeWeb Front-endFront-end Q&AWhat is the attribute that clears floats in html5?

What is the attribute that clears floats in html5?

Dec 22, 2021 pm 03:44 PM
clear attributehtml5clear float

In HTML5, the attribute to clear floats is "clear". The clear attribute specifies which side of the element does not allow other floating elements. When the "clear:both;" style is set to the floating element, the floating element can be cleared so that neither the left or right sides of the element are allowed to float.

What is the attribute that clears floats in html5?

The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.

In HTML5, the attribute to clear floats is "clear".

The clear attribute specifies which side of the element does not allow other floating elements

Let’s take a closer look at the clear attribute.

First of all, you must know that div is a block-level element. It occupies an exclusive line on the page and is arranged from top to bottom, which is the legendary flow. As shown below:

It can be seen that even if the width of div1 is very small, one line in the page can accommodate div1 and div2, and div2 will not be ranked behind div1, because div Elements are on their own line.

Note that the above theories refer to divs in the standard flow.

Xiaocai believes that no matter how complex the layout is, the basic starting point is: "How to display multiple div elements in one line."

It is obvious that the standard stream can no longer meet the demand, so floats must be used.

Floating can be understood as letting a DIV element from getting away from the standard flow, floating on the standard flow, and the standard flow is not a level.

For example, assuming that div2 in the picture above floats, it will break away from the standard flow, but div1, div3, and div4 are still in the standard flow, so div3 will automatically move upward, occupy the position of div2, and re-form a flow. As shown in the picture:

As can be seen from the picture, since div2 is set to float, it no longer belongs to the standard flow. div3 automatically moves up to replace the position of div2. div1, div3 and div4 are arranged in sequence and become a new flow. And because floats float above the standard flow, div2 blocks part of div3, and div3 looks "short".

Here div2 uses left floating (float:left;), which can be understood as floating and arranged to the left. Floating to the right (float:right;) is of course arranged to the right. The left and right here refer to the left and right edges of the page.

If we float div2 to the right, the effect will be as follows:

At this time, div2 is arranged on the right edge of the page and no longer blocks div3. Readers can You can clearly see the flow composed of div1, div3, and div4 mentioned above.

So far we have only floated one div element, how about more?

Next, we add left floating to both div2 and div3. The effect is as follows:

Similarly, since div2 and div3 are floating, they no longer belong to Standard flow, so div4 will automatically move up and form a "new" standard flow with div1, and floating is floating on the standard flow, so div2 blocks div4.

Ahem, here comes the point. When div2 and div3 are set to float at the same time, div3 will follow div2. I don’t know if readers have noticed that until now, div2 is floating in every example. , but it does not follow div1. Therefore, we can draw an important conclusion:

If a certain div element A is floating, and if the previous element of the A element is also floating, then the A element will follow the previous element. (If these two elements cannot be placed in one row, the A element will be squeezed to the next row); if the previous element of the A element is an element in the standard flow, then the relative vertical position of A will not change, that is to say, the top of A Always aligned with the bottom of the previous element.

The order of divs is determined by the order of divs in the HTML code.

The end close to the edge of the page is the front, and the end far away from the edge of the page is the back.

To help readers understand, here are a few more examples.

If we set div2, div3, and div4 to float left, the effect is as follows:

Based on the above conclusion, follow Xiao Cai to understand it: start with the analysis of div4, it found that the upper element div3 is floating, so div4 will follow div3; div3 found that the upper element div2 is also floating, so div3 will Following div2; and div2 finds that the upper element div1 is an element in the standard flow, so the relative vertical position of div2 remains unchanged, and the top is still aligned with the bottom of the div1 element. Since it is left floating, the left side is close to the edge of the page, so the left side is the front, so div2 is on the far left.

If div2, div3, and div4 are all set to float right, the effect is as follows:

The principle is basically the same as floating left, but you need to pay attention to the before and after Correspondence. Since it is floating right, the right side is close to the edge of the page, so the right side is the front, so div2 is on the far right.

If we float div2 and div4 to the left, the rendering is as follows:

It is still based on the conclusion that div2 and div4 are floating and deviated from the standard flow, so div3 will automatically move up and form a standard flow with div1. div2 finds that the previous element div1 is an element in the standard flow, so the relative vertical position of div2 remains unchanged and is aligned with the bottom of div1. div4 finds that the previous element div3 is an element in the standard flow, so the top of div4 is aligned with the bottom of div3, and this is always true, because as can be seen from the picture, after div3 moves up, div4 also moves up, and div4 always moves up. It is to ensure that the top of itself is aligned with the bottom of the previous element div3 (an element in the standard flow).

At this point, congratulations to the readers who have mastered adding floats, but there is also clearing floats. Clearing floats is very easy to understand based on the above foundation.

After the above study, it can be seen that before the elements are floated, that is, in the standard flow, they are arranged vertically, and after floating, they can be understood as horizontal arrangements.

Clearing floats can be understood as breaking the horizontal arrangement.

The keyword for clearing floats is clear. The official definition is as follows:

Syntax:

clear : none | left | right | both

Value:

none: Default value. Floating objects are allowed on both sides

left : Floating objects are not allowed on the left side

right : Floating objects are not allowed on the right side

both : Floating objects are not allowed

The definition is very easy to understand, but readers may find that this is not the case when using it in practice.

There is nothing wrong with the definition, but it is too vague and leaves us at a loss.

Based on the above basis, if there are only two elements div1 and div2 on the page, they are both left-floating. The scenario is as follows:

At this time div1, Both div2 are floating. According to the rules, div2 will follow div1, but we still want div2 to be arranged below div1, just like div1 is not floating and div2 is floating left.

At this time, clear float (clear) is used. If it is purely based on the official definition, readers may try to write like this: add clear:right; to the CSS style of div1, which means that the right side of div1 is not allowed. There are floating elements. Since div2 is a floating element, it will automatically move down one line to meet the rules.

In fact, this understanding is incorrect, and it has no effect. Let’s take a look at Xiaocai’s conclusion:

        Regarding CSS clear float (clear), you must remember: this rule can only affect the element itself that is cleared, and cannot affect other elements.

How to understand? Take the above example, we want div2 to move, but we use clear float in the CSS style of div1 element, trying to force div2 to move down by clearing the floating element on the right side of div1 (clear:right;) , this is not feasible, because this clear float is called in div1, it can only affect div1, not div2.

According to Xiaocai’s conclusion, if you want div2 to move down, you must use float in the CSS style of div2. In this example, there is a floating element div1 on the left side of div2, so as long as you use clear:left; in the CSS style of div2 to specify that floating elements are not allowed to appear on the left side of the div2 element, div2 will be forced to move down one line.

So what if there are only two elements div1 and div2 on the page, and they are both right-floating? Readers should be able to guess the scene by themselves at this time, as follows:

If you want div2 to move down to div1, what should you do?

Similarly based on Xiaocai’s conclusion, if we want to move div2, we must call float in the CSS style of div2, because float can only affect the element that calls it.

It can be seen that there is a floating element div1 on the right side of div2, then we can use clear:right; in the CSS style of div2 to specify that floating elements are not allowed to appear on the right side of div2, so that div2 is forced to move down. One row, arranged below div1.

Related recommendations: "html video tutorial"

The above is the detailed content of What is the attribute that clears floats in html5?. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
React: The Power of a JavaScript Library for Web DevelopmentReact: The Power of a JavaScript Library for Web DevelopmentApr 18, 2025 am 12:25 AM

React is a JavaScript library developed by Meta for building user interfaces, with its core being component development and virtual DOM technology. 1. Component and state management: React manages state through components (functions or classes) and Hooks (such as useState), improving code reusability and maintenance. 2. Virtual DOM and performance optimization: Through virtual DOM, React efficiently updates the real DOM to improve performance. 3. Life cycle and Hooks: Hooks (such as useEffect) allow function components to manage life cycles and perform side-effect operations. 4. Usage example: From basic HelloWorld components to advanced global state management (useContext and

React's Ecosystem: Libraries, Tools, and Best PracticesReact's Ecosystem: Libraries, Tools, and Best PracticesApr 18, 2025 am 12:23 AM

The React ecosystem includes state management libraries (such as Redux), routing libraries (such as ReactRouter), UI component libraries (such as Material-UI), testing tools (such as Jest), and building tools (such as Webpack). These tools work together to help developers develop and maintain applications efficiently, improve code quality and development efficiency.

React and Frontend Development: A Comprehensive OverviewReact and Frontend Development: A Comprehensive OverviewApr 18, 2025 am 12:23 AM

React is a JavaScript library developed by Facebook for building user interfaces. 1. It adopts componentized and virtual DOM technology to improve the efficiency and performance of UI development. 2. The core concepts of React include componentization, state management (such as useState and useEffect) and the working principle of virtual DOM. 3. In practical applications, React supports from basic component rendering to advanced asynchronous data processing. 4. Common errors such as forgetting to add key attributes or incorrect status updates can be debugged through ReactDevTools and logs. 5. Performance optimization and best practices include using React.memo, code segmentation and keeping code readable and maintaining dependability

The Power of React in HTML: Modern Web DevelopmentThe Power of React in HTML: Modern Web DevelopmentApr 18, 2025 am 12:22 AM

The application of React in HTML improves the efficiency and flexibility of web development through componentization and virtual DOM. 1) React componentization idea breaks down the UI into reusable units to simplify management. 2) Virtual DOM optimization performance, minimize DOM operations through diffing algorithm. 3) JSX syntax allows writing HTML in JavaScript to improve development efficiency. 4) Use the useState hook to manage state and realize dynamic content updates. 5) Optimization strategies include using React.memo and useCallback to reduce unnecessary rendering.

Understanding React's Primary Function: The Frontend PerspectiveUnderstanding React's Primary Function: The Frontend PerspectiveApr 18, 2025 am 12:15 AM

React's main functions include componentized thinking, state management and virtual DOM. 1) The idea of ​​componentization allows splitting the UI into reusable parts to improve code readability and maintainability. 2) State management manages dynamic data through state and props, and changes trigger UI updates. 3) Virtual DOM optimization performance, update the UI through the calculation of the minimum operation of DOM replica in memory.

Frontend Development with React: Advantages and TechniquesFrontend Development with React: Advantages and TechniquesApr 17, 2025 am 12:25 AM

The advantages of React are its flexibility and efficiency, which are reflected in: 1) Component-based design improves code reusability; 2) Virtual DOM technology optimizes performance, especially when handling large amounts of data updates; 3) The rich ecosystem provides a large number of third-party libraries and tools. By understanding how React works and uses examples, you can master its core concepts and best practices to build an efficient, maintainable user interface.

React vs. Other Frameworks: Comparing and Contrasting OptionsReact vs. Other Frameworks: Comparing and Contrasting OptionsApr 17, 2025 am 12:23 AM

React is a JavaScript library for building user interfaces, suitable for large and complex applications. 1. The core of React is componentization and virtual DOM, which improves UI rendering performance. 2. Compared with Vue, React is more flexible but has a steep learning curve, which is suitable for large projects. 3. Compared with Angular, React is lighter, dependent on the community ecology, and suitable for projects that require flexibility.

Demystifying React in HTML: How It All WorksDemystifying React in HTML: How It All WorksApr 17, 2025 am 12:21 AM

React operates in HTML via virtual DOM. 1) React uses JSX syntax to write HTML-like structures. 2) Virtual DOM management UI update, efficient rendering through Diffing algorithm. 3) Use ReactDOM.render() to render the component to the real DOM. 4) Optimization and best practices include using React.memo and component splitting to improve performance and maintainability.

See all articles

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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.