简体   繁体   English

HTML中的样式下拉元素

[英]Styling dropdown element in HTML

Hi I want to change the style of the dropdown, nearer the option tags, in HTML. 嗨,我想在HTML中更改下拉列表的样式,更接近选项标签。 In Firefox it is working, but not properly in IE and google chrome. 在Firefox中它可以正常工作,但在IE和谷歌浏览器中并不合适。

The padding is only working in firefox. 填充只适用于firefox。 The background color is working on all browsers, but in IE you can see it, even on the selected value. 背景颜色适用于所有浏览器,但在IE中您可以看到它,即使在所选值上也是如此。

Demo with JSFiddle 用JSFiddle演示

Html: HTML:

<label for="locale">locale:<em>*</em></label>
<select name="locale" id="locale">
    <option selected="selected">en_CA</option>
    <option>en_US</option>
    <option>fr_FR</option>
    <option>fr_CA</option>
    <option>ja_JP</option>
</select><br />

CSS: CSS:

label{
    margin: 5px 0px 10px 0px;
    padding: 5px;
    height: 22px;
    display: inline-block;
    width: 100px;
}

label em{
    color: red;
    font-family: Arial;
    font-weight: bold;
}

select{
    color: #333;
    margin: 5px 0px 10px -5px;
    padding: 5px;
    height: 32px;
    width: 262px;
    border: #999 1px solid;
}

select option {
    padding: 5px 8px;
    background: #ddd;
}

Webkit browsers (Safari, Chrome etc) don't allow padding on select elements. Webkit浏览器(Safari,Chrome等)不允许对select元素进行填充。 You can however mimic padding by manipulating the height for top and bottom padding and text-indent for left-padding. 但是,您可以通过操纵顶部和底部填充的高度以及左边填充的文本缩进来模拟填充。

Update: The same goes for background-color on option elements. 更新: option元素的背景颜色也是如此。 Webkit doesn't allow that and I don't believe there's a workaround other than doing your own Javascript implementation of a drop-down using for example an unordered list and some styling. Webkit不允许这样做,我不相信除了使用例如无序列表和一些样式的下拉列表执行自己的Javascript实现之外还有其他解决方法。

I don't think you could just style the option tags. 我不认为你可以设置选项标签的样式。 I think I read it from somewhere that it is based on OS or something... 我想我是从某个地方读到的,它基于操作系统或其他东西......

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

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