【问题标题】:Select form element inner text spacing选择表单元素内部文本间距
【发布时间】:2018-11-08 20:36:36
【问题描述】:

我在 -webkit 和其他浏览器之间的 select 元素上发现了这种不一致的行为。元素内文本的间距与其他输入元素不一致。 (见示例)。选择在左侧有一个额外的 ~2px 间距。因此不会与上面的输入元素水平对齐。

Chrome 工作正常,元素中的文本水平对齐。

Firefox 不能与上面的输入完全水平对齐。

元素上的填充是相同的。只有这个我无法解释的间距存在于选择元素中,而不是 -webkit 浏览器。我已经检查过了:

  • Safari(没有填充对齐)
  • Chrome(无填充对齐)
  • 火狐
  • 边缘
  • IE11

FF、Edge 和 IE11 都有(对我而言)无法解释的间距。没有文本缩进,我找不到任何东西可以解释这个额外的空间。

这是用于输入和选择的(简化的)css

html {
    font-size: 93.75%;
    line-height: 2;
    min-height: 100%;
    webkit-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
    -moz-osx-font-smoothing: grayscale;
    -webkit-font-smoothing: antialiased;
}

body {
    margin: 0;
    padding: 0;
    position: relative;
    background-color: #fff;
}

* {
    box-sizing: border-box;
}

.form
{
  background: #f1f1f1;
  padding: 15px;
  margin: 30px;
  max-width: 250px;
border-radius: 3px;
}

.form__group + .form__group
{
  margin-top: 10px;
}

input[type="text"], input[type="email"], input[type="tel"], input[type="password"] {
    border: 1px solid #999;
    padding: 6.5px 14px;
    margin: 0;
    width: 100%;
    border-radius: 0;
    -webkit-appearance: none;
    line-height: inherit;
    border-radius: 3px;
    height: 3rem;
    font-size: 14px;
    line-height: 24px;
}

select {
    font-size: 14px;
    line-height: 24px;
    -webkit-appearance: none;
    -webkit-padding-end: 29px !important;
    -webkit-padding-start: 14px !important;
    -moz-appearance: none;
    -o-appearance: none;
    appearance: none;
    border: 1px solid #999;
    padding: 6.5px 29px 6.5px 14px;
    margin: 0;
    width: 100%;
    outline: 0;
    border-radius: 3px;
    background: #fff;
    color: #484A4E;
    cursor: pointer;
    height: 3rem;
    background-size: 30px 50px;
    background-position: right center;
    background-repeat: no-repeat;
    background-image: url(data:image/svg+xml;base64,PHN2ZyBwcmVzZXJ2ZUFzcGVjdFJhdGlvPSdub25lJyB2aWV3Q…lsZT0nZmlsbDojREZFNEVDOycgcG9pbnRzPScxNSwxNyAxMCwyMiAyMCwyMicvPjwvc3ZnPg==);
}
<div class="form">
  <div class="form__group">
    <input type="text" name="example-input" value="" placeholder="Example input" />
  </div>
  <div class="form__group">
    <select name="example-select">
      <option value="">Example select</option>
      <option value="example">Example select item</option>
      <option value="another">Another one</option>
    </select>
  </div>
</div>

2018 年 8 月更新

经过一番头痛并在网上搜索后,我找到了两个答案 herehere 似乎“回答”了我的问题。答案就像我认为的浏览器渲染不一致。不幸的是,没有简单的 css 方法来解决这个问题。

【问题讨论】:

  • 在您的示例图像中,“无法解释”的空间在哪里?这两个图像对我来说看起来几乎一样。 :|
  • 它们都有相同的高度和相同的宽度我在这里没有发现任何奇怪的地方
  • 选择中的文本左侧有一个~2px的空间。它们在左侧不能很好地对齐。虽然间距相同。将更新我的问题以更好地澄清问题

标签: html css


【解决方案1】:

请遵循基本准则。

* {box-sizing: border-box; padding: 0;margin: 0;}

【讨论】:

  • 很抱歉,但这如何解决手头的问题,在尝试您的解决方案时问题仍然存在。使用 * 选择器添加 padding: 0; margin: 0; 并没有帮助,因为在表单元素上设置了填充和边距。 select 元素中的文本(据我所知)本身不是 html 元素(如 span 或其他东西),因此应用 * { ... } 不会影响内部文本,只会影响 select 元素本身(尽管该场外发生了变化里面文字的位置,并不能说明手头的问题)。
  • 我在 Mozilla 浏览器中看到输入中显示 2px 的输入。如果您仍然没有修复,请分享两个浏览器屏幕。
  • 在小提琴中应用您的修复程序后,它仍然发生。查看我最初的问题,以获取显示 Chrome 中的“工作对齐和间距”以及 Firefox 中的错误的屏幕截图。您可以在 FF 中打开这个小提琴(使用您的解决方案),与上面的输入元素相比,仍然可以看到 2px 左间距的问题。 https://jsfiddle.net/hdcgvs2z/。在 Chrome/Safari 中打开小提琴会显示表单元素内文本的正确对齐方式。
  • 这是 Firefox 浏览器的默认空间。如果你想删除你的Firefox,你可以使用。我有关于小提琴的更新:jsfiddle.net/Tushar9/hdcgvs2z/1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-10
  • 2011-08-14
  • 2015-04-18
  • 1970-01-01
  • 1970-01-01
  • 2021-09-14
  • 2018-06-28
相关资源
最近更新 更多