The float attribute floats the element on the page and displays it side by side with adjacent elements without affecting the regular fluid layout. The float values are: left (left float), right (right float), none (clear float). Floated elements affect content overflow, spacing, and parent container height. Methods for clearing floats include: clearing attributes, floating elements, and overflow: hidden.
Usage of float in CSS
float definition
CSS The float property positions the element outside its content box, causing it to float on the page. Floated elements do not affect the regular flow layout of other elements, but appear side by side with their adjacent elements.
float value
The float attribute can accept the following values:
Usage
To float an element, add the float attribute to its style:
<code class="css">element { float: left; }</code>
Influence
Floating elements will affect their nearby elements:
Clear Floats
To clear floats, you can use one of the following methods:
Example
The following code creates a container with two floating elements:
<code class="html"><div class="container"> <div class="element1">元素 1</div> <div class="element2">元素 2</div> </div></code>
<code class="css">.container { display: flex; } .element1 { float: left; background: red; width: 100px; } .element2 { float: right; background: blue; width: 100px; }</code>
Output:
<code>元素 1 | 元素 2</code>
The above is the detailed content of Usage of float in css. For more information, please follow other related articles on the PHP Chinese website!