19

Why is the parent smaller than the children on narrow screens?

See snippet... then resize your browser (width-wise) to be smaller than the pink box. The scroll bars should appear. Scroll back to the right on the page and note the green background is smaller than the pink area and there is a white spot on the right.

enter image description here

So few questions:

  1. Why does it happen?

  2. How do I prevent the parent div's green background from getting smaller than the pink box/div when the browser is resized without setting an explicit width on the parent (or anywhere else) or using overflow:hidden?

  3. Is there a flexbox solution to this problem?

Thanks,

Thomas

.parent {
  height: 400px;
  background-color: green;
  display: flex;
}

.item {

  height: 100px;
  background-color: pink;
  padding: 10px;
}
<div class="parent">
  <div class="item">looooooooooooooooooooooooooooong</div>
  <div class="item">looooooooooooooooooooooooooooong</div>
</div>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Thomas
  • 223
  • 1
  • 2
  • 7

2 Answers2

16

Flex items, by default, cannot be smaller than the size of their content. Therefore, while the parent can shrink, the flex items cannot shrink past the length of the text. This causes the overflow and, as a result, the column of white space below.

The initial setting on flex items is min-width: auto. In order for each item to stay within the container, switch to min-width: 0.

.parent {
  height: 400px;
  background-color: green;
  display: flex;
}

.item {
  height: 100px;
  background-color: pink;
  padding: 10px;
  min-width: 0;   /* NEW */
}
<div class="parent">
  <div class="item">looooooooooooooooooooooooooooong</div>
  <div class="item">looooooooooooooooooooooooooooong</div>
</div>

Now the pink boxes are not overflowing the container anymore.

However, the text is now overflowing the pink boxes.

The question doesn't specify behavior for the text, but here's one possible solution:

.parent {
  height: 400px;
  background-color: green;
  display: flex;
}

.item {
  height: 100px;
  background-color: pink;
  padding: 10px;
  min-width: 0;            /* NEW */
  text-overflow: ellipsis; /* NEW */
  white-space: nowrap;     /* NEW */
  overflow: hidden;        /* NEW */      
}
<div class="parent">
  <div class="item">looooooooooooooooooooooooooooong</div>
  <div class="item">looooooooooooooooooooooooooooong</div>
</div>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
11
  1. It happens, because your .parent is a normal block element (flex is also a block-level container) and that is initialized with width:auto;, which is in your case the width of the viewport. So scrolling to the right will show white space because your div's width is smaller than the whole scrollable area.

  2. You do that with setting the .parent to an inline element, which will respond to its childrens width.

  3. Yes, just use display: inline-flex; on the .parent.

.parent {
  height: 400px;
  width: 100%;
  background-color: green;
  display: inline-flex;
}

.item {
  flex: 1;
  height: 100px;
  background-color: pink;
  padding: 10px;
}
<div class="parent">
  <div class="item">looooooooooooooooooooooooooooong</div>
  <div class="item">looooooooooooooooooooooooooooong</div>
</div>

See display on MDN.

andreas
  • 16,357
  • 12
  • 72
  • 76