javascriptjqueryhtmlcssresponsive-design

Vertically center a content area until it reaches a certain height?


How do I allow a dynamic (changing in height) content area to be vertically centered on the users screen (regardless of screen size), UNTIL it reaches the point where there is only 100px left of free space at the top of the page?

Here's a diagram: enter image description here

I'm not entirely sure whether the solution to this problem will use only CSS or javascript - so all suggestions are welcome.


Solution

  • My solution takes into account that you want your content to be centered on the whole page and not just "in the space below". I used negative margins to achieve this.

    See the working example here:

    http://jsfiddle.net/WkQzw/5/

    HTML:

    <div id="container">
        <div id="table">
            <div id="cell">
                <div id="content">Some content</div>
            </div>
        </div>
    </div>
    

    CSS:

    #container {
        width: 100%;
        height: 100%;
        padding: 100px 0;
        margin-top: -100px;
    }
    #table {
        display: table;
        width: 100%;
        height: 100%;
        margin-top: 100px;
    }
    #cell {
        display: table-cell;
        vertical-align: middle;
    }
    #content {
        background-color: lightblue;
        width: 50%;
        margin: 0 auto;
    }
    

    Tested:

    Update:

    I used box-sizing: border-box; but Firefox required an additional -moz-box-sizing: border-box;. Now it works also in Firefox.