I'm trying to assign a value for an element's height based on the width value of a browser. For example (what I'm trying to accomplish is in between the double slash)...
.myDiv {
width: 35%;
height: // width * 1.61 //;
}
How can this be done?
I'm trying to assign a value for an element's height based on the width value of a browser. For example (what I'm trying to accomplish is in between the double slash)...
.myDiv {
width: 35%;
height: // width * 1.61 //;
}
How can this be done?
FYI:
Using percentage value for the width property refers to the width of the container block while the page is rendered.
While for the height property:
The percentage is calculated with respect to the height of the generated box's containing block. If the height of the containing block is not specified explicitly (i.e., it depends on content height), and this element is not absolutely positioned, the value computes to auto. A percentage height on the root element is relative to the initial containing block.
- MDN.
So even if you could achieve this with SASS, It doesn't work properly on the browser.
There are couple of solutions on SO, to keep the aspect ratio of a box:
You could apply padding-bottom as a percentage. Then put an element inside the div with position absolute.
Why can't you just do this?
.myDiv {
$width: 35%;
width: $width;
padding-bottom: ($width * 1.61)
}
Of course you'll need to make sure that .myDiv is positioned relative to the page, so in other words not contained inside another element with a position attribute.
As @NathanDawson suggests, you could also put another DIV inside myDiv and set the height to 100% if you need a proper height.