CSS · CSS Tutorial · Lesson 25
CSS Float
float and clear.
The float property positions an element to the left or right of its container, allowing text to wrap around it. clear stops elements from wrapping.
Example
<style>
img { float: right; margin-left: 12px; }
.clearfix::after { content: ""; display: table; clear: both; }
</style>
<div class="clearfix" style="border:1px solid #ccc; padding:8px;">
<img src="https://picsum.photos/id/1025/120/90" alt="Dog">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor.
</div>
<p style="clear:both">This paragraph starts below the floated image.</p>
Tip: Today, flexbox and grid are better for page layout. Float is mainly used to wrap text around images.
Keep reading “CSS Float”
Sign up to read the full lesson, try every example in the live editor, and take the 5-question quiz.