-2

I have read at least five articles on stackoverflow pertaining to this problem; as well as articles like this and this. It must be possible!

I have a div within my body that I would like vertically centered on my page. Like this:

<body>
    <div>I would like this to appear in the middle of the viewport.</div>
</body>

The second article above demonstrates its solution but their examples are divs inside other divs, which doesn't work for a div that you want centered within a body. I have tried having a div within a div and setting heights to 100% but that only annoyed my browser.

Please help! I don't want to have to resort to JQuery!

serlingpa
  • 12,024
  • 24
  • 80
  • 130

6 Answers6

2

Would be one of the possible solutions using absolute positioning:

html, body {
  height: 100%;
}
.center {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
<body>
  <div class="center">I would like this to appear in the middle of the viewport.</div>
</body>

Or this using display: table

html,
body {
  height: 100%;
}
.container {
  height: 100%;
  width: 100%;
  display: table;
}
.wrapper {
  display: table-cell;
  height: 100%;
  vertical-align: middle;
}
}
<body>
  <div class="container">
    <div class="wrapper">
      I would like this to appear in the middle of the viewport.
    </div>
  </div>
</body>
Mathijs Rutgers
  • 785
  • 4
  • 20
0

Try setting position to relative , setting text-align to center , top using css vh units to 45 or 50 , depending of height or line-height of div element

div {
  position:relative;
  top:45vh;
  text-align:center;
}
<body>
    <div>I would like this to appear in the middle of the viewport.</div>
</body>
guest271314
  • 1
  • 15
  • 104
  • 177
0

If you give the div a fixed height, then you can center it vertically using absolute positioning.

div {
    height:100px;
    position:absolute;
    top:50%;
    margin-top:-50px;
}

margin-top should be half of whatever your height is.

You can also vertically center a div of unspecified height if you wrap it in another div like so:

<div id="outer">
    <div id="inner">Some text here...</div>
</div>

#outer {
    height:100%;
    display:table;
}
#inner {
    display:table-cell;
    vertical-align:middle;
}

However, display:table and display:table-cell have less browser support.

Comptonburger
  • 583
  • 3
  • 10
0

Just use flexbox:

body {
  display:flex;
  height:100vh;
  align-items:center;
  justify-content:center;
}
div {
  width:125px;
  border:1px solid black;
}
<body>
    <div>I would like this to appear in the middle of the viewport.</div>
</body>

If you intend to support IE10 you'll need some prefixing, for IE9 and below you'll need other solutions. Practically speaking today - just ignore IE10 and older unless a client pays you not to. People actually upgrade these days.

Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
0

there is 2 easy ways nowdays: the flex display option ( 3 lines of code in between html & body):

html {
  min-height:100%;
  display:flex;
  }
body {
  margin:auto;
  }
/* show me where it stands */
html {
  background:linear-gradient(to left, rgba(0,0,0,0.25) 50%, transparent 50%),linear-gradient(to top, rgba(0,0,0,0.25) 50%, transparent 50%);
  }
div {
  border:solid;
  }
<body>
    <div>I would like this to appear in the middle of the viewport.</div>
</body>

or the table display (IE8 included here):

html {
  height:100%;
  width:100%;
  table-layout:fixed; /*if  to avoy spreading over the 100% width */
  display:table;
  }
body {display:table-cell;
  vertical-align:middle;
  text-align:center;
  }
div {
  display:inline-block;
  }
/* show me where it stands */
html {
  background:linear-gradient(to left, rgba(0,0,0,0.25) 50%, transparent 50%),linear-gradient(to top, rgba(0,0,0,0.25) 50%, transparent 50%);
  }
div {
  border:solid;
  }
<body>
    <div>I would like this to appear in the middle of the viewport.</div>
</body>
G-Cyrillus
  • 101,410
  • 14
  • 105
  • 129
0

@serlingpa If ALL you have in your body is this one div that you want to centre vertically, then I have solution:

div {
    position: fixed;
    top: 50%;
    transform: translateY(-50%);
}

This can be seen at this jsfiddle: http://jsfiddle.net/Rwthwyn/1bo7rx2q/

To make transform work on older versions of some browsers, you'll need to add in prefixes (-moz- , -ms- , -o- , -webkit-) eg: -webkit-transform: translateY(-50%) etc.

SYPOMark
  • 49
  • 10