Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - Can I use an object's Width in Calc()?

Tags:

css

calc

I want something like this:

height: calc(width + 5px);

This is just a small example but I'm ultimately looking for a way to use the set width as a part of the calculation, not a simpler way to solve the example. Any searches I do just gives examples on how to use calc() to set the width. Thanks!

like image 660
Austin Griner Avatar asked Feb 15 '26 06:02

Austin Griner


1 Answers

Use a container query. The cqw unit represents 1% of the item's width. You can use that unit in your calc() function.

I made the item resizable in the following demo, to make the functionality easier to observe.

.container {
  container-type: inline-size;
  background: #ddd;
  
  /* Make resizable, for demo purposes */
  resize: horizontal;
  overflow: hidden;
}

.item {
  background: orange;
}

@container (min-width: 0) {
  .item {
    /* 50% of container width + 30px */
    height: calc(50cqw + 30px);
  }
}
<div class="container">
  <div class="item">Resize me!</div>
</div>
like image 127
mfluehr Avatar answered Feb 16 '26 19:02

mfluehr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!