CSS: Keeping a div's height relative to its width [duplicate]
Asked Answered
S

4

19

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.

Sumter answered 14/5, 2013 at 9:18 Comment(1)
Thanks for the solution. I personally cannot retest it as I had this problem over a year ago and have lost context. Perhaps the next person who has this problem would try out your solution. :)Sumter
B
11

Is this what you are after? I'm not using -webkit-calc() at all. I've inserted a 1px by 6px image into a outer div which has position: fixed applied to it, and set the image to have a width of 100% and position: relative. Then I have added an inner div 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 inner div'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 inner div.

HTML

<div>
  <div></div>
  <img src="https://dl.dropboxusercontent.com/u/6928212/sixbyone.png" />
</div>

CSS

html, body {
  margin: 0px;
  padding: 0px;
}
div {
  position: fixed;
  width: 100%;
}
div > div {
  position: absolute;
  top: 0px;
  right: 0px;
  bottom: 0px;
  left: 0px;      
}
img {
  width: 100%;
  display: block;      
  position: relative;
}

Here's a jsFiddle showing the requested behaviour.

Buote answered 14/5, 2013 at 11:10 Comment(1)
+1 Very nice, creative solution!Bandurria
R
9

You can also use the solution I described in Responsive square columns.

It is based on the fact that % padding-top/bottom and margin-top/bottom are calculated according to the whidth of the parent element.

Adapted to your situation it would look like this :

FIDDLE

HTML :

<div class="wrap">
    <div class="content">
    </div>
</div>

CSS :

.wrap{
    position:fixed;
    width:100%;
    padding-bottom:16.666%; /*  100x1/6 = 16.666...*/
}
.content{
    position:absolute;
    top:0;
    bottom:0;
    left:0;
    right:0;
}
Roxieroxine answered 4/4, 2014 at 14:55 Comment(0)
K
0

you can always set div width like this:

$('#div1').css("width", $('#div1').height()/6);

EDIT:

or you could use something like this:

/* Firefox */
width: -moz-calc(100% / 6);
/* WebKit */
width: -webkit-calc(100% / 6);
/* Opera */
width: -o-calc(100% / 6);
/* Standard */
width: calc(100% / 6);

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

$('#div1').css("height", window.width()/6);
Kileykilgore answered 14/5, 2013 at 9:21 Comment(6)
Thanks for the answer but this does not solve my question. width: -webkit-calc(100% / 16.66666666667); makes width 1/6th of PARENTS WIDTH. I need height 1/6th of width! :) Thanks anywaySumter
@Akshay but you can't do that in css, you can only do it with javascriptKileykilgore
I was reading about webkit calculations and was wondering if we could do it using that..Sumter
check my edited answer is this helpful?Kileykilgore
This was always my last approach. The reason is because my width is 100%, any changes in page layout like orientation change does not guarantee my height to be 1/6th the width. Ofcourse I can listen to the resize events and handle it, but a css solution if any will reduce the effortSumter
maybe you can put your width to auto then web-calc could potentially work? I know what you mean..I had the same problems...but trust me this is the only wayKileykilgore
A
0

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

$('.btnResize').click(function() { $('.div').css('height', $('.div').width()/6);});
Aeciospore answered 14/5, 2013 at 9:28 Comment(2)
Thanks. Does not solve the issue. width needs to be 100% only for resize issues. So, if I fix my height by JS once, and I zoom or change orientation, the height is not reset to 1/6th the width.Sumter
sorry, i misunderstood, and perhaps i still do; if it's only an issue on resize, then there must be a trigger event you could catch with jquery (see example in amended post); if it's not resized via button, then there should be another appropriate event to catchAeciospore

© 2022 - 2024 — McMap. All rights reserved.