Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set an element's CSS value by another element's value?

Tags:

css

Say I have this code:

<div style="width:100%" id="a"></div>
<div id="b"></div>

I want the #b's height to equal #a's width and always following that value.

What I can do is to use JS to get #a's width and set it to #b's height.

However, is there any pure CSS way to do so, something like this:

#b {
  height:getvalue('#a', 'width');
}
like image 497
AGamePlayer Avatar asked Nov 20 '25 04:11

AGamePlayer


1 Answers

As Sangam Belose said setting both values in css is a valid option. You could define a variable in :root and access it from everywhere:

root:{
   --lenght: 100vw;
}
#a{
   width: var(--lenght);
}
#b{
   height: var(--lenght);
}

Unfortunally you would need JS to change this variable but most of the time there is a workaround for this like using aspect ratio.

like image 118
Alex Avatar answered Nov 22 '25 04:11

Alex



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!