CSS Properties
CSS clear Property
Photo Credit to CodeToFun
🙋 Introduction
The clear
property in CSS is used to control the behavior of floating elements. When elements are floated using the float property, they can affect the layout of subsequent elements.
The clear
property specifies whether an element should be moved below (cleared) floating elements that precede it.
💡 Syntax
The syntax for the clear
property is straightforward. You can specify the direction in which the element should be cleared.
element {
clear: value;
}
🎛️ Default Value
The default value of the clear
property is none, meaning that elements do not clear past any floating elements unless explicitly specified.
🏠 Property Values
Value | Description |
---|---|
none | The element is not moved down to clear past floating elements. This is the default value. |
left | The element is moved down to clear past left-floating elements. |
right | The element is moved down to clear past right-floating elements. |
both | The element is moved down to clear past both left and right-floating elements. |
inline-start | The element is moved down to clear past floating elements on the start side of the inline dimension. |
inline-end | The element is moved down to clear past floating elements on the end side of the inline dimension. |
📄 Example
In this example, we'll demonstrate how the clear
property works with floating elements.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS clear Property Example</title>
<style>
.box {
width: 100px;
height: 100px;
margin: 10px;
float: left;
background-color: lightblue;
}
.clear {
clear: both;
background-color: lightgreen;
padding: 10px;
}
</style>
</head>
<body>
<h1>Using the CSS clear Property</h1>
<div class="box">Box 1</div>
<div class="box">Box 2</div>
<div class="clear">Cleared Content</div>
</body>
</html>
In this example, the clear
property is used to ensure that the "Cleared Content" section moves below the floating boxes.
🖥️ Browser Compatibility
The clear
property is supported in all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. It has wide compatibility and can be used reliably in web development.
🎉 Conclusion
The clear
property is an essential tool for managing the layout of elements in the presence of floating content.
By using this property, you can control the flow of elements and prevent unwanted overlap or misalignment caused by floating elements. Understanding and utilizing the clear
property effectively can help you create more precise and visually appealing layouts in your web projects.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (CSS clear Property), please comment here. I will help you immediately.