view raw
Dhrub Kumar Dhrub Kumar - 8 months ago 32
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;">



h2 {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);