简体   繁体   English

如何将文本置于选择列表中心

[英]How to center text in a select list

I'm having trouble styling a select list in an html form. 我无法在html表单中设置选择列表的样式。 Basically I want it to have a font size of 12px, and a height of 30px; 基本上我希望它的字体大小为12px,高度为30px; The problem is that the box is drawn to the right hight, but the text rendering varies in each browser. 问题是该框被绘制到正确的高度,但文本呈现在每个浏览器中都有所不同。 In FireFox it renders it in the top left corner. 在FireFox中,它会在左上角呈现它。 I want to vertically center the text. 我想垂直居中文本。 Anyone know of a way to do this or do I need to make something custom out of a ul? 任何人都知道这样做的方法,还是我需要用ul定制一些东西?

Thanks 谢谢

The only way is to drop the <select> and use an <input type="text"< and a <div> to give you the exact feel of a select list but with the ability to do everything as it will be a <li> that you have at the end. 唯一的方法是删除<select>并使用<input type="text"<<div>来给你一个选择列表的确切感觉但是能够做所有事情,因为它将是<li>你最后有。

I use this approach not to show aligned text but to use a small icon per each line . 我使用这种方法不显示对齐的文本,而是每行使用一个小图标 This also gives me the ability to show exactly how I want no mater what browser is used as well. 这也使我能够准确地显示我想要的是什么,也不需要使用什么浏览器。

like the image below 如下图所示

在此输入图像描述

You cannot reliably customize <select> elements using CSS in all browsers. 您无法在所有浏览器中使用CSS可靠地自定义<select>元素。

You need to make a custom list control. 您需要进行自定义列表控件。

Good luck with this, text-align works only with block elements. 祝你好运,text-align仅适用于块元素。 The select list is formatted by browsers differently, as it is with most of the form field elements (like the file input type). 选择列表由浏览器格式化,与大多数表单字段元素(如文件输入类型)一样。 Wish for better days. 希望有更好的日子。 The workarounds that I have seen for this are really ugly. 我见过的解决方法真的很难看。

Bit late to add a new answer, however I came across your question looking for the same answer and thought there must be another way. 添加一个新的答案有点迟了,但我遇到了你的问题寻找相同的答案,并认为必须有另一种方式。

Hurray! 欢呼! I actually found one! 我其实找到了一个!

So for any new people coming to find this answer, here is how I have achieved this: 所以对于任何想要找到这个答案的新人来说,我的成就如下:

Simply use &nbsp; 只需使用&nbsp; to manually center each option in your drop down list. 手动居中下拉列表中的每个选项。

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

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