There are two types of attributes used to clear floats in css: 1. The clear attribute can define which side of the element is not allowed to float. It is usually written as "clear: both;" which means that neither side of the element is allowed to float. , which clears the float. 2. Overflow attribute, defining the "overflow:hidden;" style for the parent element can also clear floating.
The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.
What is CSS clear float?
In non-IE browsers (such as Firefox), when the height of the container is auto and there are floating (float is left or right) elements in the container content, in this case In this case, the height of the container cannot automatically extend to adapt to the height of the content, causing the content to overflow outside the container and affect (or even destroy) the layout. This phenomenon is called float overflow, and the CSS processing performed to prevent this phenomenon is called CSS clear float.
Quoting the W3C example, the news container does not surround floating elements.
.news { background-color: gray; border: solid 1px black; } .news img { float: left; } .news p { float: right; } <div> <img src="/static/imghwm/default1.png" data-src="news-pic.jpg" class="lazy" alt="What attribute is used to clear float in css" > <p>some text</p> </div>
Clear floating method
Method 1: Use an empty element with clear attribute
Use an empty element after the floating element such as
, and assign .clear{clear:both;} in CSS Properties can be used to clear floats. You can also use
or
to clean.
.news { background-color: gray; border: solid 1px black; } .news img { float: left; } .news p { float: right; } .clear { clear: both; } <div> <img src="/static/imghwm/default1.png" data-src="news-pic.jpg" class="lazy" alt="What attribute is used to clear float in css" > <p>some text</p> <div></div> </div>
Advantages: simple, less code, good browser compatibility.
Disadvantages: A large number of unsemantic html elements need to be added, the code is not elegant enough, and it is not easy to maintain later.
Method 2: Use the overflow attribute of CSS
Add overflow:hidden; or overflow:auto; to the container of the floating element to clear the float. In addition, in IE6 HasLayout needs to be triggered, such as setting the container width and height for the parent element or setting zoom:1.
After adding the overflow attribute, the floating element returns to the container layer, raising the height of the container, achieving the effect of cleaning up the floating elements.
.news { background-color: gray; border: solid 1px black; overflow: hidden; *zoom: 1; } .news img { float: left; } .news p { float: right; } <div> <img src="/static/imghwm/default1.png" data-src="news-pic.jpg" class="lazy" alt="What attribute is used to clear float in css" > <p>some text</p> </div>
Method 3: Add floats to the container of floating elements
Add the floating attribute to the container of floating elements to clear the internal floats, but this will cause It floats as a whole and affects the layout, so it is not recommended.
Method 4: Use adjacent element processing
Do nothing and add the clear attribute to the element behind the floating element.
.news { background-color: gray; border: solid 1px black; } .news img { float: left; } .news p { float: right; } .content{ clear:both; } <div> <img src="/static/imghwm/default1.png" data-src="news-pic.jpg" class="lazy" alt="What attribute is used to clear float in css" > <p>some text</p> <div></div> </div>
Method Five: Use CSS:after pseudo-element
Combined with:after pseudo-element (note that this is not a pseudo-class, but a pseudo-element, which represents after an element Recent elements) and IEhack, which are perfectly compatible with all major current mainstream browsers. IEhack here refers to triggering hasLayout.
Add a clearfix class to the container of the floating element, and then add a :after pseudo-element to this class to add an invisible block element (Block element) to the end of the element to clean up the float.
.news { background-color: gray; border: solid 1px black; } .news img { float: left; } .news p { float: right; } .clearfix:after{ content: "020"; display: block; height: 0; clear: both; visibility: hidden; } .clearfix { /* 触发 hasLayout */ zoom: 1; } <div> <img src="/static/imghwm/default1.png" data-src="news-pic.jpg" class="lazy" alt="What attribute is used to clear float in css" > <p>some text</p> </div>
Add an invisible space "020" or dot "." at the end of the internal element of the container through CSS pseudo-elements, and assign the clear attribute to clear the float. It should be noted that for IE6 and IE7 browsers, a zoom:1; must be added to the clearfix class to trigger haslayout.
Summary
Through the above example, we can easily find that the methods of clearing floats can be divided into two categories:
One It uses the clear attribute, including adding an empty div with the clear: both attribute at the end of the floating element to close the element. In fact, the method of using the :after pseudo-element is to add a dot with the content at the end of the element and the clear: both attribute. implemented by the elements.
The second is to trigger the BFC (Block Formatting Contexts, block-level formatting context) of the parent element of the floating element, so that the parent element can contain floating elements. Regarding this point.
(Learning video sharing: css video tutorial, web front-end)
The above is the detailed content of What attribute is used to clear float in css. For more information, please follow other related articles on the PHP Chinese website!

React is a front-end framework for building user interfaces; a back-end framework is used to build server-side applications. React provides componentized and efficient UI updates, and the backend framework provides a complete backend service solution. When choosing a technology stack, project requirements, team skills, and scalability should be considered.

The relationship between HTML and React is the core of front-end development, and they jointly build the user interface of modern web applications. 1) HTML defines the content structure and semantics, and React builds a dynamic interface through componentization. 2) React components use JSX syntax to embed HTML to achieve intelligent rendering. 3) Component life cycle manages HTML rendering and updates dynamically according to state and attributes. 4) Use components to optimize HTML structure and improve maintainability. 5) Performance optimization includes avoiding unnecessary rendering, using key attributes, and keeping the component single responsibility.

React is the preferred tool for building interactive front-end experiences. 1) React simplifies UI development through componentization and virtual DOM. 2) Components are divided into function components and class components. Function components are simpler and class components provide more life cycle methods. 3) The working principle of React relies on virtual DOM and reconciliation algorithm to improve performance. 4) State management uses useState or this.state, and life cycle methods such as componentDidMount are used for specific logic. 5) Basic usage includes creating components and managing state, and advanced usage involves custom hooks and performance optimization. 6) Common errors include improper status updates and performance issues, debugging skills include using ReactDevTools and Excellent

React is a JavaScript library for building user interfaces, with its core components and state management. 1) Simplify UI development through componentization and state management. 2) The working principle includes reconciliation and rendering, and optimization can be implemented through React.memo and useMemo. 3) The basic usage is to create and render components, and the advanced usage includes using Hooks and ContextAPI. 4) Common errors such as improper status update, you can use ReactDevTools to debug. 5) Performance optimization includes using React.memo, virtualization lists and CodeSplitting, and keeping code readable and maintainable is best practice.

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

React components can be defined by functions or classes, encapsulating UI logic and accepting input data through props. 1) Define components: Use functions or classes to return React elements. 2) Rendering component: React calls render method or executes function component. 3) Multiplexing components: pass data through props to build a complex UI. The lifecycle approach of components allows logic to be executed at different stages, improving development efficiency and code maintainability.

React Strict Mode is a development tool that highlights potential issues in React applications by activating additional checks and warnings. It helps identify legacy code, unsafe lifecycles, and side effects, encouraging modern React practices.

React Fragments allow grouping children without extra DOM nodes, enhancing structure, performance, and accessibility. They support keys for efficient list rendering.


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

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft