11

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hailwood
  • 89,623
  • 107
  • 270
  • 423
  • Related (but not an exact dupe): [Vertically Aligning Divs](http://stackoverflow.com/questions/2743989/vertically-aligning-divs). – Andy E Jul 29 '10 at 14:45
  • Please check those links http://jsfiddle.net/ERuX4/1/ http://stackoverflow.com/a/10010055/1312610 – ShibinRagh Apr 24 '12 at 10:45
  • Here is a example working for IE, Firefox and Chrome. http://jsfiddle.net/ETKhu/ Probably you need for future work :) – Ricardo Rodrigues Jun 27 '12 at 15:37
  • Working example. Easier way to do the same: [jsfiddle.net/NT8Mn](http://jsfiddle.net/NT8Mn) – pratik Mar 27 '14 at 12:20

5 Answers5

15

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.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
casablanca
  • 69,683
  • 7
  • 133
  • 150
  • 2
    yeah, i did search the internet, However there were so many results that i had no idea which ones were valid, and which ones were old hat. Cheers. – Hailwood Jul 29 '10 at 14:47
  • Also, another way to do this without complicated CSS. Try this: [jsfiddle.net/NT8Mn](http://jsfiddle.net/NT8Mn) – pratik Mar 27 '14 at 12:14
  • I searched the internet and this was the top result. – blockloop Sep 02 '14 at 21:27
6

If your inner div has an absolute height (let’s 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...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
oezi
  • 51,017
  • 10
  • 98
  • 115
3
.outerdiv {
  display: table-cell;
  vertical-align: middle;
}

Warning - it will not work in all browsers!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sorcy
  • 2,587
  • 5
  • 26
  • 34
0

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>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

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!

Community
  • 1
  • 1
John Slegers
  • 45,213
  • 22
  • 199
  • 169