Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using aspect-ratio in css with "calc"

I have a div with this css rule:

.somediv {
aspect-ratio: 1 / 3 !important;
}

I want to make the height equal not exactly (3 * width), but plus 8 pixels. For example, if my width is 10px, the height have to be like (10*3)+8 = 38 pixels. How can i do this, i know about calc() in css, but not sure it works with aspect-ratio.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you are to set a fixed width , you may use CSS var() to set the width, then use calc() for aspect-ratio

examples

:root {
  --width: 80;
}

div {
  margin: 1em;
  border: solid;
  width: calc(var(--width) * 1px);
  aspect-ratio: var(--width) / calc(var(--width) * 3 + 8);
  
  
/* demo purpose */
  float: left;
}

div:before{
content:attr(style);
<div>défault</div>
<div style="--width:100"> </div>
<div style="--width:101"> </div>
<div style="--width:70"> </div>
<div style="--width:90"> </div>

If width is not, then javascriipt will be required

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading