How to Center an Image on a HTML Page

A

Anonymous

Guest
I was bored and decide to write a small how-to-center an image on a HTML Page.

Code:
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Center Image</title>
        <style>
            * {
                box-sizing: border-box;
                padding: 0;
                margin: 0;
            }
            div.screen {
                position: fixed;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;
            }
            figure {
                position: relative;
                top: 50%;
                left: 50%;
                display: block;
                width: 100%;
                max-width: 300px;
                height: 350px;
                z-index: 20000;
                background-color: skyblue;
                margin-left: -150px;
                margin-top: -175px;                
            }
            figcaption {
                font-family: Arial, Helvetica, sans-serif;
                font-size: 1.4rem;
                line-height: 1.5;
                color: #fff;
                text-align: center;
            }
        </style>
    </head>
    <body>
        <div class="screen">
            <figure>
                <img src="http://lorempixel.com/300/300" alt="placeholder image">
                <figcaption>Center Image</figcaption>
            </figure>
        </div>
    </body>
</html>
 
Back
Top