<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 */
}
Result
<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 */
}
Result
<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 */
}
Result
<h4>Using line-height</h4>
h4 {
    background-color: orange;
    line-height: 200px; /* Vertically centers the text by setting line height equal to element height */
}
Result
<h4>Using Flexbox</h4>
h4 {
    background-color: orange;
    height: 200px;
    display: flex;
    align-items: center; /* Vertically centers child content within the element */
}
Result
<h4>Using Table-Cell</h4>
h4 {
    background-color: orange;
    height: 200px;
    display: table-cell;
    vertical-align: middle; /* Vertically centers the text */
}
Result
<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;
}
Result
<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%);
}
Result
<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 */
}
Result
<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;
}
Result
<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%);
}
Result