How to clear floats with the "clearfix" hack with CSS?

CSSWeb DevelopmentFront End Technology

To clear floats with the clearfix hack using CSS, the code is as follows −

Example

 Live Demo

<!DOCTYPE html>
<html>
<head>
<style>
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   }
   div {
      border: 3px solid rgb(21, 0, 114);
      padding: 5px;
   }
   img {
      float: right;
   }
   .clearfix {
      overflow: auto;
   }
</style>
</head>
<body>
<h1>Clearfix hack example</h1>
<h2>Without Clearfix</h2>
<div>
<img
src="https://i.picsum.photos/id/17/300/300.jpg"
width="100"
height="100"/>
Sample Text
</div>
<h2 style="clear:right">With Clearfix</h2>
<div class="clearfix">
<img src="https://i.picsum.photos/id/17/300/300.jpg" width="100" height="100"/>
Sample Text
</div>
</body>
</html>

Output

The above code would produce the following output −

raja
Published on 07-May-2020 12:54:17
Advertisements