我想垂直对齐选择框中的文本
我想垂直对齐选择框中的文本。我尝试使用
select{
verticle-align:middle;
}
但它在任何浏览器中都不起作用。 Chrome 似乎默认将选择框中的文本居中对齐。 FF 将其顶部对齐,IE 将其底部对齐。有什么办法可以实现这一点吗?我在 UIBinder 中使用 GWT 的 Select 小部件。
这就是我目前所拥有的:
select{
height: 28px !important;
border: 1px solid #ABADB3;
margin: 0;
padding: 0;
vertical-align: middle;
}
谢谢!
I want to vertically align the text in select box. I tried using
select{
verticle-align:middle;
}
however it does not work in any browsers. Chrome seems to align the text in select box to the center as a default. FF aligns it to the top and IE aligns it to the bottom. Is there any way to achieve this? I am using GWT's Select widget in UIBinder.
This is currently what I have:
select{
height: 28px !important;
border: 1px solid #ABADB3;
margin: 0;
padding: 0;
vertical-align: middle;
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
您最好的选择可能是调整顶部填充和顶部填充。跨浏览器进行比较。它并不完美,但我认为它已经是你能得到的最接近的了。
您需要的填充量取决于字体大小。
提示:如果您的字体设置为
px
,请也将填充设置为px
。如果您的字体设置为em
,请也将填充设置为em
。编辑
这些是我认为您拥有的选项,因为垂直对齐在浏览器中不一致。
A. 删除高度属性并让浏览器处理它。这通常对我来说效果最好。
B. 添加顶部填充以下推文本。 (在某些浏览器中按下箭头)
C. 您可以使字体更大,以尝试更好地匹配选择的高度。
Your best option will probably be to adjust the top padding & compare across browsers. It's not perfect, but I think it's as close as you can get.
The amount of padding you need will depend on the font size.
Tip: If your font is set in
px
, set padding inpx
as well. If your font is set inem
, set the padding inem
too.EDIT
These are the options I think you have, since vertical-align isn't consistant across the browsers.
A. Remove height attribute and let the browser handle it. This usually works the best for me.
B. Add top-padding to push down the text. (Pushed down the arrow in some browsers)
C. You can make the font larger to try and match the select height a little nicer.
我偶然发现了这组 css 属性,它们似乎在 Firefox 中垂直对齐大小选择元素中的文本:
不过,如果有的话,它在 IE8 中将文本向下推得更远。如果我将 box-sizing 属性设置回 border-box,至少不会使 IE8 变得更糟(并且 FF 仍然应用 -moz-box-sizing 属性)。如果能找到 IE 的解决方案就好了,但我并没有屏住呼吸。
编辑:取消这个。测试后不行。不过,对于任何感兴趣的人来说,问题似乎源于 FF 的 forms.css 文件中影响输入和选择元素的内置样式。相关属性是 line-height:normal !important。它不能被覆盖。我试过了。我发现如果我删除 Firebug 中的内置属性,我会得到一个具有合理垂直居中文本的选择元素。
I stumbled across this set of css properties which seem to vertically align the text in sized select elements in Firefox:
If anything, though, it pushes the text down even farther in IE8. If I set the box-sizing property back to border-box, it at least doesn't make IE8 any worse (and FF still applies the -moz-box-sizing property). It would be nice to find a solution for IE, but I'm not holding my breath.
Edit: Nix this. It doesn't work after testing. For anyone interested, though, the problem seems to stem from built-in styles in FF's forms.css file affecting input and select elements. The property in question is line-height:normal !important. It cannot be overridden. I've tried. I discovered that if I delete the built-in property in Firebug I get a select element with reasonably vertically-centered text.
我的解决方案是仅像这样为针对 Firefox 的选择添加 padding-top
My solution is to add padding-top for select targeted to firefox only like this
到目前为止,这对我来说效果很好:
So far this is working fine for me:
我发现简单地将行高和高度设置为相同的像素数量会产生最一致的结果。我所说的“最一致”是指最佳一致,但当然它并不是跨浏览器 100%“像素完美”。此外,我发现 Firefox(v.17.x)倾向于将选项文本集中到下拉箭头的右侧;我仅通过在 OPTION 元素上设置少量的 padding-right 来缓解这个问题。此设置不会影响 IE 7-9 中的外观。
我的结果:
注意——我的 SELECT 元素使用较小的字体,10px。显然,您需要根据您的特定 UI 上下文相应地调整比例。
I found that simply setting the line-height and height to the same pixel quantity produced the most consistent result. By "most consistent" I mean optimally consistent but of course it is not 100% "pixel-perfect" across browsers. Additionally I found that Firefox (v. 17.x) tends to crowd the option text to the right against the drop-down arrow; I alleviated this with a small amount of padding-right set on the OPTION element only. This setting does not affect appearance in IE 7-9.
My result:
NOTE -- my SELECT element uses a smaller font, 10px. Obviously you will need to adjust proportions accordingly for your specific UI context.
刚刚遇到这个问题,但是对于移动设备,主要是mobile firefox。对我来说,诀窍是定义高度、填充、行高,最后是盒子大小,全部都在选择元素。此处不使用您的示例数字,而是为了举例:
just had this problem, but for mobile devices, mainly mobile firefox. The trick for me was to define a height, padding, line height, and finally box sizing, all on the select element. Not using your example numbers here, but for the sake of an example:
我尝试过以下方法,它对我有用:
I've tried as following and it worked for me:
您可以给出 : ,
并且对于父级,您必须给出position:relative 。
它会起作用的。
you can give :
and to parent you have to give position:relative.
it will work.
尝试设置“line-height”属性
,如下所示:
这里有一些有关此属性的文档:
http://www.w3.org/wiki/CSS/Properties/line-height
http://www.w3schools.com/cssref/pr_dim_line-height.asp
Try to set the "line-height" attribute
Like this:
Here you are some documentation about this attribute:
http://www.w3.org/wiki/CSS/Properties/line-height
http://www.w3schools.com/cssref/pr_dim_line-height.asp
我曾经使用具有相同值的
height
和line-height
,但事实证明它们在互联网上不一致。我目前的方法是将其与填充混合起来,如下所示。您还可以使用
padding
作为水平值,而不是width
+text-align
I used to use
height
andline-height
with the same values, but the proved to be inconsistent across the interwebs. My current approach is to mix that with padding like so.You could also use
padding
for the horizontal value instead of thewidth
+text-align
我也遇到了同样的问题,并花了几个小时来解决它。我终于想出了一些有用的东西。
基本上,我尝试过的任何方法都适用于所有情况,直到我放置一个 div 来复制选择框上第一个选项的文本并将实际的第一个选项留空。我使用 {pointer-events:none;} 让用户点击 div。
HTMLCSS
I've had the same problem and been working on it for hours. I've finally come up something that works.
Basically nothing I tried worked in every situation until I positioned a div to replicate the text of the first option over the select box and left the actual first option blank. I used {pointer-events:none;} to let users click through the div.
HTML
CSS
我发现只添加
padding-top
就会把右侧的灰色下拉箭头框压下去,这是不可取的。对我有用的方法是进入检查器并逐渐添加
padding
直到文本居中。这也会减小下拉图标的大小,但它也会居中,因此不会在视觉上造成干扰。I found that only adding
padding-top
pushed down the grey dropdown arrow box on the right, which was undesirable.The method that worked for me was to go into the inspector and incrementally add
padding
until the text was centered. This will also reduce the size of the dropdown icon, but it will be centered as well so it isn't as visually disturbing.该问题现已在 Firefox Nightly 中得到修复,并将在下一个 Firefox 版本中修复。
请参阅此错误以获取更多信息 https://bugzilla.mozilla.org/show_bug.cgi? id=610733
This has now been fixed in Firefox Nightly and will be in the next firefox build.
Please see this bug for more information https://bugzilla.mozilla.org/show_bug.cgi?id=610733
您还可以尝试保证金:
You can also try margin:
我知道的最接近的通用解决方案使用 box-align 属性,如此处所述。
工作示例是这里(我只能在Chrome上测试它,相信其他浏览器也有等效的)。
CSS:
The nearest general solution i know uses box-align property, as described here.
Working example is here (i can test it only on Chrome, believe that has equivalent for other browsers too).
CSS: