如何设置 html“选择"选项的样式元素? [英] How to style the option of an html "select" element?

查看:35
本文介绍了如何设置 html“选择"选项的样式元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 HTML:

<select id="ddlProducts" name="ddProducts"> 
    <option>Product1 : Electronics </option>
    <option>Product2 : Sports </option>
</select>

我想仅使用 CSS 将产品名称(即 'Product1'、'Product2' 等)设为粗体,并将其类别(即电子、体育等)设为斜体.我发现了一个老问题,其中提到不可能使用 HTML 和 CSS,但希望现在有解决方案.

I want to make the name of the product (i.e. 'Product1', 'Product2' , etc) bold, and its categories(viz. Electronics, Sports, etc) italicized, using CSS only. I found an old question that mentioned it's not possible using HTML and CSS, but hopefully, there's a solution now.

推荐答案

只有少数样式属性可以应用于 元素.

There are only a few style attributes that can be applied to an <option> element.

这是因为这种类型的元素是替换元素"的一个例子.它们依赖于操作系统并且不是 HTML/浏览器的一部分.不能通过 CSS 设置样式.

This is because this type of element is an example of a "replaced element". They are OS-dependent and are not part of the HTML/browser. It cannot be styled via CSS.

有些替代插件/库看起来像

发送“验证码”获取 | 15天全站免登陆