Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method for full-screen vertically-centered HTML page?

Tags:

html

css

I'm looking for a valid cross-browser solution for an HTML page which:

  1. Consumes 100% of the screen height, with no overflow (i.e. no scrolling)
  2. has a vertically (and horizontally) centered <div> which will hold the main content

I know vertical centering is possible when the wrapping container has a static height. Is adjusting this height to browser window height something feasable? (Preferably, no JS should be used.)

like image 872
Yuval Adam Avatar asked Jan 26 '11 08:01

Yuval Adam


People also ask

How do you center a vertical page in HTML?

First position the div's top left corner at the center of the page (using position: fixed; top: 50%; left: 50% ). Then, translate moves it up by 50% of the div's height to center it vertically on the page. Finally, translate also moves the div to the right by 50% of it's width to center it horizontally.

How do you center align an image vertically in HTML?

Step 1: Wrap the image in a div element. Step 2: Set the display property to "flex," which tells the browser that the div is the parent container and the image is a flex item. Step 3: Set the justify-content property to "center." Step 4: Set the width of the image to a fixed length value.

How do you center align vertically?

For example, if you're trying to align something horizontally OR vertically, it's not that difficult. You can just set text-align to center for an inline element, and margin: 0 auto would do it for a block-level element.


1 Answers

Depends on what you mean with "cross browser". Following works fine with all current, standards compatible ones (thus not IE6):

HTML:

<div id="a">
    <div id="b">
        <div id="content"></div>
    </div>
</div>

CSS:

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

#a {
    display: table;
}

#b {
    display: table-cell;
    margin: 0;
    padding: 0;

    text-align: center;
    vertical-align: middle;
}

#content {
    border: 5px solid red;
    width: 100px;
    height: 100px;
    margin: auto;
}

Live example:

http://jsfiddle.net/mGPmr/1/

like image 140
RoToRa Avatar answered Oct 18 '22 05:10

RoToRa