If you want to center text within a box, you need to give a tag with text a height
property and then set the line-height
property to the same value as the height
property. Here's an example:
<html lang="en">
<head>
<meta charset="UTF-8>
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
/* -- rest css first -- */
*{
padding: 0;
margin: 0;
}
h2{
margin: 5px auto;
text-align: center;
background-color: rgba(89, 89, 89, 0.62);
font-family: Tahoma;
}
.box1{
width: fit-content;
height: 200px;
/*this is for text to be center of the box*/
line-height: 200px;
text-align: center;
/*----------------------------------------*/
background-color: #5548ff;
margin:10px auto;
color: #fff;
font-size: 25px;
font-family: Verdana;
padding: 5px;
}
.box2{
width: fit-content;
height: 300px;
/*this is for text to be center of the box*/
line-height: 300px;
text-align: center;
/*----------------------------------------*/
background-color: rgba(255, 46, 81, 0.76);
margin:10px auto;
color: #fff;
font-size: 25px;
font-family: Verdana;
padding: 5px;
}
</style>
</head>
<body>
<h2>In these boxes, height=line-height for text to be center of the box</h2>
<div class="box1">
This text is centered within the box
</div>
<div class="box2">
This text is centered within the box
</div>
</body>
</html>