I'm using placeholders for text inputs which is working out just fine. But I'd like to use a placeholder for my selectboxes as well. Ofcourse I can just use this code:
<select>
<option value="">Select your option</option>
<option value="hurr">Durr</option>
</select>
But the 'Select your option' is in black instead of lightgrey. So my solution could possibly be CSS-based. jQuery is fine too.
This only makes the option grey in the dropdown (so after clicking the arrow):
option:first {
color: #999;
}
Edit: The question is: how do people create placeholders in selectboxes? But it has already been answered, cheers.
And using this results in the selected value always being grey (even after selecting a real option):
select {
color:#999;
}
here is mine
I'm not content with HTML/CSS only solutions, so I've decided to create a custom
select
using JS.This is something I've written in the past 30 mins, thus it can be further improved.
All you have to do is create a simple list with few data attributes. The code automatically turns the list into a selectable dropdown. It also adds a hidden
input
to hold the selected value, so it can be used in a form.Input:
Output:
The text of the item that's supposed to be a placeholder is grayed out. The placeholder is selectable, in case the user wants to revert his/her choice. Also using CSS, all the drawbacks of
select
can be overcome (e.g., inability of the styling of the options).Update: I've improved this (selection using up/down/enter keys). Tidied up the output a little bit and turned this into a object. Current output:
Initialization:
For further examination: JSFiddle,
GitHub(renamed).Update: Rewritten this again. Instead of using a list, we can just use a select. This way it'll work even without JS (in case it's disabled).
Input:
Output:
JSFiddle, GitHub.
Another possibility in JS:
JSFiddle
Input
[type="text"]
Style Placeholder for Select ElementsThe following solution simulates a placeholder as it relates to an
input[type="text"]
element:Just stumbled across this question, here's what works in FireFox & Chrome (at least)
The Disabled option stops the
<option>
being selected with both mouse and keyboard, whereas just using'display:none'
allows the user to still select via the keyboard arrows. The'display:none'
style just makes the list box look 'nice'.Note: Using an empty
value
attribute on the "placeholder" option allows validation (required attribute) to work around having the "placeholder", so if the option isn't changed but is required; the browser should prompt the user to choose an option from the list.Update (July 2015):
This method is confirmed working in the following browsers:
Update (October 2015):
Removed the
style="display: none"
in favour of HTML5 attributehidden
which has wide support. Thehidden
element has similar traits asdisplay: none
in Safari, IE, (Project Spartan needs checking) where theoption
is visible in dropdown but is not selectable.Update (January 2016):
When the
select
element isrequired
it allows use of the:invalid
CSS pseudo-class which allows you to style theselect
element when in it's "placeholder" state.:invalid
works here because of the empty value in the placeholderoption
.Once a value has been set the
:invalid
pseudo-class will be dropped. You can optionally also use:valid
if you so wish.Most browsers support this pseudo-class. IE10+. This works best with custom styled
select
elements; In some cases i.e. ( Mac in Chrome / Safari) you'll need to change the default appearance of theselect
box so that certain styles display i.e.background-color
,color
. You can find some examples and more about compatibility at developer.mozilla.org.Native element appearance Mac in Chrome:
Using altered border element Mac in Chrome: