简体   繁体   English

如何使用CSS将选择菜单中的选项居中

[英]How to center an option in a select menu using CSS

I'd like for all the options in a select menu to be centered, however I can't find a way of doing it. 我希望选择菜单中的所有选项都居中,但是我找不到解决方法。

After a quick google I couldn't find any real solutions, however most of the entries are quite old, so I'm wondering if there have been any improvements made to css to make this possible. 经过一番快速的搜索后,我找不到任何真正的解决方案,但是大多数条目都已经很老了,所以我想知道是否对css进行了任何改进以使其成为可能。

The closest I've found is to use text indentation, which may well work for the options that are the same length, but for any options of varying lengths it of course looks off. 我发现最接近的方法是使用文本缩进,这对于长度相同的选项可能很好,但是对于长度不同的任何选项,它当然看起来都是如此。

You might be interested in using ul and li instead of a traditional select and option . 您可能对使用ulli而不是传统的selectoption感兴趣。

Bootstrap has a component that lets you do this: Single button dropdowns . Bootstrap具有一个使您可以执行此操作的组件: 单按钮下拉菜单

You can use their component and add a style similar to this: 您可以使用它们的组件并添加类似于以下的样式:

ul.btn-dropdown li {
  text-align: center;
}

You can get a result like this: 您可以得到如下结果:

在此处输入图片说明

HTML/CSS doesn't provide for formatting Option elements within Select lists - only for formatting the whole Select list. HTML / CSS不提供格式化“选择”列表中的Option元素的功能,仅提供格式化整个“选择”列表的格式。

You could try padding with spaces but, realistically, I'd concentrate on more important issues and let Select lists be Select lists. 您可以尝试使用空格进行填充,但实际上,我将专注于更重要的问题,并将“选择列表”作为“选择列表”。

(JQuery may have something to offer, but I do straight Javascript so am not familiar enough to know). (JQuery可能提供一些功能,但是我会使用纯Java脚本,因此并不熟悉)。

Tony Duffill 托尼·达菲尔

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

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