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!
html code
<div class="image-container"> <img src=""/> </div>
css code
With flexbox this is easy:
FIDDLE
Just add the following to the image container:
Here is a technique to align inline elements inside a parent, horizontally and vertically at the same time:
Vertical Alignment
1) In this approach, we create an
inline-block
(pseudo-)element as the first (or last) child of the parent, and set itsheight
property to100%
to take all the height of its parent.2) Also, adding
vertical-align: middle
keeps the inline(-block) elements at the middle of the line space. So, we add that CSS declaration to the first-child and our element (the image) both.3) Finally, in order to remove the white space character between inline(-block) elements, we could set the font size of the parent to zero by
font-size: 0;
.Note: I used Nicolas Gallagher's image replacement technique in the following.
What are the benefits?
There's no need to specify the dimensions of the image element explicitly.
We can easily use this approach to align a
<div>
element vertically as well; which may have a dynamic content (height and/or width). But note that you have to re-set thefont-size
property of thediv
to display the inside text. Online Demo.The output
Responsive Container
This section is not going to answer the question as the OP already knows how to create a responsive container. However, I'll explain how it works.
In order to make the height of a container element changes with its width (respecting the aspect ratio), we could use a percentage value for top/bottom
padding
property.A percentage value on top/bottom padding or margins is relative to the width of the containing block.
For instance:
Here is the Online Demo. Comment out the lines from the bottom and resize the panel to see the effect.
Also, we could apply the
padding
property to a dummy child or:before
/:after
pseudo-element to achieve the same result. But note that in this case, the percentage value onpadding
is relative to the width of the.responsive-container
itself.Demo #1.
Demo #2 (Using
:after
pseudo-element)Adding the content
Using
padding-top
property causes a huge space at the top or bottom of the content, inside the container.In order to fix that, we have wrap the content by a wrapper element, remove that element from document normal flow by using absolute positioning, and finally expand the wrapper (bu using
top
,right
,bottom
andleft
properties) to fill the entire space of its parent, the container.Here we go:
Here is the Online Demo.
Getting all together
Here is the WORKING DEMO.
Obviously, you could avoid using
::before
pseudo-element for browser compatibility, and create an element as the first child of the.img-container
:UPDATED DEMO.
Using
max-*
propertiesIn order to keep the image inside of the box in lower width, you could set
max-height
andmax-width
property on the image:Here is the UPDATED DEMO.
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:
Working example on JSFiddle
You can center an image, both horizontally and vertically, using
margin: auto
and absolute positioning. Also:Try
Html
CSS