Chris P Chris P -5 years ago 100
HTML Question

How to vertically center an image and make it fill available width

I am trying to make a simple webpage that displays a GIF as big as possible but right in the center of the page. I would like the image to be centered vertically. Here is a link to JSFiddle.

<!DOCTYPE html>
<title>Gandalf Sax Guy!</title>

body {
overflow: hidden;
background-color: black;
margin: 0;

img {
width: 100%;
height: 100%;


<img src=""/>

<audio autoplay sr>
<source src="">

I'd like the image to be as big as possible, but not distorted, and work dynamically.

As you can see, I'd like the image to have an equal amount of "black space" above and below it.

Answer Source

flexbox to the rescue!


<div id="positioning-context">
    <img src=""/>


#positioning-context {
display: flex;
align-items: center;
justify-content: center;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download