1

How do I select the <p> element here? The only constant that I have is the id "Standort".

Any ideas?

<section>
  <header>
    <h2 class="licht"><span id="Standort" class="-sitemap-select-item-selected">Standort</span></h2>
  </header>
  <p>Lorem ipsum</p>
</section>
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
maexls
  • 37
  • 1
  • 7

1 Answers1

1

You can use the adjacent sibling combinator:

header+p {
  color: red;
}
<section>
  <header>
    <h2 class="licht"><span id="Standort" class="-sitemap-select-item-selected">Standort</span></h2>
  </header>
  <p>Lorem ipsum</p>
</section>

If you only can base yourself on the ID of a child element contained in the header, there's no pure CSS solution, and you'd have to rely on JavaScript:

document.getElementById('Standort')
  .closest('header')
  .nextElementSibling
  .style.color = 'red';
<section>
  <header>
    <h2 class="licht"><span id="Standort" class="-sitemap-select-item-selected">Standort</span></h2>
  </header>
  <p>Lorem ipsum</p>
</section>
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • Thanks, but I only need to select the one with id "Standort" - possible? – maexls Dec 09 '18 at 12:21
  • That would first require a parent selector in CSS, which doesn't exist. – Robby Cornelissen Dec 09 '18 at 12:23
  • Since OP's only known reference is the stated _"The only constant that I have is the id `Standort`"_, you should remove the first part in your answer, as that is wrong. – Asons Dec 09 '18 at 12:43
  • @LGSon In that case, the question title needs to be edited. – Robby Cornelissen Dec 09 '18 at 12:48
  • No, not necessarily, as you shouldn't base an answer on a title alone, the question's context needs to be read too, where it is stated there is a constraint. – Asons Dec 09 '18 at 12:52
  • And as this is a clear dupe question (and the dupe is a good one that have all the workarounds), I don't understand why you don't delete this answer. – Asons Dec 09 '18 at 13:00
  • @Robby Cornelissen: I edited the title. That was a very misleading title in the first place. – BoltClock Dec 10 '18 at 03:28
  • @LGSon: Not everyone deletes their answers to duplicate questions as soon as they get closed, or at all. I keep my answers to duplicate questions sometimes, when 1) I took the time and effort to write them and did so in good faith for any of a number of reasons, and 2) they provide something specific to the question that may or may not already be covered in the linked question (and are more often than not irrelevant there anyway). – BoltClock Dec 10 '18 at 03:35