0

Is there a pseudo selector or method to detect a <section> tag with an id. I.E. <section id="no_display">?

I have a header and it has a bottom margin of 35px. However, I don't want to apply the bottom margin on one page, but I want to apply it on all others.

Is there a method to detect an id and not apply the margin?

<header>
    //stuff here
</header>
<section id="no_display">
    //other stuff here
</section>

So, if <header> is immediately followed by section id="no_display", don't apply the bottom margin

Hopefully that makes sense.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Paul Dessert
  • 6,363
  • 8
  • 47
  • 74

2 Answers2

2

You can use negative margin for your section to compensate margin-bottom of its previous sibling element:

#no_display {
    margin-top: -35px;
}
Marat Tanalin
  • 13,927
  • 1
  • 36
  • 52
2

You don't usually use pseudo-classes to express relationships between two or more elements; you use combinators. In this case, you want a preceding-sibling combinator. Unfortunately, that doesn't exist.

What does exist are following-sibling combinators. Since you really only want the margin to be there if section doesn't have that ID, you can switch the bottom margin on header out for a top margin on section, and use an override rule for when it does have the ID to remove the margin from it:

header + section {
    margin-top: 35px;
}

header + section#no_display {
    margin-top: 0;
}

(You really don't need a selector like header + section#no_display since the ID alone is specific enough, but doing this helps me tell at a glance that the second rule is related to the first.)

Community
  • 1
  • 1
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • Haven't read all the way. My bad. Your answer is the right one so I take down my imaginary hat :) – iMoses Nov 10 '12 at 01:58