我想垂直对齐选择框中的文本

发布于 2024-10-27 16:56:29 字数 392 浏览 1 评论 0原文

我想垂直对齐选择框中的文本。我尝试使用

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

慵挽 2024-11-03 16:56:29

您最好的选择可能是调整顶部填充和顶部填充。跨浏览器进行比较。它并不完美,但我认为它已经是你能得到的最接近的了。

padding-top:4px;

您需要的填充量取决于字体大小。

提示:如果您的字体设置为 px,请也将填充设置为 px。如果您的字体设置为 em,请也将填充设置为 em

编辑

这些是我认为您拥有的选项,因为垂直对齐在浏览器中不一致。

A. 删除高度属性并让浏览器处理它。这通常对我来说效果最好。

 <style>
    select{
        border: 1px solid #ABADB3;
        margin: 0;
        padding: auto 0;
        font-size:14px;
    }
    </style>
    <select>
        <option>option 1</option>
        <option>number 2</option>
    </select>

B. 添加顶部填充以下推文本。 (在某些浏览器中按下箭头)

<style>
select{
    height: 28px !important;
    border: 1px solid #ABADB3;
    margin: 0;
    padding: 4px 0 0 0;
    font-size:14px;
}
</style>
<select>
    <option>option 1</option>
    <option>number 2</option>
</select>

C. 您可以使字体更大,以尝试更好地匹配选择的高度。

<style>
select{
    height: 28px !important;
    border: 1px solid #ABADB3;
    margin: 0;
    padding: 0 0 0 0;
    font-size:17px;
}
</style>
<select>
    <option>option 1</option>
    <option>number 2</option>
</select>

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.

padding-top:4px;

The amount of padding you need will depend on the font size.

Tip: If your font is set in px, set padding in px as well. If your font is set in em, set the padding in em 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.

 <style>
    select{
        border: 1px solid #ABADB3;
        margin: 0;
        padding: auto 0;
        font-size:14px;
    }
    </style>
    <select>
        <option>option 1</option>
        <option>number 2</option>
    </select>

B. Add top-padding to push down the text. (Pushed down the arrow in some browsers)

<style>
select{
    height: 28px !important;
    border: 1px solid #ABADB3;
    margin: 0;
    padding: 4px 0 0 0;
    font-size:14px;
}
</style>
<select>
    <option>option 1</option>
    <option>number 2</option>
</select>

C. You can make the font larger to try and match the select height a little nicer.

<style>
select{
    height: 28px !important;
    border: 1px solid #ABADB3;
    margin: 0;
    padding: 0 0 0 0;
    font-size:17px;
}
</style>
<select>
    <option>option 1</option>
    <option>number 2</option>
</select>
欲拥i 2024-11-03 16:56:29

我偶然发现了这组 css 属性,它们似乎在 Firefox 中垂直对齐大小选择元素中的文本:

select
{
    box-sizing: content-box;
    -moz-box-sizing:content-box;
    -webkit-box-sizing:content-box;
}

不过,如果有的话,它在 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:

select
{
    box-sizing: content-box;
    -moz-box-sizing:content-box;
    -webkit-box-sizing:content-box;
}

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.

别在捏我脸啦 2024-11-03 16:56:29

我的解决方案是仅像这样为针对 Firefox 的选择添加 padding-top

// firefox vertical aligment of text
@-moz-document url-prefix() {
    select {
        padding-top: 13px;
   }
}

My solution is to add padding-top for select targeted to firefox only like this

// firefox vertical aligment of text
@-moz-document url-prefix() {
    select {
        padding-top: 13px;
   }
}
风苍溪 2024-11-03 16:56:29

到目前为止,这对我来说效果很好:

line-height: 100%;

So far this is working fine for me:

line-height: 100%;
江南烟雨〆相思醉 2024-11-03 16:56:29
display: flex;
align-items: center;
display: flex;
align-items: center;
吲‖鸣 2024-11-03 16:56:29

我发现简单地将行高和高度设置为相同的像素数量会产生最一致的结果。我所说的“最一致”是指最佳一致,但当然它并不是跨浏览器 100%“像素完美”。此外,我发现 Firefox(v.17.x)倾向于将选项文本集中到下拉箭头的右侧;我仅通过在 OPTION 元素上设置少量的 padding-right 来缓解这个问题。此设置不会影响 IE 7-9 中的外观。

我的结果:

select, option {
    font-size:10px;
    height:19px;
    line-height: 19px;
    padding:0;
    margin:0;
}

option {
    padding-right:6px; /* Firefox */
}

注意——我的 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:

select, option {
    font-size:10px;
    height:19px;
    line-height: 19px;
    padding:0;
    margin:0;
}

option {
    padding-right:6px; /* Firefox */
}

