This question already has an answer here:
- Maintain the aspect ratio of a div with CSS 21 answers
If I have a <div>
with a relative width (such as width: 50%), is there a simple way to make it have the same width as it does height without resorting to JavaScript?
Not with relative units like
%
, because they would be calculated relative to a container element. But it's possible to make an element square if you use units likepx
orem
to set both dimensions.No, and Yes (Kinda)
Okay, so the short answer is "no," not possible. The long answer is, "yes," given certain constraints and concessions (i.e. extra html markup, and limitations on what can be done).
Given this CSS:
With this HTML:
You can get it to do what this fiddle shows.
The keys are:
img
element is the only element that can do such proportional work, as it can base its size off the proportion of the image itself).width
or theheight
so that you can set the image class correctly to size it. Optionally, set thewidth
orheight
on theimg
itself, then you don't need to worry about setting a class to the100%
value. My demo was assuming that you set the size on the wrapper div (my.square
class).div
to collapse around theimg
which is driving the proportional sizing you need to setdisplay: inline-block
or afloat
on thediv
(as noted in the css above).div
to act more "block-like" you need to give them an extra wrapperdiv
like the third and fourth ones show.Obviously, there is a lot of extra mark-up involved in this solution. So in many ways it is better to say "just use javascript," but I did want to prove that it could be done (at least in some cases) purely with HTML and CSS.
Update: To Show Flexible Sizing Possibility
See this fiddle for percentages driving the size, with this html example (
width
set to30%
):A native JS approach in response to @Dave comment on @praveen-kumar answer:
and use it anywhere
This cannot be done with CSS alone. Using jQuery you can achieve this by doing
Check working example at http://jsfiddle.net/4Jnfq/
A CSS only solution can be found here on the last "Resize with content" update.
Although it applies for circles, you can remove the
border-radius: 50%
to make it work for squares.It is actually possible to achieve it with this neat trick i found at this blog
Hope that helps
I needed to achieve something similar today and had the same idea with the image. I wanted to check other possibilities and google led me here.
...you don't really need an image
src
. you can simply use ahash
if you want a square. It saves a http request.If you want to get different aspect ratio you should use a
src
though. If you want to have a ratio of 16:9 the image should be16px
wide and9px
high (go as small as possible)Comparing both techniques (see other answers in this thread)
img
vs.padding-bottom
Here's a fiddle to show how much more compatible the
img
version is (can easily handlepx
value too (an interval will resize the "square" each second)If you use percentage values both techniques can achieve the same result but the padding version does not need the extra markup (
<img src="#"/>
)Conclusion:
Depending on the implementation each technique has it's pros and contras.
HTML
CSS