111

I have a list and the li's have a float:left;. The contents after the <ul> should be aligned correctly. Therefore i can build the following:

http://jsfiddle.net/8unU8/

I thought, that I can remove the <div class="clear"> by using pseudo selectors like :after.

But the example is not working:

http://jsfiddle.net/EyNnk/

Do I always have to create a separate div to clear floating elements?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Torben
  • 5,388
  • 12
  • 46
  • 78

5 Answers5

274

Write like this:

.wrapper:after {
    content: '';
    display: block;
    clear: both;
}

Check this http://jsfiddle.net/EyNnk/1/

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
sandeep
  • 91,313
  • 23
  • 137
  • 155
  • This is how to do it. but please note the point in my answer about semantics. – Alex May 22 '12 at 09:36
  • 2
    Just a minor info: `::after` with double colon is the recommended way of targeting pseudo-elements. – Dennis98 Mar 21 '16 at 03:33
  • 12
    Every browser that supports the double colon (::) CSS3 syntax also supports just the (:) syntax, but IE 8 only supports the single-colon, so for now, it's recommended to just use the single-colon for best browser support. :: is the newer format indented to distinguish pseudo content from pseudo selectors. If you don't need IE 8 support, feel free to use the double-colon. https://css-tricks.com/almanac/selectors/a/after-and-before/ – Pbinder Dec 07 '16 at 07:15
  • why should we to put this: "content: ''; display: block;" ? and what if you want a inline wrapper ? – Lucke Apr 12 '19 at 12:28
6

No, you don't it's enough to do something like this:

<ul class="clearfix">
  <li>one</li>
  <li>two></li>
</ul>

And the following CSS:

ul li {float: left;}


.clearfix:after {
  content: ".";
  display: block;
  clear: both;
  visibility: hidden;
  line-height: 0;
  height: 0;
}

.clearfix {
  display: inline-block;
}

html[xmlns] .clearfix {
   display: block;
}

* html .clearfix {
   height: 1%;
}
kernelpanic
  • 2,876
  • 3
  • 34
  • 58
5

This will work as well:

.clearfix:before,
.clearfix:after {
    content: "";
    display: table;
} 

.clearfix:after {
    clear: both;
}

/* IE 6 & 7 */
.clearfix {
    zoom: 1;
}

Give the class clearfix to the parent element, for example your ul element.

Sources here and here.

Mahdi
  • 9,247
  • 9
  • 53
  • 74
0

The text 'dasda' will never not be within a tag, right? Semantically and to be valid HTML it as to be, just add the clear class to that:

http://jsfiddle.net/EyNnk/2/

Alex
  • 8,875
  • 2
  • 27
  • 44
  • 1
    It's not semantics at all. 1) We always clear parent element if his child have float on it for example as per your answer if i want to give background into the UL it's not cover the LI's because UL is not clear & 2) I have a question Why you give clear Class in a separate DIV before P. if you give to P only it's also work – sandeep May 22 '12 at 10:15
0

Use

.wrapper:after {
    content : '\n';
}

Much like solution provided by Roko. It allows to insert/change content using : after and :before psuedo. For details check http://www.quirksmode.org/css/content.html

Mike Caron
  • 14,351
  • 4
  • 49
  • 77
sachin kumar
  • 159
  • 1
  • 1
  • 9