Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vertical-align in div with height 100% [duplicate]

Tags:

html

css

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

like image 511
rzr Avatar asked Aug 21 '12 16:08

rzr


People also ask

How do I make my div 100%?

Syntax: To set a div element height to 100% of the browser window, it can simply use the following property of CSS: height:100vh; Example: HTML.

What does height 100% do CSS?

height: 100% gives the element 100% height of its parent container. height: auto means the element height will depend upon the height of its children.

How align span vertically in div?

The CSS just sizes the div, vertically center aligns the span by setting the div's line-height equal to its height, and makes the span an inline-block with vertical-align: middle. Then it sets the line-height back to normal for the span, so its contents will flow naturally inside the block.


1 Answers

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.

like image 103
Lasse Christiansen Avatar answered Oct 13 '22 03:10

Lasse Christiansen