0

I want to make my height depending on the width, The smaller the width, the higher the height as in keeping a constant surface area of the element. How can I achieve that using only CSS?

height: calc(???)

None of the answer of Maintain the aspect ratio of a div with CSS is answering my question. I don't need it to have a constant aspect ratio but a constant surface area.

Bowdzone
  • 3,827
  • 11
  • 39
  • 52
kpda806G
  • 103
  • 1
  • 6

1 Answers1

0

Here height automatically becomes half of the width. --box-width:300px and /2 changeable.

:root {
  --box-width: 300px;
}

.box {
  width:var(--box-width);
  height:calc(var(--box-width)/2);
  background:pink;
}
<div class="box"></div>
doğukan
  • 23,073
  • 13
  • 57
  • 69