Change Select List Option background colour on hov

2019-01-01 09:26发布

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

HTML:

<select id="select">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
</select>

I have tried option:hover { background-color: red; }, but it is of no use. Does anybody know how to do this?

7条回答
旧时光的记忆
2楼-- · 2019-01-01 09:40

Implementing an inset box shadow CSS works on Firefox:

select option:checked,
select option:hover {
    box-shadow: 0 0 10px 100px #000 inset;
}

Checked option item works in Chrome:

select:focus > option:checked { 
    background: #000 !important;
}
查看更多
倾城一夜雪
3楼-- · 2019-01-01 09:41

In FF also CSS filter works fine. E.g. hue-rotate:

option {
    filter: hue-rotate(90deg);
}

https://jsfiddle.net/w3a740jq/4/

查看更多
初与友歌
4楼-- · 2019-01-01 09:41

this is what you need, the child combinator:

    select>option:hover
    {
        color: #1B517E;
        cursor: pointer;
    }

Try it, works perfect.

Here's the reference: http://www.w3schools.com/css/css_combinators.asp

查看更多
有味是清欢
5楼-- · 2019-01-01 09:49

This can be done by implementing an inset box shadow. eg:

select.decorated option:hover {
    box-shadow: 0 0 10px 100px #1882A8 inset;
}

Here, .decorated is a class assigned to the select box.

Hope you got the point.

查看更多
只靠听说
6楼-- · 2019-01-01 09:49

I realise this is an older question, but I recently came across this need and came up with the following solution using jQuery and CSS:

jQuery('select[name*="lstDestinations"] option').hover(
        function() {
            jQuery(this).addClass('highlight');
        }, function() {
            jQuery(this).removeClass('highlight');
        }
    );

and the css:

.highlight {
    background-color:#333;
    cursor:pointer;
}

Perhaps this helps someone else.

查看更多
忆尘夕之涩
7楼-- · 2019-01-01 09:52

Select / Option elements are rendered by the OS, not HTML. You cannot change the style for these elements.

查看更多
登录 后发表回答