This issue is weird, acts differently on normal containers and flexbox.
Changing the font-size
of the FIRST children:
- for NORMAL CONTAINER, why is it changing the container’s height?
- for FLEXBOX, why is it making the flexbox move down or up for some distance?
How do I resolve this? I want the FLEXBOX not to move.
To be said, it’s only happening when changing font-size
of first children. Changing the second doesn’t matter… why?
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: #121212;
color: #fff;
font-size: 20px;
}
.container {
line-height: 48px;
font-size: 36px;
border: 1px solid #f90;
}
.first {
color: #ffe47e;
animation: change1 3s linear infinite;
}
@keyframes change1 {
0%,
100% {
font-size: 0.1em;
}
50% {
font-size: 1em;
}
}
@keyframes change2 {
0%,
100% {
font-size: 0.1em;
}
50% {
font-size: 2em;
}
}
.c2 {
display: inline-flex;
column-gap: 5px;
}
.c2 .first {
font-size: 0.5em;
animation: change2 3s linear infinite;
}
.c3 .first {
font-size: 0.5em;
animation: none;
}
<div style="margin-bottom: 50px;">
<div class="container c2 c3">
<span class="first">first</span>
Second
</div>
<div class="container c2 c3">
Second
<span class="first">This doesn't matter</span>
</div>
(flexbox) How to make the first flex container align with the second one?
</div>
<div style="margin-bottom: 50px;">
(flexbox) Animation illustration
<div class="container c2">
<span class="first">first</span>
Second
</div>
<div class="container c2">
Second
<span class="first">This doesn't matter</span>
</div>
</div>
(normal container) Animation illustration - Why is height changing?
<div class="container">
<span class="first">first</span>
Second
</div>
4
Answers
because font also having its on size, we you increasing font size so size of container also increase, use rem in font size,
Please don’t put multiple questions into the same SO question.
Your first question about "normal" (i.e. block) containers is a duplicate. It’s thoroughly covered by <small> tag makes height of paragraph larger
For the flexbox container, this is covered by how the flex containers determine their baselines. The relevant specification says:
The default alignment for flex items is "stretch", which means that the flex-items are not participating in baseline alignment, and point 1 does not apply.
Point 2 applies and means that the first flex item in each flex container provides the baseline on which each of the flex containers will align will each other.
The standard way of dealing with this is to take the inline-flex or inline-block container elements off the baseline by giving them
vertical-align:top
.Let’s say you have the first text at
font-size:50px
and the second atfont-size:0px
and you want to switchfont-size
s. At this point the height of the content is50px
. In the middle of the animation, both texts havefont-size:25px
, so the content height is25px
too. At the end of the animation you have50px
and0px
again, so the content height is50px
.The height of the
div
is adjusted to the height of the content.every div has height of auto by default, so it will stretch to contain elements inside unless you prevent it. you can uses css below to stop this height change
** body also have height:auto; so its height will still change due to width change in elements causing some element get pushed down.