This is happening because progress
is inline
element, which is inheriting line-height
from parent block
to create space above and below of progress
,
Such anonymous inline boxes inherit inheritable properties from their
block parent box. Non-inherited properties have their initial value.
Short description to understand the difference between Inline, Inline-block and Block level elements.
Inline: An inline element has no line break before or after it, and it
tolerates HTML elements next to it.
Inline-block: An inline-block element is placed as an inline element (on
the same line as adjacent content), but it behaves as a block element.
Block: A block element has some
whitespace above and below it and does not tolerate any HTML elements
next to it.
In following demo you can see the difference between inline
and block
level element.
In the first div
(.inline
) the progress bars are inheriting all properties from parent block except background
and margin
(these two applies on element itself) and in the second div
(.block
) nothing is being inherited.
In other words parent block element treats it's child inline
or inline-block
level element same as it treats text
in it.
.inline,
.block {
line-height: 100px;
font-size: 12px;
letter-spacing: 20px;
white-space: nowrap;
background: #ddd;
margin: 10px 0;
}
.block progress {
display: block;
}
<div class="inline">
<progress value="30" max="100"></progress>
<progress value="30" max="100"></progress>
<progress value="30" max="100"></progress>
<progress value="30" max="100"></progress>
<progress value="30" max="100"></progress>
</div>
<div class="block">
<progress value="30" max="100"></progress>
<progress value="30" max="100"></progress>
<progress value="30" max="100"></progress>
<progress value="30" max="100"></progress>
<progress value="30" max="100"></progress>
</div>
Now there are two better ways to fix this.
First set font-size: 0;
to parent block element of inline
element this will remove line-height
, white-space
and then reset font-size
of child inline element with font-size: initial;
and font-size: normal;
for IE.
div {
border: 1px solid blue;
font-size: 0;
}
div progress {
font-size: initial;
}
Bigger progress, div expands as expected
<div>
<progress max="100" value="33" style="height:30px"></progress>
</div>
<br/> Normal progress
<div>
<progress max="100" value="33"></progress>
</div>
<br/> Smaller progress, div doesn't shrink. Why?
<div>
<progress max="100" value="33" style="height:7px"></progress>
</div>
The second way is to convert inline
element to block
element.
div {
border: 1px solid blue;
}
div progress {
display:block;
}
<div>
<progress max="100" value="33" style="height:30px"></progress>
</div>
<br/> Normal progress
<div>
<progress max="100" value="33"></progress>
</div>
<br/> Smaller progress, div doesn't shrink. Why?
<div>
<progress max="100" value="33" style="height:7px"></progress>
</div>
Sources:
- Inline-boxes
- Css-display-inline-vs-inline-block