CSS: adjacent sibling selector (+)
Next-Sibling Combinator
tag1 + tag2-
match element x if:
- tag2 matches x.
- tag1 matches a element that is a sibling of x, and it comes immediately before x.
tag can be a simple selector or compound selector.
/* match the b tag that comes immediately after span */ span + b { color: red; } <div> <span>abc</span> <b>match</b> </div>
Example. First paragraph after header
h1 + p { color: red; }
Example. Styling forms
label + input { display: block; margin-bottom: 15px; padding: 8px; }
<form> <label for="name">Name</label> <input type="text" id="name"> <label for="email">Email</label> <input type="email" id="email"> </form>
CSS selectors by child or sibling rank
CSS. Selectors
Selector types
Simple selectors
- CSS: type selector (tag name)
- CSS: universal selector (* any tag)
- CSS: class selector (.x)
- CSS: ID selector (#)
- CSS: attribute selector ([x])
Combinators
- CSS: descendant selector (space)
- CSS: child selector (>)
- CSS: adjacent sibling selector (+)
- CSS: subsequent sibling selector (~)
Selector list
Nesting syntax
Special selector
- CSS: :root selector
- CSS: no child selector
- CSS: first child, last child, only child selectors
- CSS: nth-child selector
- CSS: nth-child arg of selector
- CSS: nth-of-type selector
- CSS: pseudo-class selector (:)
- CSS: pseudo-element selector (::)
- CSS: :not negation selector
- CSS: :has descendant selector