Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Topics

CSS clear Property

Posted in CSS Tutorial
Updated on Oct 02, 2024
By Mari Selvan
👁️ 30 - Views
⏳ 4 mins
💬 1 Comment
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.

Syntax
Copied
Copy To Clipboard
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

ValueDescription
noneThe element is not moved down to clear past floating elements. This is the default value.
leftThe element is moved down to clear past left-floating elements.
rightThe element is moved down to clear past right-floating elements.
bothThe element is moved down to clear past both left and right-floating elements.
inline-startThe element is moved down to clear past floating elements on the start side of the inline dimension.
inline-endThe 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.

index.html
Copied
Copy To Clipboard
<!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:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

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

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy