What causes the difference in width of a flex element and an inline-block element?

inline-flex vs inline-block
what is inline-flex
flexbox
flex container inline or block
css display
flex vs block
inline-flex w3schools
inline-flex mdn

See this fiddle:

https://jsfiddle.net/9hnj0b8v/3/

The only differnce in the two elements:

.label1
{
  display: inline-block;
}

.label2
{
  display: flex;
}

So the two labels are identical except one is set to inline-block and one to flex. Why do they have a different width? Why is one exceeding its container and the other does not?

You are styling span elements with display: inline-block. By default, block level elements are 100% of the width of their parent container. As you are forcing the input and span onto the same line with white-space: nowrap, this forces the span to break out of the boundary of the parent container.

Why do they have a different width? Why is one exceeding its container and the other does not?

Using display: flex allows child elements to shrink to fit the available space so the span element starts with the 100% width of its parent but then shrinks down to fill the available space instead.

What is the difference between inline-flex and inline-block in CSS , So the two labels are identical except one is set to inline-block and one to flex. Why do they have a different width? Why is one exceeding its container and the� The main difference between display: flex and display: inline-flex is that display: inline-flex will make the flex container an inline element while it’s content maintains its flexbox properties. In the below picture, the flex container comprises Computer, Science, Portal, and the yellow area.

The main difference I observed is that with an inline-block, it will fill your parent div with your inline-block elements regardless of how many elements are in your parent div, however with display: flex will divide your parent div width (or height) with the number of element you have.

Also, in your fiddle, you've already set inline-block for the span, just try remove it and you'll see the difference in a more visual way

What Happens When You Create A Flexbox Flex Container , The flex container itself is displayed in a new line, just like the block element. an inline element, but it allows you to set a width and height on the element, which is not possible in the display: inline. It makes the flex container display inline. But the only reason for that is because inline-block wasn't introduced until CSS2 and there for it is an "inline element behaving like an inline-block element" because it is replaced by its content. i.e you are not setting height/width to the element, you are setting it on its content - Wierd, yes. I know.

It's because of the white-space:nowrap.

Without you will have this which is an expected result:

div {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 200px;
  background: #f0f0f0;
}

span {
  display: inline-block;
  white-space: normal;
  vertical-align: top;
}

label {
  margin-top: 10px;
  /*white-space: nowrap;*/
}

.label1 {
  display: inline-block;
}

.label2 {
  display: flex;
}
<div>

  <label class=label1>
 <input type=checkbox>
 <span>
  Here is a long description for the option that is set via the checkbox
  on the left. The label is set to inline-block. For some reason,
  it overflows the containing div. But why?
</span>
</label>


</div>

Display – block, inline and flex – Must Be Built, You are styling span elements with display: inline-block. By default, block level elements are 100% of the width of their parent container. As you are forcing the� Recently I had to use an h1 and an h2 one on top of the other, but the backgrounds for both had to extend just the width of the text, not the full width of the parent element. I decided to display them as inline and, to position them one on top of the other, I added an hr (a block element) with visibility set to hidden between them.

A Complete Guide to Flexbox, The flex container is inline, so another inline element will display next to it. Flexbox is a little trickier because different things happen depending When I described row and column above, I mentioned the block and inline dimensions. To cause the items to lay themselves out in the block dimension, we� The display: inline-block Value. Compared to display: inline, the major difference is that display: inline-block allows to set a width and height on the element.. Also, with display: inline-block, the top and bottom margins/paddings are respected, but with display: inline they are not.

How to Make Inline-Block Elements Add Up to 100% Width – UX , Every HTML element falls into a display family such as block, inline or inline: makes the element act as inline even if it is by default a block element. element inline you will not be able to access box model properties such as width, height,� One major concept to grasp in HTML is the difference between block elements and inline elements. Block elements are those that take up the full width available on a web page, effectively blocking out any other elements from sitting next to it on the left or right.

What is the difference between inline flex and inline-block in CSS , This defines a flex container; inline or block depending on the given value. flex items within a flex container demonstrating the different spacing options This defines the default size of an element before the remaining space is distributed. The auto keyword means “look at my width or height property” (which was� 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.

Comments
  • Somebody tried to remove the link to jsfiddle. Please do not do that. This is one is much clearer to see and work on in a fiddle.
  • It is much better to have it inline in a snippet. If the fiddle link dies the question loses information.
  • Sometimes it helps to use transitions to understand what's going on. I've taken the liberty of forking your fiddle and adding a transition that changes the width of the encapsulating div. Check it out here: jsfiddle.net/x6prc01w - hover the grey div to watch it in play. Change the styles of the other elements and add a float to the top span. I see no bugs or errors in your fiddle. Everything renders as expected.
  • should I also say that you edited your answer to include information from mine? ;)
  • "By default, block level elements are 100% of the width of their parent container." - that would explain it. But it is not always the case. Check this fiddle for a counter example: jsfiddle.net/7osrmkph
  • I'm assuming OP intends the label text to be beside the checkbox like in the display: flex example. Your 'solution' breaks that.
  • @JamesCoyle solution? I am explaining not providing a solution ;)
  • You haven't explained why the overflow is actually occuring. You provided a solution to stop the overflow and this has nothing to do with flex-wrap as flexbox isn't wrapping anything. And now you are editing your answer to provide information from my answer.
  • @JamesCoyle what is not clear in the first paragraphe that is explaining the overflow? I have also added a blod text to hightlight why it's happening
  • @JamesCoyle and what information I copied from your answer? If it's about the last sentence, I have already provided an old answer (stackoverflow.com/a/49492522/8620333) explaining the shrink effect so I simply know this information like you do.