0

How can I vertically center my DIV on the HTML page? I thought by setting the margin to 0 auto would do the trick, however that doesn't appear to be the case as it only horizontally centers and doesn't also vertically center my #main div on the page.

Here is a quick fiddle: https://jsfiddle.net/stevdbbz/

Here is the HTML and CSS in question:

<!DOCTYPE html>

<html>

<head>

<style type="text/css">
* {
    font-family: Segoe UI;
    font-size: 9pt;
    box-sizing: border-box;
}
body {
    background: rgb(70,70,70);
    padding: 0;
    margin: 0;
}
#main {
    border: 1px solid rgb(180,180,180);
    width: 800px;
    height: 650px;
    margin: 0 auto;
}
#boxA {
    padding-top: 7px;
    padding-bottom: 7px;
    padding-left: 3px;
    border-bottom: 1px solid rgb(180,180,180);
    background: #FFF;
}
#boxB {
    height: 573px;
    width: 200px;
    border: 0;
    float: left;
    background: #FFF;
}
#boxC {
    background: rgb(240,240,240);
    height: 573px;
    width: 598px;
    border-left: 1px solid rgb(180,180,180);
    border-top: 0;
    border-bottom: 0;
    border-right: 0;
    display: inline-block;
}
#boxD {
    background: rgb(240,240,240);
    border-top: 1px solid rgb(180,180,180);
    height: 44px;
    text-align: center;
    display: table;
    width: 100%;
}
#menu {
    list-style-type: none;
    padding: 0;
    margin: 0;
}
#menu li {
    padding: 4px;
        border: 1px solid #FFF;
}
#menu li:hover {
    cursor: pointer;
}
.selected {
    background: rgb(51,153,255);
    color: #FFF;
    border: 1px solid #FFF;
    font-weight: bold;
}
.hidden{ display:none; }


.item {
    width: 100%;
    height: 100%;
}


input[type="button"] 
{
  cursor:pointer; 
  border: 1px solid #707070;
  background-color: #F0F0F0;
  border-radius: 4px;
  box-shadow: inset 0 1px 2px #fff, inset 0 -0.7em #DDD;
  background-image: -ms-linear-gradient(top, #F1F1F1 0%, #E3E3E3 50%, #D0D0D0 100%);
  padding: 3px 6px;
  width: 75px;
}

input[type="button"]:hover 
{
  cursor:pointer;
  background-color: #EAF6FD;
  border: 1px solid #3C7FB1;
  box-shadow: inset 0 1px 2px #fff, inset 0 -0.7em #BEE6FD, 0 0 3px #A7D9F5;
}

input[type="button"][disabled], input[type="button"][disabled]:hover

{
  border: 1px solid #ADB2B5;
  text-shadow: 1px 1px #fff;
  cursor:default;
  background-color: #F4F4F4;
  box-shadow: inset 0 1px 2px #fff;
}












</style>

</head>

<body>

        <div id="main">

            <div id="boxA"><b>Application Title</b></div>

            <div id="boxB">
                <ul id="menu">
                    <li data-show="#1">Menu Item 1</li>
                    <li data-show="#2">Menu Item 2</li>
                    <li data-show="#3">Menu Item 3</li>
                </ul>
            </div>

            <div id="boxC">
                <div id="1" class="hidden item">
                    <b>Content Tab 1</b>
                </div>
                <div id="2" class="hidden item">
                    Content Tab 2
                </div>
                <div id="3" class="hidden item">
                    Content Tab 3
                </div>
            </div>

            <div id="boxD">

                <div style="display: table-cell; vertical-align: middle;">
                    <input type="button" value="Search" class="btn" disabled>
                    <input type="button" value="Save" class="btn" disabled>
                    <input type="button" value="Add" class="btn" disabled>
                    <input type="button" value="Clear All" class="btn">
                    <input type="button" value="Delete" class="btn" disabled>
                    <input type="button" value="Export" class="btn" disabled>
                    <input type="button" value="Recall" class="btn" disabled>
                </div>

            </div><!-- End of Main -->

</body>

</html>
j08691
  • 204,283
  • 31
  • 260
  • 272
BobbyJones
  • 1,331
  • 1
  • 26
  • 44
  • 6
    Possible duplicate of [How to vertically center a div for all browsers?](http://stackoverflow.com/questions/396145/how-to-vertically-center-a-div-for-all-browsers) – BiscuitBaker Nov 16 '15 at 14:28
  • [http://howtocenterincss.com/](http://howtocenterincss.com/) – hungerstar Nov 16 '15 at 14:31

3 Answers3

0

You can do that by display:table;

Jsfiddle

Alex
  • 8,461
  • 6
  • 37
  • 49
0

You can vertically center an element relative to the viewport using 3 simple rules:

#main {
    position: relative;
    top: 50vh; // positions the top edge of the element 50% down the viewport
    margin: -325px auto 0; // moves the top edge down the element by 325px (650px height / 2)

    border: 1px solid rgb(180,180,180);
    width: 800px;
    height: 650px;
}
Rounin
  • 27,134
  • 9
  • 83
  • 108
  • 1
    If your viewport is smaller than your content, you will not be able to see the top of the `main` div – Pete Nov 16 '15 at 15:47
0

You can use flexbox. Here is an example:

<div class="middle-align">
        <p>This is a vertically centered paragraph</p>
    </div>
</div>

and for the css

.middle-align {
    height:400px;
    background-color:gray;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    -ms-flex-align: center;
    -webkit-align-items: center;
    -webkit-box-align: center;
    align-items: center;
}

https://jsfiddle.net/p6yoka9c/

Nikoto
  • 5
  • 1