Skip to content

Instantly share code, notes, and snippets.

@anshuraj
Created July 20, 2018 13:15
Show Gist options
  • Save anshuraj/3f85ad1ab67e620a1b75a5e64c1eb04a to your computer and use it in GitHub Desktop.
Save anshuraj/3f85ad1ab67e620a1b75a5e64c1eb04a to your computer and use it in GitHub Desktop.
Center image horizontally and vertically inside a div.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="responsive-container">
<div class="helper"></div>
<div class="img-container">
<div class="centerer"></div>
<img src="http://via.placeholder.com/350x150" >
</div>
</div>
</body>
</html>
.responsive-container {
width: 100%;
position: relative;
height: inherit;
}
.helper {
padding-top: 100%;
}
.img-container {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
text-align: center;
font: 0/0 a;
}
.img-container .centerer {
display: inline-block;
vertical-align: middle;
height: 100%;
}
.img-container img {
vertical-align: middle;
display: inline-block;
max-height: 100%;
max-width: 100%;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment