This fragment is about to be reported (you'll remain on this page): You can enter a comment to clarify the mistake if you would like to: |
It's quite often to have floating blocks and it almost equally often you need to clear them (i.e. let the following content start after their end). However, it's not always possible to insert <div style="clear: both"></div> all around your page's code simply because you might have no control over its generation.
Luckly for us, major browsers support a nifty CSS 2 selector – :after pseudoclass. And we can use it to simulate a trailing clearing <div> just like shown before.
xml<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <style> img { float: left; } div:after { content: ""; display: block; clear: left; } p { background: red; } div { background: gray; } </style> </head> <body> <img src="pic.jpg" /> <p>This is some text.</p> <div style="clear: both"></div> <div> <img src="pic.jpg" /> <p>This is some text.</p> </div> </body> </html>
And here's what it produces – paragraphs have red background and divs have white. The first div isn't cleared so its content overflows the bottom border while the second successfully clears the contained left-floating image.
Comments