0

I need something for a dynamic project, and what I want is to make a class that every header element takes some properties, the children can be or not be direct descendants, however, inside that class can be another class that must overwrite the first class properties... I want those classes to be nested like 3-4 times.... is there a way to do this, to not take too much css space?

.something1 h1{
  color: green;
}

.something2 h1{
  color: red;
}
<div class='something1'>
  <div>
  <h1>Green</h1>
  </div>
  <div class='something2'>
    <h1>Red</h1>
    <div class='something1'>
      <div><div>
      <h1>Green</h1>
      </div></div>
      <div class='something2'>
        <div>
        <h1>Red</h1>
        </div>
      </div>
    </div>
  </div>
</div>

https://jsfiddle.net/eauwr8zo/1/

3 Answers 3

1

Using with > in css

.something1 > h1{
  color: green ;
}

.something2 > h1{
  color: red;
}
<div class='something1'>
  <h1>Green</h1>
  <div class='something2'>
    <h1>Red</h1>
    <div class='something1'>
      <h1>Green</h1>
      <div class='something2'>
        <h1>Red</h1>
      </div>
    </div>
  </div>
</div>

Using with !important; in css

.something1 > h1{
  color: green !important;
}

.something2 h1{
  color: red;
}
<div class='something1'>
  <h1>Green</h1>
  <div class='something2'>
    <h1>Red</h1>
    <div class='something1'>
      <h1>Green</h1>
      <div class='something2'>
        <h1>Red</h1>
      </div>
    </div>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

0

Add the ">" simbol to .something1 > h1 and .something2 > h1, like this it will only affect its direct child:

.something1 > h1{
  color: green;
}

.something2 > h1{
  color: red;
}
<div class='something1'>
  <h1>Green</h1>
  <div class='something2'>
    <h1>Red</h1>
    <div class='something1'>
      <h1>Green</h1>
      <div class='something2'>
        <h1>Red</h1>
      </div>
    </div>
  </div>
</div>

Comments

0

Use the direct descendant combinator >.

.something1 h1 {
  color: green;
}

.something2 > div > h1, .something2 > h1 {
  color: red;
}
<div class='something1'>
  <div>
  <h1>Green</h1>
  </div>
  <div class='something2'>
    <h1>Red</h1>
    <div class='something1'>
      <div><div>
      <h1>Green</h1>
      </div></div>
      <div class='something2'>
        <div>
        <h1>Red</h1>
        </div>
      </div>
    </div>
  </div>
</div>



Or if can't find a combination to use > that suits your random html,
then you can add a new selector by being even more specific by adding the same class multiple times.

.something1 h1,
.something1 .something1 h1{
  color: green;
}

.something2 h1,
.something2 .something2 h1 {
  color: red;
}
<div class='something1'>
  <div>
  <h1>Green</h1>
  </div>
  <div class='something2'>
    <h1>Red</h1>
    <div class='something1'>
      <div><div>
      <h1>Green</h1>
      </div></div>
      <div class='something2'>
        <div>
        <h1>Red</h1>
        </div>
      </div>
    </div>
  </div>
</div>

3 Comments

Thanks, I updated my question, the children is not necessary to be direct descendants
@Brada You can find a combination to use > that works for you, or add the same class multiple times. jsfiddle.net/eauwr8zo/2
Yes, i know, but I need to update the classes with PHP and in the end the resulting file may be some big....

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.