CSS clearfix

What is CSS Clearfix?

A CSS method known as clearfix helps maintain floated elements within containers, preventing them from collapsing in the presence of floated child elements. When elements are floated in CSS, they are taken out of the normal document flow, sometimes leading to issues with the layout of their parent container.

This scenario is quite common: when a container holds floated items without a specified height, it runs the risk of collapsing and disrupting the layout. Employ a clearfix technique to guarantee the container expands to accommodate its floated descendants.

A float clearing method, known as clearfix, is a technique used by an element to manage or clear its child elements without requiring extra markup. This approach addresses the issue that arises when multiple floated elements are positioned alongside each other.

If we adjust the alignment of a sidebar to the left of the primary content block, the components collapse and intersect. This scenario resembles when a nested element is floated and exceeds the height of its containing element, causing it to spill beyond the container.

By employing the clearfix technique, layout issues can be circumvented, ensuring that the floated children within the container are properly contained.

Typically, the clearfix is implemented by assigning a distinct class or pseudo-element to the container element. A common method to generate a clearing element following the container content involves leveraging the ::after pseudo-element.

Syntax

The syntax of clearfix in CSS is as follows:

Example

.clearfix::after {

  content: "";

  display: table;

  clear: both;

}

Use of Clearfix CSS

Avoiding Container Collapse is one of the key benefits of employing Clearfix in CSS.

A container that contains floated child elements could collapse, resulting in the loss of its ability to contain the floated children and potentially collapsing to a height of zero. This situation can lead to layout issues, causing elements to be displayed differently than anticipated.

  1. Managing Floats in a Container

When a container is filled with floated elements, it loses its ability to adjust height automatically based on the content of the floated items. To accommodate the floated children, the container expands with the help of the clearfix technique.

  1. Preserving Document Flow

Floating elements are taken out of the normal document flow. The clearfix method aids in restoring the regular flow by adding a clearing element after the floated content.

  1. Simplicity of Execution

Implementing the clearfix method is quite straightforward. Streamlining the container and incorporating a clearfix class or pseudo-element is a simple approach to resolving layout issues induced by floated elements.

  1. Ensuring Compatibility Across Different Browsers

Due to its widespread adoption and strong browser compatibility, Clearfix is effective across a variety of browsers, ensuring dependable and uniform layout rendering.

It's crucial to understand that the need for the classic clearfix method has somewhat diminished due to the emergence of contemporary layout strategies such as Flexbox and Grid. Nevertheless, clearfix remains valuable and is utilized in situations necessitating floats or when dealing with older designs.

Examples:

Example 1:

Example

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <link rel="stylesheet" href="styles.css">

  <title>Two-Column Layout</title>

</head>

<body>

  <header>

    <h1>Header</h1>

  </header>

  
  <nav>

    <ul>

      <li><a href="#">Home</a></li>

      <li><a href="#">About</a></li>

      <li><a href="#">Services</a></li>

      <li><a href="#">Contact</a></li>

    </ul>

  </nav>

  
  <div class="container clearfix">

    <main>

      <h2>Main Content</h2>

      <p>This is the main content area.</p>

      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed euismod, mauris vel aliquet luctus, felis elit varius orci, vitae hendrerit dui velit vel velit. Nulla facilisi. Praesent quis sem sed nisi ullamcorper consectetur. Ut volutpat tincidunt nisl, et rhoncus arcu finibus in.</p>

    </main>

    
    <aside>

      <h2>Sidebar</h2>

      <p>This is the sidebar.</p>

    </aside>

  </div>

  
  <footer>

    <p>© 2023 Example Company. All rights reserved.</p>

  </footer>

</body>

</html>
Example

body {

  font-family: Arial, sans-serif;

  margin: 0;

  padding: 0;

}


header, nav, main, aside, footer {

  padding: 10px;

  margin: 10px;

  border: 1px solid #ccc;

}


nav ul {

  list-style: none;

  padding: 0;

  margin: 0;

}


nav li {

  display: inline;

  margin-right: 10px;

}


.container::after {

  content: "";

  display: table;

  clear: both;

}


main {

  float: left;

  width: 70%;

  box-sizing: border-box;

}


aside {

  float: right;

  width: 28%;

  box-sizing: border-box;

}


footer {

  clear: both;

  text-align: center;

}

Output:

Example 2:

In this instance, the image is floated and has a greater height than the encompassing element, causing it to extend beyond the boundaries of its container.

Now, we are establishing a class named jtp and applying the overflow: auto; style to the relevant element.

Example

<!DOCTYPE html>  

<html>  

<head>  

<style>  

div {  

  border: 3px solid red;  

  padding: 5px;  

  background-color:pink;  

  font-size:20px;  

}  

  
img{  

  float: right;  

  border: 3px solid blue;  

    
}  

p{  

font-size:20px;  

clear:right;  

}  

.jtp{  

  overflow: auto;  

}  

</style>  

</head>  

<body>  

  
<h1>Without Clearfix</h1>  

<div>  

  <img class="img1" src="https://placehold.co/400x300/1abc9c/ffffff?text=Sample+Image">  

  Welcome to the C# Tutorial. Here, you can find the best tutorials that are easy to read and help you to clear your concepts.  

</div>  

<p>Use the overflow:auto; CSS property</p>  

<h1>With Clearfix</h1>  

<div class="jtp">  

  <img class="img2" src="https://placehold.co/400x300/1abc9c/ffffff?text=Sample+Image">  

  Welcome to the C# Tutorial. Here, you can find the best tutorials that are easy to read and help you to clear your concepts.  

</div>  

  
</body>  

</html>

The aforementioned clearfix technique is effective when handling paddings and margins properly. However, a more contemporary approach to clearing floats is considered to be more secure.

Example 3: Let's see another method of clearfix.

In this instance, we designate the clear attribute as 'both', indicating that the floated items will not be permitted on either the left or right side. The display attribute is specified as 'table', causing the element to function similarly to a table element in HTML. Additionally, the content should remain blank.

Example

<!DOCTYPE html>  

<html>  

<head>  

<style>  

div {  

  border: 3px solid red;  

  padding: 5px;  

  background-color:pink;  

  font-size:20px;  

}  

  
img{  

  float: right;  

  border: 3px solid blue;  

}  

p{  

font-size:20px;  

clear:right;  

}  

.jtp:after{  

content:'.';  

clear:both;  

display: table;  

}  

</style>  

</head>  

<body>  

  
<h1>Without Clearfix</h1>  

<div>  

  <img src="https://placehold.co/400x300/1abc9c/ffffff?text=Sample+Image">  

  Welcome to the C# Tutorial. Here, you can find the best tutorials that are easy to read and help you to clear your concepts.  

</div>  

<p>Another clearfix method</p>  

<h1>With Clearfix</h1>  

<div class="jtp">  

  <img src="https://placehold.co/400x300/1abc9c/ffffff?text=Sample+Image">  

  Welcome to the C# Tutorial. Here, you can find the best tutorials that are easy to read and help you to clear your concepts.  

</div>  

  
</body>  

</html>

Input Required

This code uses input(). Please provide values below: