-2

In this example, why does container need to have the pseudo-element after and before with content: '' and display: table to show the gray background? Shouldn't container automatically expand to fit both of its children and then fill out the negative space with the gray background without the pseudo-elements already?

0

1 Answer 1

-1

This is called a 'clearfix', when a container has all of his the childs floating, you have to use a clearfix on it. I put a black background in the container:

With clearfix: https://codepen.io/anon/pen/GvoEjx

Without clearfix: https://codepen.io/anon/pen/NvxgRe

https://css-tricks.com/snippets/css/clear-fix/

.container:before,
.container:after {
  content: "";
  display: table;
  clear: both;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Additionally, display: flow-root is now an option in some browsers, for which the explanation of how it differs from clearfix is quite technical.
clear:fix doesn't allow elements to be floating above it, but in this situation how does it affect whether the container element get a background or not?
Also for that example, why do you need it in the before? Don't you just need it for the after?
The element get a background in any case, but without clearfix, The container cant get The height of The content, in other words, we cant see The background @stackjlei
You might as well not bother answering the question in that case.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.