Here you go:
<select
size="1"
name="lbxRange"
id="lbxRange"
tabindex="110"
title="Click to select the range"
class="listbox"
onfocus="this.size=10;"
onblur="this.size=0;"
onchange="this.size=1; this.blur();">
<option selected="selected" value="--None--">--None--</option>
<option value="My Value 1">My Value 1</option>
<option value="My Value 2">My Value 2</option>
</select>
and the css:
.listbox {
width: 80%;
min-width: 220px;
border-radius: 4px;
background-color: red;
color: black;
}
option:hover {
background-color: yellow;
}
option[selected="selected"] {
background-color: green;
}
Notice that I use a css selector for the selected item. Here is more on scc selectors:
CSS Selectors Reference[
^]
UPDATE: The answer is staring at you...
To change Hover color of selected color, combine the css selector with the
:hover
state:
option[selected="selected"]:hover {
background-color: purple;
}
UPDATE #2 It turns out there is a small trick to the hover on the checked option:
option:checked:hover {
box-shadow: 0 0 10px 100px pink inset;
}
You can see the final solution working here:
Select Option Highlighting on Hover - CodePen.io[
^]
UPDATE #3 This is an Asp.Net WebForms project.
Here is a working example for .Net Framework 4.7...
1.
Default.aspx file
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebformListSelectColor._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<main>
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
</main>
</asp:Content>
2.
Default.aspx.cs file
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
ListBox1.DataSource = new List<Person>
{
new Person(1, "Fred"),
new Person(2, "Paul"),
new Person(3, "Ringo"),
};
ListBox1.DataTextField = nameof(Person.Name);
ListBox1.DataValueField = nameof(Person.Id);
ListBox1.DataBind();
ListBox1.SelectedIndex = 0;
ListBox1.CssClass = "listbox";
}
}
public class Person
{
public Person(int id, string name)
{
Id = id;
Name = name;
}
public int Id { get; set; }
public string Name { get; set; }
}
3.
Site.css file
.body-content {
margin-top: 15px;
padding-left: 15px;
padding-right: 15px;
}
input,
select,
textarea {
max-width: 280px;
}
.listbox {
width: 80%;
min-width: 220px;
border-radius: 4px;
background-color: red;
color: black;
}
option:hover {
background-color: yellow;
}
option[selected="selected"] {
background-color: green;
}
option[selected="selected"]:hover {
background-color: purple;
}
option:checked:hover {
box-shadow: 0 0 10px 100px pink inset;
}
@media screen and (min-width: 768px) {
.body-content {
padding: 0;
}
}
Create a new Asp.Net > WebForms project and use the above code. This will show a Listbox, not a dropdown, and will highlight as per your requirement. No CSS was modified from my previous answers or the CodePen link above.
If you get different results, then you need to check that you followed the instructions to the letter.