If you want to achieve this layout, the best approach is to utilize Flexbox. However, if you prefer not to use Flexbox, you can apply the following styles to the .units
element: text-align: center
, width: 100%
, and top: 62%
.
div.div1 {
height:200px;
width:200px;
background-color:orange;
position: relative;
}
span.number {
font-family: Verdana;
font-size: 36px;
font-weight: bold;
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
}
span.units {
margin: 0 auto;
position: absolute;
top: 62%;
left: 0;
text-align: center;
width: 100%;
}
<div class="div1">
<span class="number">1,000</span>
<br/>
<span class="units">miles</span>
</div>
I opted for a top
value of 62%
after some experimentation. Feel free to adjust this value if you find that the spacing between the value and unit is too close or too far apart.
Edit:
To achieve a more flexible and responsive layout without hardcoding values like 62%
, consider using Flexbox:
div.div1 {
height: 200px;
width: 200px;
background-color: orange;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
span.number {
font-family: Verdana;
font-size: 36px;
font-weight: bold;
}
<div class="div1">
<span class="number">1,000</span>
<span class="units">miles</span>
</div>
The unnecessary <br>
tag has been removed. Remember to use <br>
tags only within <p>
elements.