Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to style a select tag's option element?

I'm trying to set the style of an option in a select dropdown menu in Google Chrome. It works in all browsers except IE9 and Chrome.

option.red {      background-color: #cc0000;       font-weight: bold;       font-size: 12px;       color: white;  }
<select name="color">      <option class="red" value="red">Red</option>      <option value="white">White</option>      <option value="blue">Blue</option>      <option value="green">Green</option>  </select>

Without using JavaScript, is there a way to set style to the options in Google Chrome? Once selected the background color does not display.

like image 544
Mike Avatar asked May 04 '11 17:05

Mike


People also ask

How do you style options in select element?

You can use inline styles to add custome styling to <option> tags. For eg : <option style="font-weight:bold;color:#09C;">Option 1</option> This will apply the styles to this particular <option> element only. You can also use <option value="" disabled> <br> </option> to add a line-break between the options.

How do I style a selected element in CSS?

<select> tags can be styled through CSS just like any other HTML element on an HTML page rendered in a browser. Below is an (overly simple) example that will position a select element on the page and render the text of the options in blue.

How do you style a dropdown?

Example Explained HTML) Use any element to open the dropdown content, e.g. a <span>, or a <button> element. Use a container element (like <div>) to create the dropdown content and add whatever you want inside of it. Wrap a <div> element around the elements to position the dropdown content correctly with CSS.


1 Answers

Unfortunately, WebKit browsers do not support styling of <option> tags yet, except for color and background-color.

The most widely used cross browser solution is to use <ul> / <li> and style them using CSS. Frameworks like Bootstrap do this well.

like image 112
Tushar Roy Avatar answered Sep 19 '22 11:09

Tushar Roy