As you know, we can use the line-height
property with a value that is equal to the height
property of a container to vertically center a single line within that container:
.container {
width: 300px;
height: 200px;
line-height: 200px;
text-align: center;
outline: 1px solid red;
}
<div class="container">
<p>This is some text.</p>
</div>
I have seen this method in many references, for example:
- AtoZ CSS Quick Tip: How to Vertically Center Text and Icons
- Centering Text with Line Height
- Center Text Vertically in CSS
Now the question is, why is the height
property set? Is it necessary, or can we simply manage with line-height
only?