Use CSS (and maybe JavaScript) to make an element be square (or maintain a specific aspect ratio)

Posted by David Pfeffer on Stack Overflow See other posts from Stack Overflow or by David Pfeffer
Published on 2010-01-14T17:23:24Z Indexed on 2010/04/22 5:43 UTC
Read the original article Hit count: 175

Filed under:
|
|
|

I have a div that I want to have the following characteristics:

  • Width = 50% of its parent element
  • Height equal to whatever it needs to be in order to maintain a certain aspect ratio.

I need to use percentages because the object will resize left-right when the browser is resized. I want the object to be resized top-bottom to ensure the object maintains the same aspect ratio.

I don't think there's any way to use pure CSS to do this, but does anyone know of a way? Alternatively, is there an easy JavaScript way to do this? (JQuery is fine.)

© Stack Overflow or respective owner

Related posts about css

Related posts about JavaScript