Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify Select So Only The First One Is Gray

Tags:

html

css

I have a select element and am using the first option as the title of the select field. I am wondering if there is a way to gray out the text inside the select field when the first option is selected. Can this only be done in JS, or is there a CSS solution?

I have tried changing the style of the first option but that only changes the colour of the text when I activate the dropdown menu.

<select>   <option>Please select your favourite fruit</option>   <option>Apple</option>   <option>Banana</option> </select> 
like image 619
Jon Avatar asked Dec 04 '12 00:12

Jon


People also ask

How do I change the color of a selection dropdown?

To change the selected option background-color CSS style, we can set the style attribute of the select and option elements. to set the whole select element to background color 009966 and color FFF . Then we override the the styles in the option elements with style="background: white; color: black;" .

How do I style a selection dropdown in CSS?

There are many ways to design a <select> dropdown menu using CSS. The Dropdown Menu is mainly used to select an element from the list of elements. Each menu option can be defined by an <option> element that can nested inside the <select> element.


1 Answers

Here is a more modern solution so it's not specific to the first option, but rather an invalid option and requires no JS to show only the title/placeholder option as grey whereas the rest appear normal.

select,  select option {    color: #000000;  }    select:invalid,  select option[value=""] {    color: #999999;  }    label {    display: block;    margin: 16px 0;  }    /*Added for browser compatibility*/  [hidden] {    display: none;  }
<label>      Invalid option cannot be selected and is hidden from the user in the dropdown.      <select required>        <option value="" selected disabled hidden>Please select your favourite fruit</option>        <option>Apple</option>        <option>Banana</option>      </select>  </label>    <label>      Invalid option cannot be selected, but is not hidden from the user in the dropdown.      <select required>        <option value="" selected disabled>Please select your favourite fruit</option>        <option>Apple</option>        <option>Banana</option>      </select>  </label>    <label>      Invalid option can be selected and is not hidden from the user in the dropdown.      <select required>        <option value="" selected>Please select your favourite fruit</option>        <option>Apple</option>        <option>Banana</option>      </select>  </label>

The :invalid selector on the select only works on an option if the select box is required, and the selected option's value is empty, so you can style it as you would a text box's placeholder text.

Setting it to disabled prevents the user from selecting it in the select options, and setting it to hidden hides it from the select options.

Here is my CodePen demo that explores additional select box styles and shows this one in action on a light background.

like image 161
Tessa Avatar answered Sep 16 '22 12:09

Tessa