<div class="ghost-center">
<p>I'm vertically centered multiple lines of text in a container. Centered with a ghost pseudo element</p>
</div>
body {
background: #f06d06;
font-size: 80%;
}
div {
background: white;
width: 240px;
height: 200px;
margin: 20px;
color: white;
resize: vertical;
overflow: auto;
padding: 20px;
}
.ghost-center {
position: relative;
letter-spacing: -0.5em; /* remove the gap between the ::after element and the vertical aligned content */
}
.ghost-center > * {
letter-spacing: normal; /* reset letter-spacing */
}
.ghost-center::after {
content: " ";
display: inline-block;
height: 100%;
width: 0;
vertical-align: middle;
}
.ghost-center p {
display: inline-block;
vertical-align: middle;
margin: 0;
padding: 20px;
background: black;
}
This Pen doesn't use any external CSS resources.