Page 1 of 1

Make a placeholder for select box

Posted: 12 Jan 2015, 08:35
by PloMar
I'm using placeholders for input fields which is working out just fine. But I'd like to use a placeholder for select options list as well.
Of course I can just use this code:

Code: Select all

<select name="sname">
  <option value="">Select your option</option>
  <option value="op1">Option 1</option>
  <option value="op2">Option 2</option>
</select>
But the "Select your option" can be selected, and is in black instead of lightgrey.
So, is there any HTML-CSS based solution to make a placeholder for select box.

Make a placeholder for select box

Posted: 12 Jan 2015, 08:57
by MarPlo
Add the "disabled" and "selected" attributes in the first <option> used as placeholder.
- disabled : stops the Option being selected with both mouse and keyboard.
- selected : makes the option as default selection.

Code: Select all

<select name="sname">
  <option value="" disabled selected>Select your option</option>
  <option value="op1">Option 1</option>
  <option value="op2">Option 2</option>
</select>
Demo: