Z
Z
zrim2021-07-11 16:17:13
css
zrim, 2021-07-11 16:17:13

CSS Variable Inheritance. How to do it right?

The situation is the following. There is a CSS variable A which sets the color. This variable is used for another variable (B), which in turn is used to set the background color. If you change the color of variable A for a nested element, and specify that the background color of the element is equal to variable B, then the background color does not change. How to make it work. Do not ask why such frills - this is a greatly simplified scheme of the working system.

A demonstration of the problem can be viewed here

HTML

<div class="test-block bg-color">
  В этом пряугольнике должне быть красный цвет фона.
</div>


css
:root{
  --sf-color: #DBEDFF;
  --sf-test: var(--sf-color);
  background-color: var(--sf-test);
}

.bg-color{
  --sf-color: #FDAEB7;
  background-color: var(--sf-test);
}

.test-block{
  width: 100%;
  height: 50vh;
  border: 2px solid #333;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Fedorov, 2021-07-11
@aliencash

.bg-color{
  --sf-color: #FDAEB7;
  --sf-test: var(--sf-color);
  background-color: var(--sf-test);
}

Z
zrim, 2021-07-12
@zrim

the issue was resolved. solution here https://codepen.io/simai/pen/GRmjPrK

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question