As @Diegosantos just talked of putting a height already solves, but that doesn’t explain why with the Overflow
works and without fail.
The explanation is that the overflow
changes the context of the element. Here you can read about it: https://makandracards.com/makandra/9245-use-overflow-hidden-to-avoid-floating-elements-from-wrapping-a-container-s-text
Including the same technique as overflow
is used to correct problems of the Father’s height when the Sons have some float
setado and the father has no height.
Here is the Mozilla documentation that addresses the subject: https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Block_formatting_context
A block formatting context is created by at least one of the
following:
- floats (Elements Where float is not None)
- block Elements Where
overflow
has a value other than > Visible
In your case your <li>
has float:left
, if you just take it off or put it on display:inline-block
the color of BG will appear as the <ul>
will gain the height of your <a>
who is with display:block
.
Now if you want to continue using the float:left
the effect that the overflow:hidden
makes it that the <ul>
assume the height of the child you have display:block
in case your <a>
. So much so that with the overflow:hidden
in <ul>
any value of height, line-height, or padding for example that you put in <a>
will reflect on the time of the father <ul>
See the example in the case of float
:
div {
background-color: #f00;
width: 200px;
}
div > div {
float: left;
background-color: aqua;
width: 100px;
height: 100px;
}
.over {
overflow: hidden;
}
<div class="over">
<div>com overflow no pai</div>
</div>
<br>
<div>
<div>sem overflow no pai</div>
</div>
If you can edit the title, please (I don’t know how to clarify the doubt in the title).
– Wellington Albertoni
I think the title is perfect!
– DiegoSantos