Html 有没有办法在 HTML5 数据列表选项上应用 CSS 样式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13693482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 04:12:02  来源:igfitidea点击:

Is there a way to apply a CSS style on HTML5 datalist options?

csshtmloptionhtml-datalist

提问by n0n0bstan

I would like to modify the way that the list of the different options of my datalist are displayed. Is it possible to apply on it some CSS properties ?

我想修改数据列表不同选项列表的显示方式。是否可以在其上应用一些 CSS 属性?

<input list="languages" id="language_id">
<datalist id="languages">
      <option value="html">HTML</option>
      <option value="java">Java</option>
      <option value="perl">Perl</option>
      <option value="php">PHP</option>
      <option value="ruby-on-rails">Ruby on Rails</option>
</datalist>

I tried

我试过

option {
    background: red;
}

but it does not seem to work.

但它似乎不起作用。

回答by adrift

Like selectelements, the datalistelement has very little flexibility in styling. You cannot style any of the suggested terms if that's what your question was asking.

select元素一样,datalist元素在样式上的灵活性很小。如果这就是您的问题,则您无法为任何建议的术语设置样式。

Browsers define their own styles for these elements.

浏览器为这些元素定义自己的样式。

回答by jarrodean

try:

尝试:

input[list]
{
  background: red;
}