NOTE -- my SELECT element uses a smaller font, 10px. Obviously you will need to adjust proportions accordingly for your specific UI context.

轮廓§ 2024-11-03 16:56:29

刚刚遇到这个问题,但是对于移动设备,主要是mobile firefox。对我来说,诀窍是定义高度填充行高,最后是盒子大小,全部都在选择元素。此处不使用您的示例数字,而是为了举例:

padding: 20px;
height: 60px;
line-height: 1;
-webkit-box-sizing: padding-box;
-moz-box-sizing: padding-box;
box-sizing: padding-box;

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:

padding: 20px;
height: 60px;
line-height: 1;
-webkit-box-sizing: padding-box;
-moz-box-sizing: padding-box;
box-sizing: padding-box;
喜爱皱眉﹌ 2024-11-03 16:56:29

我尝试过以下方法,它对我有用:

select {
     line-height:normal;
     padding:3px;
} 

I've tried as following and it worked for me:

select {
     line-height:normal;
     padding:3px;
} 
南冥有猫 2024-11-03 16:56:29

您可以给出 : ,

select{
   position:absolute;
   top:50%;
   transform: translateY(-50%);
}

并且对于父级,您必须给出position:relative 。
它会起作用的。

you can give :

select{
   position:absolute;
   top:50%;
   transform: translateY(-50%);
}

and to parent you have to give position:relative.
it will work.

卸妝后依然美 2024-11-03 16:56:29

尝试设置“line-height”属性

,如下所示:

select{
    height: 28px !important;
    line-height: 28px;
}

这里有一些有关此属性的文档:

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:

select{
    height: 28px !important;
    line-height: 28px;
}

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

如痴如狂 2024-11-03 16:56:29

我曾经使用具有相同值的 heightline-height ,但事实证明它们在互联网上不一致。我目前的方法是将其与填充混合起来,如下所示。

select {
  font-size:14px;
  height:18px;
  line-height:18px;
  padding:5px 0;
  width:200px;
  text-align:center;
}

您还可以使用 padding 作为水平值,而不是 width + text-align

I used to use height and line-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.

select {
  font-size:14px;
  height:18px;
  line-height:18px;
  padding:5px 0;
  width:200px;
  text-align:center;
}

You could also use padding for the horizontal value instead of the width + text-align

却一份温柔 2024-11-03 16:56:29

我也遇到了同样的问题,并花了几个小时来解决它。我终于想出了一些有用的东西。

基本上,我尝试过的任何方法都适用于所有情况,直到我放置一个 div 来复制选择框上第一个选项的文本并将实际的第一个选项留空。我使用 {pointer-events:none;} 让用户点击 div。

HTMLCSS

    <div class='custom-select-container'>
      <select>
        <option></option>
        <option>option 1</option>
        <option>option 2</option>
      </select>
      <div class='custom-select'>
        Select an option
      </div>
    <div>

.custom-select{position:absolute; left:28px; top:10px; z-index:1; display:block; pointer-events:none;}

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

    <div class='custom-select-container'>
      <select>
        <option></option>
        <option>option 1</option>
        <option>option 2</option>
      </select>
      <div class='custom-select'>
        Select an option
      </div>
    <div>

CSS

.custom-select{position:absolute; left:28px; top:10px; z-index:1; display:block; pointer-events:none;}
装纯掩盖桑 2024-11-03 16:56:29

我发现只添加 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.

栀梦 2024-11-03 16:56:29

该问题现已在 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

绻影浮沉 2024-11-03 16:56:29

您还可以尝试保证金:

.select{
    margin-top: 25px;
}

You can also try margin:

.select{
    margin-top: 25px;
}
似最初 2024-11-03 16:56:29

我知道的最接近的通用解决方案使用 box-align 属性,如此处所述。
工作示例是这里(我只能在Chrome上测试它,相信其他浏览器也有等效的)。

CSS:

select{
  display:-webkit-box;
  display:-moz-box;
  display:box;
  height: 30px;;
}
select:nth-child(1){
  -webkit-box-align:start;
  -moz-box-align:start;
  box-align:start;
}
select:nth-child(2){
  -webkit-box-align:center;
  -moz-box-align:center;
  box-align:center;
}
select:nth-child(3){
  -webkit-box-align:end;
  -moz-box-align:end;
  box-align:end;
}

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:

select{
  display:-webkit-box;
  display:-moz-box;
  display:box;
  height: 30px;;
}
select:nth-child(1){
  -webkit-box-align:start;
  -moz-box-align:start;
  box-align:start;
}
select:nth-child(2){
  -webkit-box-align:center;
  -moz-box-align:center;
  box-align:center;
}
select:nth-child(3){
  -webkit-box-align:end;
  -moz-box-align:end;
  box-align:end;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文