display: inline-flex
When you use display: inline-flex , you install the flex container.
The initial setting for the flex container is flex-direction: row .
This means that all children in the stream in the container (including pseudo-elements in the stream) will line up in a row. The display value of these children ( table in this case) is overridden / ignored according to the rules of the flex formatting context .
Your flexible container has two flexible elements (pseudo-elements) in one line:
.a, .b { border: 1px solid red; } .a { text-align: center; } .b { display: inline-flex; height: 20px; width: 20px; } .cf:before, .cf:after { content: "x"; display: table; } .cf:after { clear: both; }
<div class="a"> <div class="b cf"></div> </div>
display: inline-block
When you use display: inline-block , you set the formatting block .
The display property of child elements is respected.
Your pseudo-elements with display: table are block elements that by default occupy the full available width. Therefore, pseudo-hosts create two lines:
.a, .b { border: 1px solid red; } .a { text-align: center; } .b { display: inline-block; height: 20px; width: 20px; } .cf:before, .cf:after { content: "x"; display: table; } .cf:after { clear: both; }
<div class="a"> <div class="b cf"></div> </div>
vertical-align: baseline
Since both versions of your code use display inline-level values, this causes the vertical-align property to be played, the initial value of which is baseline .
The empty space that you see below div.b when setting display: inline-flex is due to alignment of the baseline.
The empty space that you see below div.b when set to display: inline-block is due to the alignment of the baseline in combination with the effects of two children of the block.
Here is a more detailed explanation: fooobar.com/questions/385890 / ...
Clear property
.cf:after { clear: both; }
Your clearfix method is not the source of any white space. In fact, this does not affect your layout and can be safely removed.
You use the clear property only when working with float.
From the specification:
9.5.2 Flow control next to floats: clear Property
This property indicates which sides of the element block (s) cannot be adjacent to an earlier floating field.
There are no floating elements in your layout, but if they were, the float and clear properties are nevertheless ignored in the context of flex formatting.
3. Flex containers: displaying flex and inline-flex values
float and clear do not create or exit the flex element and do not exit it.