155

I've got the following HTML code on a page:

<h4>Some text</h4>
<p>
Some more text!
</p>

In my .css I've got the following selector to style the h4 element. The HTML code above is just a small part of the entire code; there are several divs more wrapped around belonging to a shadowbox:

#sb-wrapper #sb-wrapper-inner #sb-body #myDiv h4
{
    color               : #614E43;
    margin-top          : 5px;
    margin-left         : 6px;
}

So, I have the correct style for my h4 element, but I also want to style the p tag in my HTML.

Is this possible with CSS-selectors? And if yes, how can I do this?

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
Rob
  • 6,731
  • 12
  • 52
  • 90

5 Answers5

251
#many .more.selectors h4 + p { ... }

This + is called the adjacent sibling selector.

Adjacent sibling selectors have the following syntax: E1 + E2, where E2 is the subject of the selector. The selector matches if E1 and E2 share the same parent in the document tree and E1 immediately precedes E2, ignoring non-element nodes (such as text nodes and comments).

David d C e Freitas
  • 7,481
  • 4
  • 58
  • 67
Phrogz
  • 296,393
  • 112
  • 651
  • 745
  • 1
    An alternative is to use JS to find your `h4` elements, walk to the next sibling and add a CSS class to that. With jQuery, this would simply be `$('#sb-wrapper #sb-wrapper-inner #sb-body #myDiv h4').next().addClass('shadowbox-h4-p');` – Phrogz Jan 07 '11 at 14:10
  • 3
    Note that this only works on elements following the first one IMMEDIATELY. If you want to have the second next html element, you can chain it up like this: #selector .original + h4 + p to get the p following an h4 following an .original element. Very usefull – user1610743 Apr 01 '14 at 17:07
54

For your literal example you'd want to use the adjacent selector (+).

h4 + p {color:red}//any <p> that is immediately preceded by an <h4>

<h4>Some text</h4>
<p>I'm red</p>
<p>I'm not</p>

However, if you wanted to select all successive paragraphs, you'd need to use the general sibling selector (~).

h4 ~ p {color:red}//any <p> that has the same parent as, and comes after an <h4>

<h4>Some text</h4>
<p>I'm red</p>
<p>I am too</p>

It's known to be buggy in IE 7+ unfortunately.

Joel Mellon
  • 3,672
  • 1
  • 26
  • 25
21

Just hit on this when trying to solve this type of thing my self.

I did a selector that deals with the element after being something other than a p.

.here .is.the #selector h4 + * {...}

Hope this helps anyone who finds it :)

Tom
  • 229
  • 2
  • 4
8

Simplifying for the beginners:

If you want to select an element immediately after another element you use the + selector.

For example:

div + p The + element selects all <p> elements that are placed immediately after <div> elements


If you want to learn more about selectors use this table.

Canned Man
  • 734
  • 1
  • 7
  • 26
Trooller
  • 179
  • 1
  • 2
  • 9
-2

I use latest CSS and "+" didn't work for me so I end up with

:first-child

VNicholson
  • 25
  • 4