3

This (http://jsfiddle.net/77RRA/1/) is working, while this (http://jsfiddle.net/77RRA/) is not.

Isn't clearfix supposed to substitute the line <div style="clear: both;"></div>?

3 Answers3

5

Isn't clearfix supposed to substitute the line <div style="clear: both;"></div>

Yes. The clearfix is there to avoid a non-semantic empty tag. However, for this to work you need to place it on the parent element. (Example)

In your case however, it does not address the problem that siblings will ignore the floated element. This is not the intend of clearfix, you simply add clear:right (or both as you will) on your #child sibling to restore the normal document flow.

your fixed Example

Christoph
  • 50,121
  • 21
  • 99
  • 128
  • maybe you can help me understand this then? http://jsfiddle.net/BenRacicot/owtqncfb/ – Ben Racicot Feb 06 '15 at 22:11
  • @BenRacicot as I tried to explain, a clearfix is supposed to restore the *parents height*. If you want to restore the document flow you just declare `clear:both;` on the sibling element instead - this however still does not fix the issue, that floated elements are taken out of the normal document flow! Have a look at your modified example: http://jsfiddle.net/owtqncfb/9/ – Christoph Feb 08 '15 at 04:00
  • Thanks for responding! So the parent's height must be accounted for with overflow or a clearfix itself...? – Ben Racicot Feb 08 '15 at 14:16
  • 1
    @BenRacicot technically spoken, you have to create a new [block formatting context](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Block_formatting_context) so it "recognizes" it's floated child elements and the correct height is calculated. – Christoph Feb 08 '15 at 17:17
1

"Isn't clearfix supposed to substitute the line <div style="clear: both;"></div>?"

  • No

Imagine you have a container holding several items. If all of those items are floating, the container effectively loses its information of height. So margin-bottoms and background-styles are being displayed wrong. The clearfix solves this problem by adding pseudo-elements before and after the container element + setting a display: table; to stretch it back to its full height.

In your case, you will have to add a clear: both; on #child

Kyle
  • 65,599
  • 28
  • 144
  • 152
  • 1
    -1 Not true, clearfix *is* there to remove those unsemantic element, however it does not apply to the OP's problem! – Christoph Jun 14 '13 at 13:41
0

In your case , you are trying to clear floatting element from itself (with a pseudo-element that belongs to itself).

Clear should be on elements following floatting elements.

Some other rules can achieve this too.

http://jsfiddle.net/77RRA/6/

#main {
    background: lightgreen;
    width: 100px;
    height: 200px;
}

#one {
    float: right;
    display: block;
}

#child {
    background: red;
    width: 100%;
    height: 20px;
    display:inline-block
}

display:inline-block; will clear this element from floatting elements any sides.

G-Cyrillus
  • 101,410
  • 14
  • 105
  • 129
  • While `display:inline-block;` is possible, it's not feasible to use it, since sometimes you need another display type and after all that's what `clear` is there for. – Christoph Jun 14 '13 at 15:14
  • it's fine for the example (i removed :after and added just that rule) I believe , especially for beginner that it is usefull to realized that clear is not the only option to deal with flotting element. Floatting element element has behavior that can be very tricky to understand among fixed, absolute, relative and static. It's a lot to get at once, some clues help to learn by yourself. – G-Cyrillus Jun 14 '13 at 15:21
  • If you can explain *why* this works, then I might accept your point, otherwise I would just consider this as another possibility to confuse people. :) – Christoph Jun 15 '13 at 10:34
  • inline-block, as overflow:hidden, as display:table, modify the layout of elements. When receving one of this rules, the calculation of area is done according to areas unused by floattings elements (if any). this floatting elements can stand ahead in the flow, or being child. (sorry for my horrible english) – G-Cyrillus Jun 15 '13 at 10:41
  • in your example i used inline-block, cause you gave already 100% of width :) – G-Cyrillus Jun 15 '13 at 10:46