Centering Text with CSS for Beginners
This guide explains how to center text using CSS!
It covers both horizontal and vertical alignment techniques and provides clear examples and practical tips that even beginners can easily understand.
Horizontal Centering of Text Using CSS
Elements with Horizontal Space: Using text-align: center
<h4>Element with Horizontal Space</h4>
h4 {
background-color: orange; /* Element that has horizontal space */
text-align: center; /* Centers text within the horizontal space of this element */
}
Element with Horizontal Space
Elements Without Horizontal Space: Apply text-align: center
to Ancestor Element with Horizontal Space
<div class="wrapper">
<span>Element Without Horizontal Space</span>
</div>
.wrapper {
background-color: orange; /* Ancestor element with horizontal space */
text-align: center; /* Centers text inside descendant elements based on ancestor's horizontal space */
}
.wrapper span {
background-color: yellow; /* Element without horizontal space */
}
Element Without Horizontal Space
Elements Without Horizontal Space: Use display: flex
and justify-content: center
on Parent with Horizontal Space
<div class="wrapper">
<span>Element Without Horizontal Space</span>
</div>
.wrapper {
background-color: orange; /* Parent element with horizontal space */
display: flex;
justify-content: center; /* Horizontally centers child elements */
}
.wrapper span {
background-color: yellow; /* Element without horizontal space */
}
Element Without Horizontal Space
Vertical Centering of Text Using CSS
Centering Text Within Its Own Element: Using line-height
<h4>Using line-height</h4>
h4 {
background-color: orange;
line-height: 200px; /* Vertically centers the text by setting line height equal to element height */
}
Using line-height
Centering Text Within Its Own Element: Using display: flex
and align-items: center
<h4>Using Flexbox</h4>
h4 {
background-color: orange;
height: 200px;
display: flex;
align-items: center; /* Vertically centers child content within the element */
}
Using Flexbox
Centering Text Within Its Own Element: Using display: table-cell
and vertical-align: middle
<h4>Using Table-Cell</h4>
h4 {
background-color: orange;
height: 200px;
display: table-cell;
vertical-align: middle; /* Vertically centers the text */
}
Using Table-Cell
Centering Child Elements Vertically in a Parent: Using display: flex
and align-items: center
<div class="box">
<span>Child Element</span>
</div>
.box {
background-color: orange;
height: 200px;
display: flex;
align-items: center; /* Vertically centers child elements */
}
.box span {
background-color: yellow;
}
Child Element
Centering Child Elements Vertically Using position
and transform
<div class="box">
<span>Child Element</span>
</div>
.box {
background-color: orange;
height: 200px;
position: relative;
}
.box span {
background-color: yellow;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
Child Element
Centering Text Horizontally and Vertically with CSS
Centering Text Within Its Own Element Using line-height
and text-align: center
<h4>Using line-height & text-algin: center</h4>
h4 {
background-color: orange;
text-align: center; /* Centers text horizontally within the element */
line-height: 200px; /* Vertically centers text by matching line-height to element height */
}
Using line-height & text-algin: center
Using display: flex
, justify-content: center
, and align-items: center
on a Parent Element with Horizontal Space
<div class="box">
<span>Child Element</span>
</div>
.box {
background-color: orange;
height: 200px;
display: flex;
justify-content: center; /* Centers child horizontally */
align-items: center; /* Centers child vertically */
}
.box span {
background-color: yellow;
}
Child Element
Using position
and transform
on Both Parent and Child Elements for Centering
<div class="box">
<span>Child Element</span>
</div>
.box {
background-color: orange;
height: 200px;
position: relative;
}
.box span {
background-color: yellow;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Child Element