This seems to be the best solution I’ve found to this problem, as long as your browser supports the ::before
pseudo element: CSS-Tricks: Centering in the Unknown.
It doesn’t require any extra markup and seems to work extremely well. I couldn’t use the display: table
method because table
elements don’t obey the max-height
property.
.block {_x000D_
height: 300px;_x000D_
text-align: center;_x000D_
background: #c0c0c0;_x000D_
border: #a0a0a0 solid 1px;_x000D_
margin: 20px;_x000D_
}_x000D_
_x000D_
.block::before {_x000D_
content: '';_x000D_
display: inline-block;_x000D_
height: 100%; _x000D_
vertical-align: middle;_x000D_
margin-right: -0.25em; /* Adjusts for spacing */_x000D_
_x000D_
/* For visualization _x000D_
background: #808080; width: 5px;_x000D_
*/_x000D_
}_x000D_
_x000D_
.centered {_x000D_
display: inline-block;_x000D_
vertical-align: middle;_x000D_
width: 300px;_x000D_
padding: 10px 15px;_x000D_
border: #a0a0a0 solid 1px;_x000D_
background: #f5f5f5;_x000D_
}
_x000D_
<div class="block">_x000D_
<div class="centered">_x000D_
<h1>Some text</h1>_x000D_
<p>But he stole up to us again, and suddenly clapping his hand on my_x000D_
shoulder, said—"Did ye see anything looking like men going_x000D_
towards that ship a while ago?"</p>_x000D_
</div>_x000D_
</div>
_x000D_