How to Center an Image on a HTML Page

Discussing Html . Code , Software , other

Moderators: egami, macek, gesf

Post Reply
User avatar
php-forum GURU
php-forum GURU
Posts: 398
Joined: Sat Mar 23, 2013 8:24 am
Location: Livonia, MI

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">
        <meta charset="UTF-8">
        <title>Center Image</title>
            * {
                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;
        <div class="screen">
                <img src="" alt="placeholder image">
                <figcaption>Center Image</figcaption>
Post Reply