【问题标题】:Vertically align an input placeholder with CSS (or set line-height)使用 CSS 垂直对齐输入占位符(或设置行高)
【发布时间】:2019-09-27 02:38:01
【问题描述】:

是否可以独立于父输入垂直对齐占位符?似乎没有办法让下面代码中的占位符出现在输入字段的垂直中心。我已经尝试了所有我能想到或在网上找到的 css 属性和解决方法,但输入和占位符上的不同字体大小似乎至少在 webkit 浏览器中是不可能的。

编辑:左边是问题,右边是想要的样子:

input {
  font-size: 22px;
  text-align: center;
  font-weight: bold;
  line-height: 44px;
}

input::-webkit-input-placeholder {
  font-size: 9px;
  text-transform: uppercase;
  color: #AAA;
}
<input type='text' placeholder='enter some text' />

【问题讨论】:

标签: html css


【解决方案1】:

最好的解决方案可能是简单地将translate3d 转换添加到占位符 CSS 规则中,如下所示:

input {
  font-size: 22px;
  text-align: center;
  font-weight: bold;
  line-height: 44px;
}

input::-webkit-input-placeholder {
  font-size: 9px;
  text-transform: uppercase;
  color: #AAA;
  transform:translate3d(0,-4px,0)
}
<input type='text' placeholder='enter some text' />

【讨论】:

    【解决方案2】:

    这感觉像是一个愚蠢的“解决方案”,但也许你可以用transform:scale(0.45) 代替font-size: 9px;。它似乎在支持::-webkit-input-placeholder 中的font-size 的任何地方都可以工作。

    input {
      font-size: 22px;
      text-align: center;
      font-weight: bold;
      line-height: 44px;
    }
    
    input::-webkit-input-placeholder {
      text-transform: uppercase;
      color: #AAA;
      transform: scale(0.45);
    }
    <input type='text' placeholder='enter some text' />

    编辑:左对齐

    input {
      font-size: 22px;
      text-align: left;
      font-weight: bold;
      line-height: 44px;
    }
    
    input::-webkit-input-placeholder {
      text-transform: uppercase;
      color: #AAA;
      transform: scale(0.45);
      transform-origin: 0% 50%;
    }
    <input type='text' placeholder='enter some text' />

    【讨论】:

    • 哦,我喜欢它!如此hacky,看起来很完美!谢谢!!
    • @cronoklee 哈哈,很高兴你对此感到高兴。它只是让我觉得很脏。
    • 不了解你但不是垂直居中,或者我应该这样说,做不到。
    • 当输入左对齐时,此解决方案不起作用。现在正在尝试寻找解决方法...
    • @cronoklee 它可以通过调整。只需使用transform-origin: 0% 50%; 将变换设置为左中心即可。
    猜你喜欢
    • 1970-01-01
    • 2016-07-25
    • 2019-04-09
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    • 2022-11-22
    • 2014-01-08
    • 1970-01-01
    相关资源
    最近更新 更多