Bootstrap - Clearfix



This chapter discusses the clearfix feature of helper classes. The clearfix feature in Bootstrap is used to clear floated elements within a container. When elements within a container are floated, the container's height can sometimes be collapsed, causing issues with the layout.


Here's an example of how to use the .clearfix class in Bootstrap:

Example

The example below shows the usage of clearfix. Without the clearfix, the wrapping div would not span around the buttons which would cause a broken layout.

You can edit and try running this code using the Edit & Run option.

<!DOCTYPE html>
<html lang="en">
    <head>
       <title>Bootstrap - Helper - Clearfix</title>
       <meta charset="UTF-8">
       <meta http-equiv="X-UA-Compatible" content="IE=edge">
       <meta name="viewport" content="width=device-width, initial-scale=1.0">
       <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
       <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
    </head>
    <body>
        <h4>Clearfix example</h4><br>
          <div class="bg-success clearfix">
          <button type="button" class="btn btn-secondary float-start">Button floated left</button>
          <button type="button" class="btn btn-secondary float-end">Button floated right</button>
          </div>
    </body>
</html>
Advertisements