I have the following code which sets up a container which has a height that changes with the width when the browser is re-sized (to maintain a square aspect ratio).
HTML
<div class="responsive-container">
<div class="dummy"></div>
<div class="img-container">
<IMG HERE>
</div>
</div>
CSS
.responsive-container {
position: relative;
width: 100%;
border: 1px solid black;
}
.dummy {
padding-top: 100%; /* forces 1:1 aspect ratio */
}
.img-container {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
How can I vertically align the IMG inside the container? All my images have variable heights and the container can't have a fixed height/line height because it's responsive... Please help!
This question is related to
html
css
responsive-design
vertical-alignment
I came across this thread in search of a solution that:
Testing some of the solutions posted above I didn't find one to meet all of this criteria, so I put together this simple one which might be useful for other people needing to do the same:
.container {_x000D_
width: 30%;_x000D_
float: left;_x000D_
border: 1px solid turquoise;_x000D_
margin-right: 3px;_x000D_
margin-top: 3px;_x000D_
}_x000D_
_x000D_
.container:last-of-kind {_x000D_
margin-right: 0px;_x000D_
}_x000D_
_x000D_
.image-container {_x000D_
position: relative;_x000D_
overflow: hidden;_x000D_
padding-bottom: 70%;_x000D_
/* this is the desired aspect ratio */_x000D_
width: 100%;_x000D_
}_x000D_
_x000D_
.image-container img {_x000D_
position: absolute;_x000D_
/* the following 3 properties center the image on the vertical axis */_x000D_
top: 0;_x000D_
bottom: 0;_x000D_
margin: auto;_x000D_
/* uses image at 100% width (also meaning it's horizontally center) */_x000D_
width: 100%;_x000D_
}
_x000D_
<div class="container">_x000D_
<div class="image-container">_x000D_
<img src="http://placehold.it/800x800" class="img-responsive">_x000D_
</div>_x000D_
</div>_x000D_
_x000D_
<div class="container">_x000D_
<div class="image-container">_x000D_
<img src="http://placehold.it/800x800" class="img-responsive">_x000D_
</div>_x000D_
</div>_x000D_
_x000D_
<div class="container">_x000D_
<div class="image-container">_x000D_
<img src="http://placehold.it/800x800" class="img-responsive">_x000D_
</div>_x000D_
</div>
_x000D_
Working example on JSFiddle
Try
Html
<div class="responsive-container">
<div class="img-container">
<IMG HERE>
</div>
</div>
CSS
.img-container {
position: absolute;
top: 0;
left: 0;
height:0;
padding-bottom:100%;
}
.img-container img {
width:100%;
}
Try this one
.responsive-container{
display:table;
}
.img-container{
display:table-cell;
vertical-align: middle;
}
With flexbox this is easy:
Just add the following to the image container:
.img-container {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
display: flex; /* add */
justify-content: center; /* add to align horizontal */
align-items: center; /* add to align vertical */
}
Here's a technique that allows you to center ANY content both vertically and horizontally!
Basically, you just need a two containers and make sure your elements meet the following criteria.
display: table;
display: table-cell;
vertical-align: middle;
text-align: center;
display: inline-block;
If you use this technique, just add your image (along with any other content you want to go with it) to the content box.
body {_x000D_
margin : 0;_x000D_
}_x000D_
_x000D_
.outer-container {_x000D_
position : absolute;_x000D_
display: table;_x000D_
width: 100%;_x000D_
height: 100%;_x000D_
background: #ccc;_x000D_
}_x000D_
_x000D_
.inner-container {_x000D_
display: table-cell;_x000D_
vertical-align: middle;_x000D_
text-align: center;_x000D_
}_x000D_
_x000D_
.centered-content {_x000D_
display: inline-block;_x000D_
background: #fff;_x000D_
padding : 12px;_x000D_
border : 1px solid #000;_x000D_
}_x000D_
_x000D_
img {_x000D_
max-width : 120px;_x000D_
}
_x000D_
<div class="outer-container">_x000D_
<div class="inner-container">_x000D_
<div class="centered-content">_x000D_
<img src="https://i.stack.imgur.com/mRsBv.png" />_x000D_
</div>_x000D_
</div>_x000D_
</div>
_x000D_
See also this Fiddle!
Use this css, as you already have the markup for it:
.img-container {
position: absolute;
top: 50%;
left: 50%;
}
.img-container > img {
margin-top:-50%;
margin-left:-50%;
}
Here is a working JsBin: http://jsbin.com/ihilUnI/1/edit
This solution only works for square images (because a percentage margin-top value depends on the width of the container, not the height). For random-size images, you can do the following:
.img-container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%); /* add browser-prefixes */
}
Working JsBin solution: http://jsbin.com/ihilUnI/2/edit
Make another div and add both 'dummy' and 'img-container' inside the div
Do HTML and CSS like follows
html , body {height:100%;}_x000D_
.responsive-container { height:100%; display:table; text-align:center; width:100%;}_x000D_
.inner-container {display:table-cell; vertical-align:middle;}
_x000D_
<div class="responsive-container">_x000D_
<div class="inner-container">_x000D_
<div class="dummy">Sample</div>_x000D_
<div class="img-container">_x000D_
Image tag_x000D_
</div>_x000D_
</div> _x000D_
</div>
_x000D_
Instead of 100% for the 'responsive-container' you can give the height that you want.,
html code
<div class="image-container">
<img src=""/>
</div>
css code
img
{
position: relative;
top: 50%;
transform: translateY(-50%);
}
You can center an image, both horizontally and vertically, using margin: auto
and absolute positioning. Also:
.responsive-container {_x000D_
margin: 1em auto;_x000D_
min-width: 200px; /* cap container min width */_x000D_
max-width: 500px; /* cap container max width */_x000D_
position: relative; _x000D_
overflow: hidden; /* crop if image is larger than container */_x000D_
background-color: #CCC; _x000D_
}_x000D_
.responsive-container:before {_x000D_
content: ""; /* using pseudo element for 1:1 ratio */_x000D_
display: block;_x000D_
padding-top: 100%;_x000D_
}_x000D_
.responsive-container img {_x000D_
position: absolute;_x000D_
top: -999px; /* use sufficiently large number */_x000D_
bottom: -999px;_x000D_
left: -999px;_x000D_
right: -999px;_x000D_
margin: auto; /* center horizontally and vertically */_x000D_
}
_x000D_
<p>Note: images are center-cropped on <400px screen width._x000D_
<br>Open full page demo and resize browser.</p>_x000D_
<div class="responsive-container">_x000D_
<img src="http://lorempixel.com/400/400/sports/9/">_x000D_
</div>_x000D_
<div class="responsive-container">_x000D_
<img src="http://lorempixel.com/400/200/sports/8/">_x000D_
</div>_x000D_
<div class="responsive-container">_x000D_
<img src="http://lorempixel.com/200/400/sports/7/">_x000D_
</div>_x000D_
<div class="responsive-container">_x000D_
<img src="http://lorempixel.com/200/200/sports/6/">_x000D_
</div>
_x000D_
Source: Stackoverflow.com