Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do display: block and display: flex render the same element with a different height?

Tags:

html

css

flexbox

So I have a div with a span inside. And I'm setting display:block or display:flex on the div, and a small font-size on the span. Surprisingly, this is giving different heights on the div. See the example.

If I set the smaller font-size on the body or div then the height of both is equal. But if I set the smaller font-size on the span like in the example, then the divs get different heights. How come? And can I do anything about it?

span {
  font-size: 0.8rem;
  border: 1px red solid;
}

div {
  border: 1px blue solid;
}
<div style="display: block;">
  <span>test text 1</span>
</div>

<div style="display: flex;">
  <span>test text 2</span>
</div>
like image 672
Sámal Rasmussen Avatar asked Nov 22 '17 16:11

Sámal Rasmussen


People also ask

What is the difference between display flex and display block?

Block: Displays an element as a block element. It starts on a new line and takes up take up as much horizontal space as they can. Block-level elements do not appear in the same line, but breaks the existing line and appear in the next line. Flex: Flex displays an element as a flexible structure.

How do I make one DIV the same height as another?

Basically what you do is make both divs/columns very tall by adding a padding-bottom: 100% and then "trick the browser" into thinking they aren't that tall using margin-bottom: -100% . It is better explained by Ed Eliot on his blog, which also includes many examples.

How do you set the height of a flex item?

Instead of max-height: 250px you should use flex: 0 0 250px and with flex-direction: column it will set height of element to 250px.

What is the difference between display block and display inline?

Also, with display: inline-block , the top and bottom margins/paddings are respected, but with display: inline they are not. Compared to display: block , the major difference is that display: inline-block does not add a line-break after the element, so the element can sit next to other elements.


1 Answers

In a block formatting context, the line-height property makes a difference.

This is because line-height establishes the minimum height for inline-level elements and line boxes inside block-level containers.

In a block formatting context a span is an inline-level element and line-height applies.

In the code sample, any font size on the span below 1rem will change the font-size, but not the line-height, which remains fixed. That's what you're seeing with font-size: .8rem. The height of the div doesn't change. And it won't change unless the font size exceeds 1rem.

In a flex formatting context, the span is a flex item. A flex item is always a block-level element (regardless of the element type). Flex items are "blockified", according to the flexbox spec. Because there are no inline-level elements, line-height doesn't apply.

Also, an initial setting of a flex container is align-items: stretch. This means that the span sets the height of the container.

In summary, with display: block the line-height keeps the div height fixed. With display: flex, there is no line-height interference and the div tracks the height of the span freely.

One solution: Add display: block to the span, which eliminates the line-height issue.

like image 143
Michael Benjamin Avatar answered Oct 20 '22 04:10

Michael Benjamin