Issue when centering vertically with flexbox when heights are unknown
Asked Answered
T

3

69

My layout has a container flex-container and a child.

HTML:

<div class="flex-container">
  <div>text</div>
</div>

The container and the child have an unknown height. And the goal is:

  • If the child has a lower height than the container, it appears centered horizontally and vertically.
  • If the child has a greater height than the container, it adjusts to the top and the bottom and we can do scroll.

Scheme: enter image description here

The fastest way for centering a element with flexbox is the follow:

.flex-container
{
  display: flex;
  align-items: center; /* vertical */
  justify-content: center; /* horizontal */

  width: 100%;
  height: 300px; /* for example purposes */
  overflow-y: scroll;
  background: #2a4;
}

.flex-container > div
{
  background: #E77E23;
  width: 400px;
}
<div class="flex-container">
  <div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure fugit voluptas eius nemo similique aperiam quis ut! Ipsa aspernatur rem nesciunt est sed hic culpa nisi delectus error explicabo reprehenderit. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure fugit voluptas eius nemo similique aperiam quis ut! Ipsa aspernatur rem nesciunt est sed hic culpa nisi delectus error explicabo reprehenderit. </div>
</div>

Codepen: http://www.codepen.io/ces/pen/slicw

But, if the container's child have a greater height, the child is not shown correctly. The child appears cutted (only the top part).

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

.flex-container
{
  display: flex;
  align-items: center; // vertical
  justify-content: center; // horizontal
  
  width: 100%;
  height: 100px;
  overflow-y: scroll;
  background: #2a4;
}

.flex-container > div
{
  background: #E77E23;
  width: 400px;
}
<div class="flex-container">
  <div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure fugit voluptas eius nemo similique aperiam quis ut! Ipsa aspernatur rem nesciunt est sed hic culpa nisi delectus error explicabo reprehenderit. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure fugit voluptas eius nemo similique aperiam quis ut! Ipsa aspernatur rem nesciunt est sed hic culpa nisi delectus error explicabo reprehenderit. </div>
</div>

Codepen: http://www.codepen.io/ces/pen/sGtfK

Scheme:

enter image description here

Is there a way for resolve this issue?

Threatt answered 2/7, 2014 at 18:7 Comment(0)
T
140

I found the solution:

.flex-container {
    display: flex; /* only */
    overflow-y: scroll;
}

.flex-container > div {
    margin: auto; /* horizontal and vertical align */
}

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

.flex-container {
    display: flex;
    width: 100%;
    height: 100px; /* change height to 300px */
    overflow-y: scroll;
    background: #2a4;
}

.flex-container > div {
    padding: 1em 1.5em;
    margin: auto;
    background: #E77E23;
    width: 400px;
}
<div class="flex-container">
  <div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure fugit voluptas eius nemo similique aperiam quis ut! Ipsa aspernatur rem nesciunt est sed hic culpa nisi delectus error explicabo reprehenderit. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure fugit voluptas eius nemo similique aperiam quis ut! Ipsa aspernatur rem nesciunt est sed hic culpa nisi delectus error explicabo reprehenderit. </div>
</div>

Codepen: http://codepen.io/ces/pen/Idklh

Threatt answered 3/7, 2014 at 2:11 Comment(10)
I wish I could upvote this more than once - it's not only useful it's also very well communicated, thanks.Attainable
Here .flex-container has the initial value align-items: flex-start (i.e. top).Penuchle
Been trying to find answer to this for a longgg time now! thank you.Bowel
Nice solution but is there a why?Eros
@Eros - here are some details on the "why?" ... https://mcmap.net/q/36652/-can-39-t-scroll-to-top-of-flex-item-that-is-overflowing-containerLighterman
Nice! But are there any IE11 fixes that you can put in?Weise
Thank you very much! you saved me HOURS of research and frustration.Overspend
It doesn't work horizontally. But if you use a min-width instead of width on the child. it works like a charm.Retral
Unfortunately setting margin to auto still doesn't fix the problem if flex-wrap: wrap; and the flex item is flex: 1 100%;. In this situation, the margin of the flex item remains 0! How can we fix the problem here?Winniewinnifred
3 years later and you save my day. Thanks!Dawes
C
0

Don't use justify-content: center or align-items: center. To achieve center alignment, just put empty elements on either side and set the empty elements to flex: 1, so that they push the center element to the center.

Careaga answered 31/12, 2021 at 14:59 Comment(0)
C
-1

Add align-self:flex-start; to .flex-container > div can resolve this problem too.

See: http://www.w3.org/TR/css-flexbox-1/#auto-margins

Charlettecharley answered 28/10, 2015 at 15:43 Comment(3)
This answers the question instead of providing a workaround like the accepted answer does. +1Winser
Your solution is simply to ignore the centering requirement. Of course, if centering wasn't necessary, there wouldn't be a problem..Lighterman
This is not the correct answer because it ignores the vertical centering. So it is not a solution.Thereby

© 2022 - 2024 — McMap. All rights reserved.