0

Pardon me if the question is very basic, but I am learning CSS through online tutorials and templates and I cannot solve this doubt.

I have some headers descendants of objects with the same id/class tree. I want to target specifically one of them to change his format. More concretely, my code is:

<aside id="id-one" class="js-classone">
    <div class="class-two js-classone">
        <ul class="class-three">
            <li>
                <div class="class-four"></div>
                <div class="class-five">
                <div class="class-six  js-classone">
                <div class="class-seven">
                    <h2 id="id-target">Targeted h2</h2>
                    <p class="class-eight">some text</p>    
                </div>
                </div>
                </div>
            </li>
        </ul>
    </div>
</aside>  

At other parts of the web (not the same file .html, a different one) the h2 are not targeted (hence, with no id), but they are under the same parent/tree of id and classes.

My template came with some style.css applying to this headers as

#id-one .class-two .class-six > .class-seven .class-eight {
  color: #fff;
}

If I now add to my style.css

#id-target {
  color: #000;
}

for some reason I don't understand the first CSS code overwrites the color of #id-target. Hence, my question:

#id-one .class-two .class-six>.class-seven .class-eight {
  color: #fff;
}

#id-target {
  color: #000;
}
<aside id="id-one" class="js-classone">
  <div class="class-two js-classone">
    <ul class="class-three">
      <li>
        <div class="class-four"></div>
        <div class="class-five">
          <div class="class-six  js-classone">
            <div class="class-seven">
              <h2 id="id-target">Targeted h2</h2>
              <p class="class-eight">some text</p>
            </div>
          </div>
        </div>
      </li>
    </ul>
  </div>
</aside>

Question: Do you know why #id-target does not apply? What should someone change in order to format the targeted h2?

Thanks in advance and please correct whatever in this question which is not adequately asked.

connexo
  • 41,035
  • 12
  • 60
  • 87
Tintin
  • 101
  • 3
  • 1
    `

    some text` look closely.

    – connexo Jan 02 '18 at 10:23
  • Thank you @connexo , the question is now edited (the original code is right) – Tintin Jan 02 '18 at 10:26
  • 1
    The dev tools inspector will show you why your rule is not applied. – connexo Jan 02 '18 at 10:26
  • 1
    i thing you can use `#id-target { color: #000 !important; }` and other option add a new class than write css what u want. – lalit bhakuni Jan 02 '18 at 10:30
  • Thank you @connexo, I am not experienced with the dev tool inspector, but I think it says that the first css code I wrote overwrites the second, which I don't understand why. The dev tool allow me to "uncheck" the first css command for color, then the second css apply. – Tintin Jan 02 '18 at 10:41
  • 1
    @lalit Suggesting `!important` to solve specificity issues you don't even understand is **very bad advice**. – connexo Jan 02 '18 at 10:43
  • thank you @lalitbhakuni, and connexo. With lalitbhakuni suggestion the second css apply, so at least I obtain what I want. Now I would like to understand why it happens that way – Tintin Jan 02 '18 at 10:44
  • 1
    I created a snippet from your code which I edited into your post. As you can see, your `#id-target` rule gets applied as expected. – connexo Jan 02 '18 at 10:46
  • it's option and other option is add `class` is the best way to write css . – lalit bhakuni Jan 02 '18 at 10:46
  • Thank you @connexo Now I am totally confused, I don't know why it doesn't apply on my webpage.... But I least I know that my basics of html and css are solid! Things should work as I expected them – Tintin Jan 02 '18 at 10:53

1 Answers1

2

Oh boy, a couple of issues.

  1. Your p tag has no closing tag.
  2. You have closed your h2 tag twice.
  3. You have applied a color: white; to your p tag.
  4. You are trying to apply color: #000; to your h2 tag.

In order for you to see the results properly

  1. Nest your code properly.
  2. Close any tags that you open.
  3. Don't have overspecific selectors in CSS, i.e. #id-one .class-two .class-six > .class-seven .class-eight can be simply written as .class-eight (which is a paragraph).
  4. Also as a good practice give your classes and IDs meaningful names for them to be descriptive of how you would like to use them.

Hope that helps.

davi_singh
  • 110
  • 7
  • 1
    While this does not solve the problem, it is generally good advice. – connexo Jan 02 '18 at 10:49
  • 1
    "Your p tag has no closing tag" — The end tag for p elements is optional – Quentin Jan 02 '18 at 10:52
  • 1
    While what Quentin says is correct, I'd strongly advise beginners to properly close each and every tag they use until they get to the point where they know exactly which tags must be closed, and on what tags closing is optional. – connexo Jan 02 '18 at 10:55
  • Thanks everybody for their comments and help. The name on the classes and id's here is given to make you easier reading. Thanks to @connexo snippet it seems there is a previous bug on my code. I'll try to debug my it – Tintin Jan 02 '18 at 11:28
  • Thanks @Quentin for pointing out the right answer. The problem was about points and weight of the css call. The other question is very well written – Tintin Jan 05 '18 at 16:25