Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flexbox parent smaller than child elements on browser resize [duplicate]

Tags:

html

css

flexbox

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>
like image 823
Thomas Avatar asked Aug 13 '16 22:08

Thomas


People also ask

How do you make Flexbox children 100% Height of their parents using CSS?

Getting the child of a flex-item to fill height 100% Set position: absolute; on the child. You can then set width/height as required (100% in my sample).

Is the FLEX item only the direct child of the Flexbox container?

Only direct children of flex containers are flex items. In your example, "childOfChildClass" would only be a flex item if "childClass" was a flex container.


Video Answer


2 Answers

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>
like image 94
Michael Benjamin Avatar answered Oct 19 '22 13:10

Michael Benjamin


  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.

like image 37
andreas Avatar answered Oct 19 '22 11:10

andreas