Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Selector for selecting an element that comes BEFORE another element? [duplicate]

I'm working on a login page for my website and I want to bold the label that comes before the input box when the text box gains focus. Right now I have the following markup:

<label for="username">Username:</label> <input type="textbox" id="username" />  <label for="password">Password:</label> <input type="textbox" id="password" /> 

I can use the adjacent selector to select the label after the text box, but I can't select the element before it. I can use this CSS selector rule, but it only selects the label after, so when the username text box gains focus, the password label becomes bold.

input:focus + label {font-weight: bold} 

Is there anything I can do to make this work? I know JavaScript could be used to easily do this, but I'd like to use a purely CSS-based solution if possible, I just can't figure out a way to do it.

like image 442
Dan Herbert Avatar asked Jan 29 '09 18:01

Dan Herbert


People also ask

How do you style every element which has an adjacent item right before it?

You can easily style every element which has an adjacent item right before it using the Adjacent Sibling Selector (+). The adjacent sibling selector is used to select the element that is adjacent or the element that is next to the specified selector tag.

How do I get an element before an element in CSS?

The CSS ::before selector can be used to insert content before the content of the selected element or elements. It is used by attaching ::before to the element it is to be used on. In the example above we are prepending an asterisk and a space before every paragraph element on the page.

How do I select a previous sibling in CSS?

No, there is no "previous sibling" selector. On a related note, ~ is for general successor sibling (meaning the element comes after this one, but not necessarily immediately after) and is a CSS3 selector. + is for next sibling and is CSS2. 1.

What is a sibling selector in CSS?

The adjacent sibling selector is used to select an element that is directly after another specific element. Sibling elements must have the same parent element, and "adjacent" means "immediately following".


1 Answers

The CSS Selectors 4 Spec provides a syntax for defining the "subject" of a selector by using a !. As of early-2016, this is not available in any browser. I'm including this because it will be the correct way to do this using pure CSS once browsers implement this syntax.

Given the markup in the question

<label for="username">Username:</label> <input type="textbox" id="username" />  <label for="password">Password:</label> <input type="textbox" id="password" /> 

This CSS would do the trick.

label:has(+ input:focus) {font-weight: bold} 

Of course, this assumes browsers actually implement the subject selector and that there are no bugs related to how this syntax works with the :focus pseudo-class.

like image 156
Dan Herbert Avatar answered Oct 25 '22 11:10

Dan Herbert