这是我的HTML:

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

我想让产品的名称(即。'Product1', 'Product2',等等),以及它的类别(即。电子,体育等)斜体,只使用CSS。我发现了一个老问题,提到这是不可能使用HTML和CSS,但希望,现在有一个解决方案。


当前回答

不,这是不可能的,因为这些元素的样式是由用户的操作系统处理的。MSDN将在这里回答你的问题:

除了background-color和color,通过style对象应用于option元素的样式设置将被忽略。

其他回答

您可以使用内联样式向<option>标记添加自定义样式。

<option style="font- family:宋体;“>选项1 > < /选项 这将只对这个<option>元素应用样式。

然后你可以使用一点javascript魔法,将内联样式应用到<select>标签中的所有<option>元素,如下所示:

var select = $(document).getElementById('#select-element-id')

Var选项= select.children('#option-element-id')

option.css(“粗细”、“大胆”)

option.css(“字体大小”、“24 px”)

您也可以使用<option value="" disabled> <br> </option>在选项之间添加换行符。

即使没有太多的属性要改变,但你可以实现以下样式只有css:

.options { 边框:1px solid #e5e5e5; 填充:10 px; } 选择{ 字体大小:14 px; 边界:没有; 宽度:100%; 背景:白色; } < div class = "选项”> <选择> 苹果<选项值= " " > < /选项> 香蕉<选项值= " " > < /选项> 橙色<选项值= " " > < /选项> 芒果<选项值= " " > < /选项> < /选择> < / div >

一些属性可以为<option>标签设置样式:

字体类型 颜色 字体- * 背景颜色

此外,您可以使用自定义字体的个人<选项>标签,例如任何谷歌字体,材质图标或其他图标字体从icomoon或类似。(这可能会方便字体选择器等)

考虑到这一点,您可以创建字体家族堆栈,并在<option>标记中插入图标,例如。

    <select>
        <option style="font-family: 'Icons', 'Roboto', sans-serif;">a    ★★★</option>
        <option style="font-family: 'Icons', 'Roboto', sans-serif;">b    ★★★★</option>
    </select>

★取自Icons,其余取自Roboto。

注意,自定义字体并不适用于移动选择。

这就是你要找的东西吗?我用jQuery做的!

运行代码片段

$(".custom-select").each(function() { var classes = $(this).attr("class"), id = $(this).attr("id"), name = $(this).attr("name"); var template = '<div class="' + classes + '">'; template += '<span class="custom-select-trigger">' + $(this).attr("placeholder") + '</span>'; template += '<div class="custom-options">'; $(this).find("option").each(function() { template += '<span class="custom-option ' + $(this).attr("class") + '" data-value="' + $(this).attr("value") + '">' + $(this).html() + '</span>'; }); template += '</div></div>'; $(this).wrap('<div class="custom-select-wrapper"></div>'); $(this).hide(); $(this).after(template); }); $(".custom-option:first-of-type").hover(function() { $(this).parents(".custom-options").addClass("option-hover"); }, function() { $(this).parents(".custom-options").removeClass("option-hover"); }); $(".custom-select-trigger").on("click", function() { $('html').one('click',function() { $(".custom-select").removeClass("opened"); }); $(this).parents(".custom-select").toggleClass("opened"); event.stopPropagation(); }); $(".custom-option").on("click", function() { $(this).parents(".custom-select-wrapper").find("select").val($(this).data("value")); $(this).parents(".custom-options").find(".custom-option").removeClass("selection"); $(this).addClass("selection"); $(this).parents(".custom-select").removeClass("opened"); $(this).parents(".custom-select").find(".custom-select-trigger").text($(this).text()); }); body { font-family: 'Roboto', sans-serif; } .custom-select-wrapper { position: relative; display: inline-block; user-select: none; } .custom-select-wrapper select { display: none; } .custom-select { position: relative; display: inline-block; } .custom-select-trigger { position: relative; display: block; width: 170px; padding: 0 84px 0 22px; font-size: 19px; font-weight: 300; color: #5f5f5f; line-height: 50px; background: #EAEAEA; border-radius: 4px; cursor: pointer; margin-left:20px; border: 1px solid #5f5f5f; transition: all 0.3s; } .custom-select-trigger:hover { background-color: #d9d9d9; transition: all 0.3s; } .custom-select-trigger:after { position: absolute; display: block; content: ''; width: 10px; height: 10px; top: 50%; right: 25px; margin-top: -3px; border-bottom: 1px solid #5f5f5f; border-right: 1px solid #5f5f5f; transform: rotate(45deg) translateY(-50%); transition: all 0.4s ease-in-out; transform-origin: 50% 0; } .custom-select.opened .custom-select-trigger:after { margin-top: 3px; transform: rotate(-135deg) translateY(-50%); } .custom-options { position: absolute; display: block; top: 100%; left: 0; right: 0; margin: 15px 0; border: 1px solid #b5b5b5; border-radius: 4px; box-sizing: border-box; box-shadow: 0 2px 1px rgba(0,0,0,.07); background: #fff; transition: all 0.4s ease-in-out; margin-left: 20px; opacity: 0; visibility: hidden; pointer-events: none; transform: translateY(-15px); } .custom-select.opened .custom-options { opacity: 1; visibility: visible; pointer-events: all; transform: translateY(0); } .custom-options:before { position: absolute; display: block; content: ''; bottom: 100%; right: 25px; width: 7px; height: 7px; margin-bottom: -4px; border-top: 1px solid #b5b5b5; border-left: 1px solid #b5b5b5; background: #fff; transform: rotate(45deg); transition: all 0.4s ease-in-out; } .option-hover:before { background: #f9f9f9; } .custom-option { position: relative; display: block; padding: 0 22px; border-bottom: 1px solid #b5b5b5; font-size: 18px; font-weight: 600; color: #b5b5b5; line-height: 47px; cursor: pointer; transition: all 0.15s ease-in-out; } .custom-option:first-of-type { border-radius: 4px 4px 0 0; } .custom-option:last-of-type { border-bottom: 0; border-radius: 0 0 4px 4px; } .custom-option:hover, .custom-option.selection { background: #f2f0f0; } <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select name="sources" id="sources" class="custom-select sources" placeholder="My Categories"> <option value="categoryOne">Category 1</option> <option value="categoryTwo">Category 2</option> <option value="categoryThree">Category 3</option> <option value="categoryFour">Category 4</option> </select>

似乎我可以直接在Chrome或Firefox中为选择设置CSS。CSS和HTML代码提供如下:

. boldoption { font-weight: bold; 的 选择< > <option>一些规范-字体 <option>另一种规范-字体option</option> <option class=“boldoption”>一些勇敢的选择</option> 选择< - >