Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change color of selection in select option?

I'm trying to change color of selection in select option. I was trying to do this:

option::selection {background: #ccc;}
option::-moz-selection {background: #ccc;}
option::-webkit-selection {background: #ccc; color:#fff;}

But it doesn't work. This works only for simple text, not to option. Is there any way to change color of selection? I dont need to change background of selected option. I need to change color of selection.

like image 338
Peter Avatar asked Jul 17 '13 11:07

Peter


People also ask

How do I change the color of a selection in HTML?

The colour of selected text can be easily changed by using the CSS | ::selection Selector. In the below code, we have used CSS ::selection on <h1> and <p> element and set its colour as yellow with green background.

How do you change the color of a selection in CSS?

You can change the background color and color of selected text by styling ::selection . Styling this pseudo element is great for matching user-selected text to your sites color scheme.

How do I change the color of my select box arrows?

Attach Inspector, press Ctrl+Shift+LMB on that arrow and you'll see what you should change.


2 Answers

try this one maybe it will be helpful for you.

select{
    margin:40px;
    background: yellow;
    color:#000;
    text-shadow:0 1px 0 rgba(0,0,0,0.4);
}
option:not(:checked) { 
    background-color: #FFF; 
}

HTML

<select>
    <option val="">Select Option</option>
    <option val="1">Option 1</option>
    <option val="2">Option 2</option>
    <option val="3">Option 3</option>
    <option val="4">Option 4</option>
</select>

Demo JsFiddle: https://jsfiddle.net/hamzanisar/yfg8vdme/

like image 174
Muhammad Hamza Nisar Avatar answered Oct 18 '22 07:10

Muhammad Hamza Nisar


Hope this is what you expected.

function ChangedSelection()
{
var x = document.getElementById("mySelect").selectedIndex;
var color =document.getElementsByTagName("option")[x].value;
var y = document.getElementById("mySelect");
y.style.color=color;
}
<select id="mySelect" onchange="ChangedSelection()" style="Color:red;">
    <option value="red" selected="selected">Red</option>
    <option value="blue">Blue</option>
    <option value="Green">Green</option>
    <option value="Yellow">Yellow</option>
</select>
like image 26
MANOJ GOPI Avatar answered Oct 18 '22 09:10

MANOJ GOPI