Created
July 20, 2018 13:15
-
-
Save anshuraj/3f85ad1ab67e620a1b75a5e64c1eb04a to your computer and use it in GitHub Desktop.
Center image horizontally and vertically inside a div.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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