How to make text vertically and horizontally center in an HTML page

Centering horizontally is easy – centering vertically is a bit tricky in css as it’s not really supported (besides table cells <td>, which is bad style for layouting unless a table is really needed as – well – a table). But you can use semantically correct html tags and apply table display properties to it.

That’s one possible solution – there are many approaches, here is a good article on that.

In your case something like that should be sufficient:

<!DOCTYPE html>
<html lang="de">
    <head>
        <title>Hello World</title>
        <style>

        html, body {
            height: 100%;
            margin: 0;
            padding: 0;
            width: 100%;
        }

        body {
            display: table;
        }

        .my-block {
            text-align: center;
            display: table-cell;
            vertical-align: middle;
        }
        </style>
    </head>
    <body>
    <div class="my-block">
       WORD1<br />
       WORDWORDWORDWORD2
    </div>
    </body>
</html>

Leave a Comment