The answer is simple. Because line-height
is strictly referring to inline elements
, you'll want to treat the content box as inline text.
Here we have two divs, a wrapper and content:
<div class="wrapper">
<div class="content"> Some stuff</div>
</div>
So, first thing's first, we'll need to set a height for the wrapper
element, let us give it a height of 600px
. In order for our vertical-align
to work properly, we'll need to give it a line-height: 600px
as well, note it's the same height as the div
. (you can use javascript to do this part if there are dynamically added things).
We should give it some properties to make it more visible so we'll add a border to it. Oh, and we want the content to be centered as well, so we'll give it a text-alignment
of center
.wrapper{
height: 600px;
border: solid 1px black;
line-height: 600px;
text-align: center;
}
Next, we will deal with the actual content itself. First thing is first, we need to tell the browser that it is a display: inline-block;
And here is what you were asking for: vertical-align: middle
. After we'll give it a height: 200px
and give it some distinguishing qualities as well, so a border: solid 1px black;
should do the trick. And last but not least, we'll need to reset a couple of things that it will inherit from the parent (text-align: center; line-height: normal
).
/* enter code here */
.content{
display: inline-block;
text-align: left;
line-height: normal;
height: 200px;
border: solid 1px black;
vertical-align: middle;
width: 200px;
}
You can see the working product here