Search code examples
csscalc

Can I use calc and var to change my div height based on its width?


So let's say my divs width of 470px is 100% and 350px is 0% of height.
From 100% to 0% I want my divs height to decrease from 100px to 0px.

Hope it's not too confusing...

Can it be done without javascript?

It's for an empty spacer div.


Solution

  • Codepen demo

    CSS

    div {
       background: #9bc;
       max-width: 470px;
       min-width: 350px;
       max-height: 100px;
       height: calc(0.83333 * 100vw - 291.66px);
       overflow: hidden;
    }
    

    The height has been calculated with the equation of the line passing from 2 points.

    I show you the calculations so you will be able to change that expression as you prefer

    enter image description here