How to Center an Image on a HTML Page

Discussing Html . Code , Software , other

Moderators: macek, egami, gesf

Post Reply
User avatar
Strider64
php-forum Active User
php-forum Active User
Posts: 287
Joined: Sat Mar 23, 2013 8:24 am

How to Center an Image on a HTML Page

Post by Strider64 » Wed Dec 28, 2016 4:21 am

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

Code: Select all

<!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>

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest