How to Center Text Horizontally and Vertically Inside a DIV Block Using CSS
Topic: HTML / CSSPrev|Next
Answer: Use CSS Flex
Layout
You can simply use the CSS3 flex layout model to align the text both vertically and horizontally center inside a DIV block. The flex or flexbox layout is supported in all major modern browsers.
Let's try out the following example to understand how it basically works:
Example
Try this code »<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Horizontally and Vertically Center Text Inside a DIV Block</title>
<style>
.wrapper{
display: flex;
align-items: center;
justify-content: center;
background: #dbdfe5;
height: 200px;
margin: 10px;
}
.text-block {
background: #abb1b8;
flex: 0 0 auto;
width: auto;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="text-block">Multiple lines of text<br> horizontally and vertically aligned.</div>
</div>
</body>
</html>
Related FAQ
Here are some more FAQ related to this topic: