Take another element’s background-color

Asked

Viewed 1,404 times

2

It’s boring to keep changing the background color of various objects, I want to know if there’s any way I can define the color of an object and then just match the color of others with the color of that object, to make something dynamic, change the color of all at the same time and in the code I only indicate the color in one. Example, I have a div.teste define your color as blue.. how do I make other Ivs and other tags take the same color from div.test without having to go out assigning blue to everyone.

  • 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?

  • 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.

  • 1

    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.

  • 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.

  • @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 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 for me the question became a little confused, so I may have misunderstood what he wants to do.

  • @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

Show 3 more comments

2 answers

2

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>

0

You can try using background-color:inherit set the background-color for the parent element...and in the child elements put this property so that they inherit from the parent element. You can create a div with main and put other Divs in, ex:

<div id="principal" style="background-color:#CCC">
  
  
  
  
  <div id="div1" style="background-color:inherit">
    
    
    </div>
  
  
  
  
  <div id="div2" style="background-color:inherit">
    
    
    </div>
  
   
  
  
  </div>

In case you would need to change only the main div...because the others would inherit the property, Voce can also do in css this way:

div[name=colorir]{

  background-color:#CCC;

}

Then just name the Divs you want to change the background with the name colorize

  • 1

    At no time did he say that the elements were each other’s children.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.