Ralph Shillington
Ralph Shillington

Reputation: 21098

Is it possible to select an an element that immediately precedes another element using the adjacent selector

I would like to select <label> elements that immediately precede <input type="checkbox">

from the CSS guide

Adjacent sibling selectors have the following syntax: E1 + E2, where E2 is the subject of the selector.

But, unless I'm misunderstanding the spec I need a select where E1 is the subject not E2. I'm sure this must be available, what am I missing?

Upvotes: 1

Views: 517

Answers (2)

Michael Kessler
Michael Kessler

Reputation: 14235

I've never used it, but I understand from the link that you've provided that it should be done exactly like follows:

input[type='checkbox'] + label { color: Red }

This should work for the next HTML:

<input type='checkbox' id='chckTest' /><label>Test checkbox</label>

I don't have the possibility to check it now but I believe that the "Test checkbox" text will be red...

Upvotes: -1

prodigitalson
prodigitalson

Reputation: 60413

Actually no, youre not missing anything (unless i have been for quite some time now as well) - you cant select a previous sibling with css2 selectors.

Upvotes: 2

Related Questions