Try the technique discussed in the following article:
http://alistapart.com/article/creating-intrinsic-ratios-for-video
I have my own poor-man's version of the above.
You define a container block with two child elements, an image and another block element:
<div class="sq-wrapper">
<img class="spacer" src="http://placehold.it/100x100">
<div class="content">Some content...</div>
</div>
Just create a lightweight image with the aspect ratio that you need.
Apply the following CSS:
.sq-wrapper {
position: relative;
display: inline-block;
width: 100%;
border: 1px solid gray;
}
.sq-wrapper .spacer {
width: 100%;
visibility: hidden;
vertical-align: top;
}
.sq-wrapper .content {
position: absolute;
top: 0;
left: 0;
padding: 20px;
}
Give the .sq-wrapper
a width of 100% and it will expand to fill its containing block.
Give the .spacer
child element/image a width of 100% so that it will expand to fill the .sq-wrapper
. Because the image has an intrinsic height, .sq-wrapper
will shrink-to-fit the image because of the inline-block
display type.
Use visibility: hidden
to hide the image but preserve its space within the layout,
and vertical-align: top
to deal with the extra-whitespace-at-the-bottom issue.
Finally, take the .content
out of the flow by using position: absolute
.
You can see a demo at: http://jsfiddle.net/audetwebdesign/BQ2eJ/
The one thing to watch out for is that content could overflow if you shrink the window to a small enough size.
Reference
To learn how CSS computes the height of an inline replaced element, see:
http://www.w3.org/TR/CSS2/visudet.html#inline-replaced-height