简体   繁体   English

如何在html下拉列表中添加填充?

[英]How can I add padding to html dropdownlist?

I want to add padding to the options of html "select" tag. 我想在html“select”标签的选项中添加填充。

I try to add it in the style property of "select" and "option" and it doesn't work in IE. 我尝试在“select”和“option”的样式属性中添加它,它在IE中不起作用。

What can I do? 我能做什么?

好吧,我讨厌整个20世纪90年代的声音,但只是预先和应用结束选项文本的空间是否可以接受你想要的目标?

It is difficult to style a drop down box cross browser. 跨浏览器的下拉框很难设置样式。 To get any sort of control, you'll need to code a replacement one in JavaScript. 要获得任何控制,您需要使用JavaScript编写替换控件。 Be warned, however: 但请注意:

  • Remember that the user may have difficulty using your drop down - take usabilty into account 请记住,用户可能难以使用您的下拉菜单 - 考虑到可用性
  • Replace a select element with JS - this is an accessibility issue (and also allows users without JS support to use the form input) 用JS替换select元素 - 这是一个可访问性问题(并且还允许没有JS支持的用户使用表单输入)

The following works, in FF3+ and Midori (and presumably other Webkit browsers): 以下工作,在FF3 +和Midori(以及可能是其他Webkit浏览器):

select
        {width: 14em;
        margin: 0 1em;
        text-indent: 1em;
        line-height: 2em;}

select *    {width: 14em;
        padding: 0 1em;
        text-indent: 0;
        line-height: 2em;}

The width is to allow enough room in the displayed select box when not active, the margin does what it always does, text-indent is used to feign padding between the left boundary of the select box and the inner-text. 宽度是为了在显示的select框中没有活动时允许足够的空间,边距执行它总是做的事情, text-indent用于在选择框的左边界和内部文本之间填充填充。 line-height is just to allow vertical spacing. line-height只是为了允许垂直间距。

I can't comment on its use in IE, I'm afraid, so if anyone could feed back -or adapt- to suit that'd be good. 我不能评论它在IE中的使用,我担心,所以如果有人可以反馈 - 或者适应 - 那就是好的。

It's worth noting that the drop-down part of the select ( select * ) isn't affected outside of FF3 for me, for whatever reason, so if that was the question you wanted answering, I apologise for offering nothing new. 值得注意的是,无论出于何种原因,select( select * )的下拉部分对我来说都不会受到影响,所以如果这是你想要回答的问题,我很抱歉没有提供任何新内容。

Demo at: 演示: http://davidrhysthomas.co.uk/so/select-styling.html http://davidrhysthomas.co.uk/so/select-styling.html

Create a class for the SELECT 为SELECT创建一个类

.listBox{
   width: 200px; 
   ...etc
}

Now define the Css for the options of the SELECT 现在为SELECT的选项定义Css

 .listBox option{
     padding:4px;
    ...etc
  }

Tested on IE 10 在IE 10上测试过

CSS: CSS:

option{
 padding: 0.5em;
}

I couldnt get this to work with padding or spacing for some reason. 由于某种原因,我无法使用填充或间距。 I went with a jQuery solution that looks like this: 我选择了一个看起来像这样的jQuery解决方案:

$(document).click(function(){  
  $('#selector option').each(function(){  
    $(this).html("  "+$(this).text());  
  });  
});

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

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