This question already has an answer here:
- Maintain the aspect ratio of a div with CSS 21 answers
My question is very similar to this question: CSS: 100% width or height while keeping aspect ratio?
I have a div whose position is fixed. The width of the div must be 100% and its height exactly 1/6th of its width. Is there a -webkit-calc() way of doing this?
Note: JS solutions are not preferred as a zoom/orientation change can affect the width/height.
you could use jquery, e.g.
$('.someclass').css('width', 180); $('.someclass').css('height', $('.someclass').width() / 6);
moved the second suggestion from the comment for readability
you can always set div width like this:
EDIT:
or you could use something like this:
This is only an example-..But it is impossible to get height of a div in a pixels in the css file..you need to use jquery for that
EDIT:
height 1/6 of a width
Is this what you are after? I'm not using
-webkit-calc()
at all. I've inserted a1px
by6px
image into a outerdiv
which hasposition: fixed
applied to it, and set the image to have awidth
of100%
andposition: relative
. Then I have added an innerdiv
which is absolutely positioned to be as high and wide as its ancestor.Now you can change the width of the outer
div
, and the images'width: 100%
setting will ensure that both the outer and the innerdiv
's are guaranteed to always have a height equal to 1/6th of their width (or at least as close to exactly equal as it can get, the heights will be rounded off to the closest whole number of pixels). Any content could go inside the innerdiv
.HTML
CSS
Here's a jsFiddle showing the requested behaviour.
You can also use the solution I described in Responsive square columns.
It is based on the fact that %
padding-top/bottom
andmargin-top/bottom
are calculated according to the whidth of the parent element.Adapted to your situation it would look like this :
FIDDLE
HTML :
CSS :