- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is a clearfix?
The clearfix, as the name suggests, is used to clear floats. It is generally used in float layouts. The clearfix is considered a hack to clear floats.
Overflow Issue
Example
Let us see the problem first before moving towards the solution. We have an image here, floated to the right. It overflows outside its container since it is way taller than the element belonging to it −
<!DOCTYPE html> <html> <head> <style> div { border: 2px solid blue; padding: 5px; } .myimg { float: right; } </style> </head> <body> <h2>Demo Heading</h2> <p>We haven't used clearfix below:</p> <div> <img class="myimg" src="https://www.tutorialspoint.com/machine_learning_with_python/images/machine-learning-with-python-mini-logo.jpg" alt="Machine Learning" width="150" height="150"> Etiam accumsan metus sapien, rutrum sagittis nunc posuere eu. Ut facilisis tortor eget justo scelerisque, quis porta nisl sagittis. </div> </body> </html>
The output displays the overflow issue −
Fix with Clearfix using overflow auto
Example
Let us now fix the issue with clearfix −
<!DOCTYPE html> <html> <head> <style> div { border: 2px solid blue; padding: 5px; } .myimg { float: right; } .clearfix { overflow: auto; } </style> </head> <body> <h2 style="clear:right">Demo Heading</h2> <p>We have used clearfix below:</p> <div class="clearfix"> <img class="myimg" src="https://www.tutorialspoint.com/machine_learning_with_python/images/machine-learning-with-python-mini-logo.jpg" alt="Machine Learning" width="150" height="150"> Etiam accumsan metus sapien, rutrum sagittis nunc posuere eu. Ut facilisis tortor eget justo scelerisque, quis porta nisl sagittis. </div> </body> </html>
Fix with Clearfix using ::after selector
Example
The ::after selector is used here to fix the clearfix −
<!DOCTYPE html> <html> <head> <style> div { border: 2px solid blue; padding: 5px; } .myimg { float: right; } .clearfix::after { content: ""; clear: both; display: table; } </style> </head> <body> <h2 style="clear:right">Demo Heading</h2> <p>We have used clearfix below:</p> <div class="clearfix"> <img class="myimg" src="https://www.tutorialspoint.com/machine_learning_with_python/images/machine-learning-with-python-mini-logo.jpg" alt="Machine Learning" width="150" height="150"> Etiam accumsan metus sapien, rutrum sagittis nunc posuere eu. Ut facilisis tortor eget justo scelerisque, quis porta nisl sagittis. </div> </body> </html>
- Related Articles
- Clearfix Bootstrap class
- How to clear floats with the "clearfix" hack with CSS?
- What is a Periscope and what is its ;use?
- What is a periodic table? What is its importance?
- What is a Litecoin?
- What is a satire?
- What is a molecule?
- What is a rectangle?
- What is a parallelogram?
- What is a biosphere?
- What is a Crimeware?
- What is a teenager?
- What Is (a² - b² )
- what is a microorganism?
- What is a Magnet?

Advertisements