Is it possible to center a div vertically in a % height div?

Solution 1

This has been asked enough times here as well as all over the Internet.

A quick search will bring you tons of results. Anyhow, my preferred way of doing this is to use display: table-cell; and vertical-align: middle;. See this page for an example. (Beware that this doesn't work on Internet Explorer 6.)

Solution 2

If your inner div has an absolute height (lets say 100 pixels), you could do this:

.outerdiv{
  position: relative; // Or absolute, or fixed
  height: 80%;
}

.innerdiv{
  position: absolute;
  width: 100px;
  height: 100px;
  top: 50%;  // It's at 50%, but not really centered
  margin-top: -50px; // So just move it up by the half of its height :D
}

I don't like this solution very much, and I'm sure there are a lot of other possibilities (maybe using tables or display: table-cell;) - but this is the first that comes into my mind...

Solution 3

.outerdiv {
  display: table-cell;
  vertical-align: middle;
}

Warning - it will not work in all browsers!

Solution 4

There isn't any need for px units.

Change top, bottom, right, left or use percentages:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Insert title here</title>
    </head>

    <body>
        <div style="position: absolute;
                    top: 0;
                    bottom: 0;
                    right: 0;
                    left: 0;
                    text-align: center;
                    white-space: nowrap;
                    overflow: hidden;">
            <div style="position: relative;
                        display: inline-block;
                        height: 100%;
                        vertical-align: middle;"></div>
            <div style="background-color: #FEEF88;
                        position: relative;
                        display: inline-block;
                        vertical-align: middle;">Hola todo el mundo :D</div>
        </div>
    </body>
</html>

Solution 5

I prefer using the following technique, which involves two containers:

The outer container:

  • should have display: table;

The inner container:

  • should have display: table-cell;
  • should have vertical-align: middle;

The content box:

  • should have display: inline-block;

You can add any content you want to the content box without caring about its width or height!

Also, you can easily add horizontal centering by adding text-align: center; to your inner container.

Demo:

body {
    margin : 0;
}

.outer-container {
    position : absolute;
    display: table;
    width: 100%;
    height: 100%;
    background: #ccc;
}

.inner-container {
    display: table-cell;
    vertical-align: middle;
}

.centered-content {
    display: inline-block;
    background: #fff;
    padding : 20px;
    border : 1px solid #000;
}
<div class="outer-container">
   <div class="inner-container">
     <div class="centered-content">
        Malcolm in the Middle
     </div>
   </div>
</div>

See also this Fiddle!