【问题标题】:How can I center the HTML date & time inputs' native controls?HTML5 日期和时间输入的原生控件定位
【发布时间】:2019-05-20 15:30:18
【问题描述】:

如果我将自定义高度设置为 <input type="date" /> 元素,则悬停时出现的本机控件(向上和向下按钮)与输入的顶部而不是中心对齐。

input {
  height: 40px;
}
<input type="date" />

type="time" 输入也会发生同样的情况,但type="number" 不会。如何将这些按钮定位到输入的中心(垂直)?

【问题讨论】:

  • 在高度上使用填充代替

标签: css html html-input html5-input-date


【解决方案1】:

使用padding 代替height

input {
      padding: 10px 0px 10px 0px;
}
<input type="date" />

使用height

input {
  height: 40px;
}
input[type=date]::-webkit-inner-spin-button {
margin-top: 10px!important;
}



input[type=date]::-webkit-calendar-picker-indicator {
margin-top: 10px!important;
}
<input type="date" />

【讨论】:

  • 在某些情况下我需要设置一个固定的高度而不是填充,那么有什么解决方案吗?
  • 太好了,谢谢!那些伪元素选择器正是我要找的
【解决方案2】:

input[type=date]::-webkit-inner-spin-button,
input[type=date]::-webkit-outer-spin-button {
  height: 40px;
}
<input type="date" />

海这个我刚刚编辑的。

【讨论】:

  • 上下箭头仍未居中
  • 是的,我运行了代码,它不影响向上/向下按钮
猜你喜欢
  • 2017-03-05
  • 2015-12-17
  • 2021-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-27
  • 2015-09-06
相关资源
最近更新 更多