Vertically Center Text in a 100% Height Div

vertical-align in div with height 100%

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.

Vertically center text in a 100% height div?

This answer is no longer the best answer ... see the flexbox answer below instead!


To get it perfectly centered (as mentioned in david's answer) you need to add a negative top margin. If you know (or force) there to only be a single line of text, you can use:

margin-top: -0.5em;

for example:

http://jsfiddle.net/45MHk/623/

//CSS:
html, body, div {
height: 100%;
}

#parent
{
position:relative;
border: 1px solid black;
}

#child
{
position: absolute;
top: 50%;
/* adjust top up half the height of a single line */
margin-top: -0.5em;
/* force content to always be a single line */
overflow: hidden;
white-space: nowrap;
width: 100%;
text-overflow: ellipsis;
}

//HTML:
<div id="parent">
<div id="child">Text that is suppose to be centered</div>
</div>​

The originally accepted answer will not vertically center on the middle of the text (it centers based on the top of the text). So, if you parent is not very tall, it will not look centered at all, for example:

http://jsfiddle.net/45MHk/

//CSS:
#parent
{
position:relative;
height: 3em;
border: 1px solid black;
}

#child
{
position: absolute;
top: 50%;
}​

//HTML:
<div id="parent">
<div id="child">Text that is suppose to be centered</div>
</div>​

flexbox vertical center with 100% height

Add display: flex for your items. Then only it can align items inside it as per your requirements. Please have a look at the updated code.

.wrapper {  display: flex;  width: 100vw;  height: 100vh;  text-align: center;
}
.item { display: flex; background-color: green; align-items: center; justify-content: center; height: 100%; width: 100%;
}
<div class='wrapper'>  <div class='item'>    sdafsdfs  </div></div>

How to vertically align text in the middle of a div that has a percentage height?

You can use display:inline-block , height:100% and vertical-align:middle to a single element or pseudo element aside the text (before or after): DEMO

#chooseStateAlabama:before {/* this can be an extra tag within HTML structure if pseudo used for other purpose */
content:'';
display:inline-block;
height:100%;
vertical-align:middle;
}

If you happen to have more content or more than 1 line, then use an element to wrap it as well and apply to it display and vertical-align. DEMO2 to see behavior

Vertically center text in a grid layout with grid and items 100% height

Put flex on the boxes, just like in an answer on the post you linked.

https://css-tricks.com/snippets/css/a-guide-to-flexbox

section {
height: 400px;
}

#boxes {
background-color: green;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
gap: 6px;
height: 100%;
min-height: 10em;
min-width: 12em;
}

#boxes>div {
background-color: #8ca0ff;
text-align: center;
border: 1px solid red;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
}
<section>
<div id="boxes">
<div id="00">I'm not</div>
<div id="01">vertically</div>
<div id="02">centered.</div>
<div id="10">I</div>
<div id="11">am not</div>
<div id="12">either!</div>
<div id="20">Was CSS designed</div>
<div id="21">by a psychopath?</div>
<div id="22">Seems like it!</div>
</div>
</section>

vertically center div when body height: 100% without absolute pos

You can use display:table and display:table-cell:

html, body {    width: 100%;    height: 100%;}
body{ margin: 0; display: table}
body>div { display: table-cell; text-align: center; /* horizontal */ vertical-align: middle; /* vertical */}
<div>    <img src="http://paw.princeton.edu/issues/2012/07/11/pages/6994/C-beer.jpg" /></div>

How to center text top/bottom when height 100% & display inline-block?

Try using Flexbox: This is the working example of vertically and horizontally centering with flexbox.

Working example

*,
*:before,
*:after {
box-sizing: border-box;
}
body {
margin: 0
}
.testing-here {
font-size: 0;
}
.panel-default {
box-sizing: border-box;
position: relative;
width: 50%;
padding-bottom: 40%;
overflow: hidden;
background-color: #446CB3;
display: inline-block;
}

.panel-body {
color: white;
width: 100%;
height: 100%;
text-align: center;
position: absolute;
font-size: 16px;
justify-content: center;
align-items: center;
display: flex;
}

How can I center text (horizontally and vertically) inside a div block?

If it is one line of text and/or image, then it is easy to do. Just use:

text-align: center;
vertical-align: middle;
line-height: 90px; /* The same as your div height */

That's it. If it can be multiple lines, then it is somewhat more complicated. But there are solutions on http://pmob.co.uk/. Look for "vertical align".

Since they tend to be hacks or adding complicated divs... I usually use a table with a single cell to do it... to make it as simple as possible.



Update for 2020:

Unless you need make it work on earlier browsers such as Internet Explorer 10, you can use flexbox. It is widely supported by all current major browsers. Basically, the container needs to be specified as a flex container, together with centering along its main and cross axis:

#container {
display: flex;
justify-content: center;
align-items: center;
}

To specify a fixed width for the child, which is called a "flex item":

#content {
flex: 0 0 120px;
}

Example: http://jsfiddle.net/2woqsef1/1/

To shrink-wrap the content, it is even simpler: just remove the flex: ... line from the flex item, and it is automatically shrink-wrapped.

Example: http://jsfiddle.net/2woqsef1/2/

The examples above have been tested on major browsers including MS Edge and Internet Explorer 11.

One technical note if you need to customize it: inside of the flex item, since this flex item is not a flex container itself, the old non-flexbox way of CSS works as expected. However, if you add an additional flex item to the current flex container, the two flex items will be horizontally placed. To make them vertically placed, add the flex-direction: column; to the flex container. This is how it works between a flex container and its immediate child elements.

There is an alternative method of doing the centering: by not specifying center for the distribution on the main and cross axis for the flex container, but instead specify margin: auto on the flex item to take up all extra space in all four directions, and the evenly distributed margins will make the flex item centered in all directions. This works except when there are multiple flex items. Also, this technique works on MS Edge but not on Internet Explorer 11.



Update for 2016 / 2017:

It can be more commonly done with transform, and it works well even in older browsers such as Internet Explorer 10 and Internet Explorer 11. It can support multiple lines of text:

position: relative;
top: 50%;
transform: translateY(-50%);

Example: https://jsfiddle.net/wb8u02kL/1/

To shrink-wrap the width:

The solution above used a fixed width for the content area. To use a shrink-wrapped width, use

position: relative;
float: left;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);

Example: https://jsfiddle.net/wb8u02kL/2/

If the support for Internet Explorer 10 is needed, then flexbox won't work and the method above and the line-height method would work. Otherwise, flexbox would do the job.



Related Topics



Leave a reply



Submit