简体   繁体   English

如何更改 hover 上 select 选项的背景颜色

[英]How to change the background color of a select option on hover

I want to change the background color of the select options on hover and i have tried the followed rules but it didn't worked either:我想更改 hover 上 select 选项的背景颜色,我尝试了以下规则,但它也没有奏效:

 select option {
    color: #fff;
  }
  select option:hover {
    color: #000;
    box-shadow: inset 20px 20px #fff;
  }

What rules i have to apply to achieve that effect?我必须应用哪些规则才能达到这种效果? Does JS recognize a hover in a option element? JS 是否识别选项元素中的 hover?

you can try something like this:你可以尝试这样的事情:

 #select { width: 100px; } select:hover { color: #444645; background: green; /* hover on select */ } select:focus>option:checked { background: yellow; /* selected */ }
 <select id="select"> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> </select>

If you need a cheat code for the options list then you can try this:如果您需要选项列表的作弊码,那么您可以试试这个:

 #select { width: 100px; } select:hover { color: #444645; background: green; /* hover on select */ } select option { color: #444645; background: red; /* hover on select */ } option:hover { /*optional rendered */ background-color: teal; }
 <select onfocus='this.size=9;' onblur='this.size=0;' onchange='this.size=1; this.blur();' id="select"> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> <option value="4">Four</option> <option value="5">Five</option> <option value="6">Six</option> <option value="7">Seven</option> <option value="8">Eight</option> <option value="9">Nine</option> </select>

Unfortunately, you can't just change default background color and text color of a selected option by just css.不幸的是,您不能仅通过 css 更改所选选项的默认背景颜色和文本颜色。 They're maganed by your browser, and some of them don't want to keep your css style.它们被您的浏览器放大,其中一些不想保留您的 css 风格。

That's why you always see that default ugly blue background and always white text.这就是为什么您总是看到默认丑陋的蓝色背景和总是白色的文本。

BUT I have a hack here:但我在这里有一个技巧:

For changing the backround color you can use linear-gradient with same color like this:要更改背景颜色,您可以使用具有相同颜色的线性渐变,如下所示:

option:hover,
option:checked,
option:active,
option:focus {
    background: linear-gradient(#ffffd4, #ffffd4);
    position: relative;    
}

For changing text color you can use pseudo-class with content which is equal to your option text, like this:要更改文本颜色,您可以使用内容等于您的选项文本的伪类,如下所示:

select:focus > option:checked:before {
    color: #101010;
    position: absolute;
    content: attr(data-content);
}

data-content can be added directly in html or via javascript/jQuery.数据内容可以直接添加到 html 或通过 javascript/jQuery。

<option data-content="One">One</option>

You need position absolute to hide your option text under the pseudo class.您需要 position absolute 来隐藏伪 class 下的选项文本。

 option:hover, option:checked, option:active, option:focus { background: linear-gradient(#ffffd4, #ffffd4); position: relative; } select:focus > option:checked:before { color: #A52A2A; position: absolute; content: attr(data-content); } select { width: 200px; } option { padding: 5px 10px; } option:hover { cursor: pointer; }
 <select size=5> <option data-content="One">One</option> <option data-content="Two">Two</option> <option data-content="Three">Three</option> <option data-content="Four">Four</option> <option data-content="Five">Five</option> </select>

Use background or background-color to change the background color of an element.使用backgroundbackground-color来改变元素的背景颜色。 color is for text color, not background color. color用于文本颜色,而不是背景颜色。

 div { background: #ffffff; /* equivalent to background-color */ } div:hover { background: #000; color: #fff; }
 <div>Hover!</div>

The element is notoriously difficult to style productively with CSS.众所周知,该元素很难使用 CSS 高效地设置样式。 You can affect certain aspects like any element.您可以像任何元素一样影响某些方面。 (Source: MDN ). (来源: MDN )。 To produce a consistent appearance across all browsers, I suggest that you use libraries like Select2 , which appends an additional div below the dropdown, essentially covering the original dropdown.为了在所有浏览器中产生一致的外观,我建议您使用像Select2这样的库,它在下拉列表下方附加了一个额外的 div,基本上覆盖了原始下拉列表。 This way, it allows a much more flexible styling since you're essentially styling the newly appended div.这样,它允许更灵活的样式,因为您实际上是在为新附加的 div 设置样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM