Using only CSS, I don’t see any solution outside the suggestions they gave in the comments: Use a class containing the specific rules that the element should have.
But I think that still would not be what you are looking for, would not be dynamic, even because CSS has no events and you will not be able to control the "change" in an element.
Then, I would use Javascript to get the color of the element and assign it to others. The control of "who is the target element?" and "who are the elements that will be affected?" can we do with data Attributes. For example:
(function(){
var backgroundColor = document.querySelector('[data-parent]').style.backgroundColor,
targets = document.querySelectorAll('[data-target]');
for(var i = 0; i < targets.length; i++)
targets[i].style.backgroundColor = backgroundColor;
})();
<div data-parent style='background-color:blue'>Eu sou azul :)</div>
<hr>
<div data-target>Eu serei afetado.</div>
<div data-target>Eu também.</div>
<div>Eu não.</div>
<div data-target>Eu sim.</div>
Dynamic example
(function(){
document.querySelector('button').addEventListener('click', function(){
setRandomParentColor(); // Altera a cor do 'data-parent'
setTargetsColors(getParentColor()); // Pega a cor do elemento e altera todos os 'data-target'
});
/**
* Gera uma cor randomica e altera a cor de backgroundd
* do elemento com atributo 'data-parent'.
*
* créditos - @ZPiDER: http://stackoverflow.com/a/5365036/4056678
*/
function setRandomParentColor(){
var color = "#"+((1<<24)*Math.random()|0).toString(16); // cor "randomica"
document.querySelector('[data-parent]').style.backgroundColor = color;
}
/**
* Obtém a cor do elemento com atributo 'data-parent'.
*/
function getParentColor(){
return document.querySelector('[data-parent]').style.backgroundColor;
}
/**
* Altera a cor de background de todos os elementos
* com atributo 'data-target'.
*/
function setTargetsColors(color){
var targets = document.querySelectorAll('[data-target]'),
len = targets.length;
for(var i = 0; i < len; i++)
targets[i].style.backgroundColor = color;
}
setTargetsColors(getParentColor());
})();
<div data-parent style='background-color:blue'>Eu sou azul :)</div>
<button>Mudar cor</button>
<hr>
<div data-target>Eu serei afetado.</div>
<div data-target>Eu também.</div>
<div>Eu não.</div>
<div data-target>Eu sim.</div>
<div>Eu também não.</div>
You can use a class in all these elements, select them with Javascript and change the background-color of all of them in a single taca, that would be it?
– Zignd
in case it is only in css itself, because it has not yet defined the final color, can not apply without being in a new class? just as I explained in the question.
– Leonardo
Well you can start using SASS or LESS, which are basically pre-processed CSS languages, with them you create scripts that after running generates the CSS ready, with this you can use variables and other things that exist, in the programming languages.
– SK15
There is no way, the classes are static, you cannot change them after loading the page. The only way would be using Javascript, changing the style of the elements through the style attribute.
– Zignd
@SK15 does not understand how SASS or LESS could help in this case, CSS will be generated in the same way and he wants to change the background-color at runtime.
– Zignd
@Zignd Because in SASS or LESS I can choose a variable and put a color in it and apply it in all my elements, so when I change the color all my elements change the color automatically, but as I said if it is really run-time only with Javascript, unless you use a SASS/LESS compiler in general time.
– SK15
@SK15 for me the question became a little confused, so I may have misunderstood what he wants to do.
– Zignd
@Ivcs You can create a variable in SASS and using a map, dynamically change the color directly in Chrome Devtools https://developer.chrome.com/devtools/docs/css-preprocessors
– rzani