Dhrub Kumar Dhrub Kumar - 1 year ago 63
HTML Question

Center aligning a text horizontally and vertically in a div element

I have made a div of fixed height and I want the text to align both
vertically and horizontally in the div.

I tried using margin:auto and
text-align but it didn't help.

I am beginner HTML/CSS.

<!DOCTYPE html>

<div style="background-color:black;color:white;padding:20px; height:400px;">



Answer Source
h2 {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download