3

I have three divs inside a flexbox container.

I would like the three divs to be displayed as follows:

+-----------------------------------------------+
|     +-----------------+-----------------+     |
|     |                 |                 |     |
|     |                 |                 |     |
|     +-----------------+-----------------+     |
|                                               |
|     +-----------------------------------+     |
|     |                                   |     |
|     |                                   |     |
|     +-----------------------------------+     |
|                                               |
+-----------------------------------------------+

so the container will have to adapt vertically to its content but only to its content, not more than that.

I am getting big white spaces between the divs and the container is getting the 100% of the height of its parent, though.

I tried without putting height property to the container and setting height: auto; but neither of them worked.

Here is the code I have:

*{
   box-sizing: border-box;
}

html,body{
   width: 100%;
   margin: 0;
}

#container{
 display: flex;
 position: absolute;
 top: 0;
 right: 0;
 bottom: 0;
 left: 0;
 margin: auto;
 justify-content: center;
 align-items: center;
 flex-wrap: wrap;
 max-width: 450px;
 border: 1px solid;
}

#div1{
 width: 50%;
 height: 155px;
 background-color: blue;
}

#div2{
    width: 50%;
    height: 155px;  
    background-color: red;
}

#div3{
    width: 70%;
    height: 155px;
    background-color: green;
}
<div id="container">
  <div id="div1"></div>
  <div id="div2"></div>
  <div id="div3"></div>
</div>

Note: Make it bigger to see the white spaces between the divs.

How can I make the container shrink-wrap the content and not be 100% height of its parent?

Thanks in advance!

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Francisco Romero
  • 12,787
  • 22
  • 92
  • 167

1 Answers1

2

When you create a flex container, an initial setting is align-content: stretch.

This causes multiple lines of flex items to distribute themselves across the full length of the container.

Override this setting with align-content: flex-start on #container.


To make the #container shrink-wrap its contents vertically, you need to remove this rule:

#container { bottom: 0; }

Because #container is absolutely positioned, and there is no parent element that is positioned, which would establish the containing block for #container, the default containing block becomes the initial containing block or, the viewport.

That's why the container is stretching from top to bottom, as illustrated by the black border you applied. Once your remove bottom: 0 the container will act more like height: auto.

Learn more about CSS positioning here: https://developer.mozilla.org/en-US/docs/Web/CSS/position

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • Thank you! It is very close, but not exactly what I am looking for. It still does not adapt the height of the container to its content. Just put the content together (one of the two approachs). Look it [here](https://jsfiddle.net/4dfLv48y/). – Francisco Romero May 24 '16 at 15:27
  • I had `bottom: 0` because I wanted to have the container both vertically and horizontally centered on the screen. I guess that I have to find another way now to center it. – Francisco Romero May 24 '16 at 18:11
  • To center the container on the screen, just use another flex container: https://jsfiddle.net/rf15p2vw/2/ – Michael Benjamin May 24 '16 at 18:41
  • Thank you! Sorry for reply too late. I thought about it but I also was doubting about if it was a good idea to put a `flex` on the `body` tag or not. Now you clarify my mind. Thank you again :) – Francisco Romero May 24 '16 at 21:12
  • You're welcome. I found no problems using `flex` on `body` element. Works fine for me. – Michael Benjamin May 24 '16 at 21:14
  • 1
    Sure. You are one of my best helpers :) – Francisco Romero May 24 '16 at 21:15