【问题标题】:Centered text in html select with CSS in Safari在 Safari 中使用 CSS 选择 HTML 中的居中文本
【发布时间】:2020-05-27 22:06:04
【问题描述】:

我正在尝试使用 CSS 使 HTML 选择表单中的文本居中,它适用于除 Safari 之外的所有浏览器,我尝试使用 text-align: -webkit-center; 但它不起作用。

select {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
}

label.cw-select {
  display: inline-block;
  position: relative;
}

.cw-select select {
  background: #4d4d4d;
  border-radius: 0.3rem;
  border: 1px solid #212121;
  color: white;
  display: inline-block;
  height: 30px;
  line-height: 1.2;
  margin: 0;
  min-width: 48px;
  outline: none;
  padding: 4px 20px 3px 0px;
  text-align: center;
  text-align: -webkit-center;
  text-align: -moz-center;
}

.cw-select:after {
  background: #1d1d1d;
  border-top-right-radius: 0.3rem;
  border-bottom-right-radius: 0.3rem;
  bottom: 0;
  color: white;
  content: "▼";
  font-size: 60%;
  line-height: 30px;
  padding: 0 6px;
  pointer-events: none;
  position: absolute;
  right: 0;
  top: 0;
}
<label class="cw-select">
 <select name="numberOfPlayers" form="numberOfPlayers">
   <option value="1">1</option>
   <option value="2">2</option>
   <option value="3">3</option>
   <option value="4">4</option>
   <option value="5">5</option>
 </select>
</label>

Safari

其他

【问题讨论】:

  • 也添加你的html
  • 你想要这个选项还需要center吗?
  • 如果你想包含 safari,也许文本缩进可以工作。 (text-align-last) 在这里似乎还不起作用 (safari/IE)
  • 这能回答你的问题吗? Is it possible to center text in select box?

标签: css select safari


【解决方案1】:

我认为左侧填充设置为 0。请检查此代码。

.cw-select select {
    background: #4d4d4d;
    border-radius: 0.3rem;
    border: 1px solid #212121;
    color: white;
    display: inline-block;
    height: 30px;
    line-height: 1.2;
    margin: 0;
    min-width: 48px;
    outline: none;
    padding: 4px 20px 3px 10px; // instead of 4px 20px 3px 0;
    text-align: center;
    text-align: -webkit-center;
    text-align: -moz-center;
}

【讨论】:

  • 我试过了,这只是在左边添加了填充,实际上并没有使文本居中。
猜你喜欢
  • 2021-05-17
  • 1970-01-01
  • 1970-01-01
  • 2023-01-25
  • 2011-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-30
相关资源
最近更新 更多