11

Here's my situation: I'm trying to make a page with two DIVsfilling whole page (height 100%, width 50% each). Also, the content in the DIVs is to be vertically aligned to middle.

Is there an easy way to achieve this without hacks or javascript?

I've tried body,html{height:100%;} .mydiv {display:table-cell;height:100%;vertical-align-middle}

but that doesn't work...and with that code, i have to specify width in pixels instead of percentage

MayThrow
  • 2,159
  • 4
  • 24
  • 38
  • 1
    So many questions here answering this topic, please search. – jackJoe Aug 21 '12 at 16:46
  • Not exactly. Some answers are about making the div 100% in height and some are about vertical aligning. making them co-exist is what i want to do – MayThrow Aug 21 '12 at 16:47
  • Height in CSS (with the exception of a finite object with fixed dimensions) is a nightmare! I have tried countless times to accomplish this, and just when I think I have it... The solutions breaks something else. If there is any way to avoid this scenario, I would suggest doing so. – CoderMarkus Aug 21 '12 at 17:17

3 Answers3

15

Live Demo

I just made a jsFiddle showing my suggestion to a solution. Here I take into account that you want two <div>s filling 50% of the width each, 100% height, and that you want the content to be vertically aligned in the middle. Here is the simplified working solution with source code.

HTML

<div id="outer">
    <div id="table-container">
        <div id="table-cell">
            This content is vertically centered.
        </div>
    </div>
</div>

CSS

#outer {
    position:absolute;
    top:0;
    left:0;
    width:50%;
    height:100%;
}

#table-container {
    height:100%;
    width:100%;
    display:table;
}

#table-cell {
    vertical-align:middle;
    height:100%;
    display:table-cell;
    border:1px solid #000;
}

For reference, I used this tutorial.

totymedli
  • 29,531
  • 22
  • 131
  • 165
Lasse Christiansen
  • 10,205
  • 7
  • 50
  • 79
0
position: absolute;
top: 0;
bottom: 0;

Will give you a box that fills to 100% height. Make sure your HTML and BODY tags are large enough:

html, body {
  height: 100%;
}
Henrik
  • 3,908
  • 27
  • 48
  • 1
    But that leaves the question half-answered. What about vertical-alignment issue? With your code, the content is not vertically aligned even with `display:table-cell;vertical-align:middle` – MayThrow Aug 21 '12 at 17:00
0

Do you want this type of design ? => Example Fiddle

ygssoni
  • 7,219
  • 2
  • 23
  • 24