0

Set css variable equal to class property

I want to set a css variable equal to the height of one of my divs.

I tried

:root{
    --height-of-div: .div1.height;
}

.div2{
    height: calc(--height-of-div/2);
}

But it is not working. Please help.

7
  • It can't be done Commented Sep 7, 2020 at 7:13
  • this is not valid css syntax, it does not have variables Commented Sep 7, 2020 at 7:13
  • this is not possible without javascript Commented Sep 7, 2020 at 7:15
  • i assume you want to add logic directly to your css file, well, as stated above by others, you cannot do it with css. You should look at SCSS for what you are looking for Commented Sep 7, 2020 at 7:19
  • How would I do it in SCSS Commented Sep 7, 2020 at 7:28

1 Answer 1

1

Not possible using CSS alone. You could use jQuery, something like this:

function SetDivHeight() {
         $("#div2").css({'height':($("#div1").height())});
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.