Hack Forums
How to center an image on a page - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Webmasters (https://hackforums.net/forumdisplay.php?fid=141)
+---- Forum: Website Construction (https://hackforums.net/forumdisplay.php?fid=50)
+---- Thread: How to center an image on a page (/showthread.php?tid=5506769)



How to center an image on a page - Strider64 - Dec 28, 2016

I was going to help someone out, but they closed the thread before I could post. So I'll post my solution here for anyone who's having the same problem. 👍
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;
                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>