Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML <select> selected option background-color CSS style

Is there a style for a select option's "selected" color? For example:

<HTML> <BODY> <FORM NAME="form1"> <SELECT NAME="mySelect" SIZE="7" style="background-color:red;"> <OPTION>Test 1 <OPTION>Test 2 <OPTION>Test 3 <OPTION>Test 4 <OPTION>Test 5 <OPTION>Test 6 <OPTION>Test 7 </SELECT> </FORM> </BODY> </HTML> 

When I select an option it turns blue, I want to override this and make it a different color. In the style I expected something like "selected-color", but it doesn't exist.

like image 243
arieltools Avatar asked Mar 08 '10 14:03

arieltools


People also ask

How do I change the background color of a selection using CSS?

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 you style select options in CSS?

You can style the option elements to some extent. Using the * CSS selector you can style the options inside the box that is drawn by the system.

How can we select background color in HTML?

To set the background color in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <body> tag, with the CSS property background-color. HTML5 do not support the <body> tag bgcolor attribute, so the CSS style is used to add background color.

Is it possible to change the default background color of a select list option on hover?

You cannot change the style for these elements.


1 Answers

You may not be able to do this using pure CSS. But, a little javascript can do it nicely.

A crude way to do it -

var sel = document.getElementById('select_id'); sel.addEventListener('click', function(el){     var options = this.children;     for(var i=0; i < this.childElementCount; i++){         options[i].style.color = 'white';     }     var selected = this.children[this.selectedIndex];         selected.style.color = 'red';     }, false); 
like image 159
N 1.1 Avatar answered Sep 30 '22 17:09

N 1.1