我试过这个:http://jsfiddle.net/ilyaD/KGcC3/

HTML:

<select name="state" class="ddList">
    <option value="">(please select a state)</option>
    <option class="lt" value="--">none</option>
    <option class="lt" value="AL">Alabama</option>
    <option class="lt" value="AK">Alaska</option>
    <option class="lt" value="AZ">Arizona</option>
    <option class="lt" value="AR">Arkansas</option>
    <option class="lt" value="CA">California</option>
    <option class="lt" value="CO">Colorado</option>
</select>

CSS:

select { width: 400px; text-align: center; }
select .lt { text-align: center; }

如你所见,它不起作用。是否有css唯一的方法来居中选择框中的文本?


当前回答

在你的选择使用宽度:自动和无填充来查看你的文本有多长。 我在我的选择上使用100%的可用宽度,所有的选项都有相同的长度,这允许我使用非常简单的css。

text-indent将从左边移动文本,类似于左填充 120px是我的文本长度-我想把它居中,所以取一半的大小和一半的选择大小,留下50% - 60px

select{
  width: 100%;
  text-indent: calc(50% - 60px);
}

如果我有不同大小的选项呢?

然而,解决方案可能并不完美。 如果选项之间的差异不像5个字符,那么前一种解决方案可能会让你非常接近居中。

如果你仍然需要更精确的居中,你可以这样做

准备这门课:

.realWidth{
   width: auto;
}

应用onChange监听器选择元素 在该监听器中,将.realWidth应用到select元素with

const selectRef = document.getElementById("yourId");
selectRef.classList.add("realWidth");

获取期权的实际宽度。

const widthOfSelect = selectRef.getBoundingClientRect().width / 2;

widthOfSelect是你要找的宽度。存储在全局/组件变量中。 删除realWidth,你不再需要它了。

selectRef.classList.remove("realWidth");

我正在使用react,我不确定这将在香草工作,如果不是你必须找到另一个解决方案。

<select style={`textIndent: calc(50% - ${widthOfSelect}) %`}> ... </select>

另一个解决方案,然而,这是一个坏的一个可以创建CSS类与js和把它放在头部。

优点:

可能有用,我还没试过动态解决方案,但应该有用。

缺点:

如果程序不够快,用户将看到width: auto发生,因此想知道发生了什么。如果是这种情况,只需创建重复的select,将它隐藏在具有更高z-index的东西后面,并将on select侦听器应用于原始的隐藏副本。 可能很难使用,如果你不能内联的风格,因为香草的限制,但你可以做一个脚本来优化appendChild的头部。

其他回答

我也遇到过类似的问题。 我的解决方法是在样式部分更改为固定大小的字体:

option {
  font-family: Courier New;
  width: 12ch;
  font-size: 14pt
}

select {
  font-family: Courier New;
  width: 14ch;
  font-size: 14pt
}

在body中,我用下划线填充所显示的选项,使它们具有相同的长度。 注意使用下划线两边的中心显示:

<select onchange='O.src=this.options[this.selectedIndex].value' align="center">
  <option value="" align="center">___(none)___</option>
  <option value="https://www.247backgammon.org/" align="center">_Backgammon_</option>
  <option value="https://www.247klondike.com/klondikeSolitaire3card.php" align="center">__Klondike__</option>
  <option value="https://www.247minesweeper.com/" align="center">_Minesweeper</option>
  <option value="https://sudoku.com/" align="center">
    <p>___Soduku___</p>
  </option>
</select>

虽然你不能将选项文本居中,但你可以在选择的顶部放置一个绝对定位的div,以达到同样的效果:

#centered
{
  position: absolute;
  top: 10px;
  left: 10px;
  width: 818px;
  height: 37px;
  text-align: center;
  font: bold 24pt calibri;
  background-color: white;
  z-index: 100;
}

#selectToCenter
{
  position: absolute;
  top: 10px;
  left: 10px;
  width: 840px;
  height: 40px;
  font: bold 24pt calibri;
}

$('#selectToCenter').on('change', function () {
    $('#centered').text($(this).find('option:selected').text());
});

<select id="selectToCenter"></select>
<div id="centered"></div>

确保div和select在文档中的位置是固定的。

2020年,我使用:

select {
    text-align: center;
    text-align-last: center;
    -moz-text-align-last: center;
}

就用这个:

select {

text-align-last: center;
padding-right: 29px;

}

不完全是居中,但是使用上面的例子,你可以在选择框中设置左边边距。

<style>.UName { text-indent: 5px; }</style><br>

<select  name="UserName" id="UserName" size="1">
    <option class="UName" selected value="select">Select User</option>
    <option class="UName" value="User1">User 1 Name</option>
    <option class="UName" value="User2">User 2 Name </option>
    <option class="UName" value="User3">User 3 Name</option>
</select>