html - 为什么具有相同字体大小、行高、填充和高度的文本区域和输入文本垂直对齐方式不同?

标签 html css input textarea vertical-alignment

enter image description here

问题 1:

为什么要执行以下操作 textarea和文本input如果它们具有相同的 font-size,则具有不同的垂直文本对齐方式 , line-height , height , padding

问题1.1

我怎样才能制作textareainput 具有相同的垂直对齐方式?

.myTextarea {
  display: block;
  font-family: Arial;
  font-size: 14px;
  line-height: 21px;
  height: 32px;
  padding: 2px 5px;

  resize: none;
}

.myInput {
  display: block;
  font-family: Arial;
  font-size: 14px;
  line-height: 21px;
  height: 32px;
  padding: 2px 5px;
}

.myDiv {
  margin-top: 20px;
  margin-bottom: 5px;
  font-weight: bold;
}
<div>
  <div class="myDiv">Textarea</div>
  <textarea class="myTextarea" rows="1">12345</textarea>
  <div class="myDiv">Input</div>
  <input class="myInput" type="text" value="12345"/>
</div>

最佳答案

文本区域用于多行文本,而输入用于单行。

这就是为什么行高不会对输入产生影响,因为它被认为等于输入的高度。

您可以删除输入中的行高,因为它对输入没有影响。

此外,您的行高应该与文本区域的高度相同,以重现与输入相同的效果。

.myTextarea {
  display: block;
  font-family: Arial;
  font-size: 14px;
  line-height: 32px;
  height: 32px;
  padding: 2px 5px;

  resize: none;
}

.myInput {
  display: block;
  font-family: Arial;
  font-size: 14px;
  height: 32px;
  padding: 2px 5px;
}

.myDiv {
  margin-top: 20px;
  margin-bottom: 5px;
  font-weight: bold;
}
<div>
  <div class="myDiv">Textarea</div>
  <textarea class="myTextarea" rows="1">12345</textarea>
  <div class="myDiv">Input</div>
  <input class="myInput" type="text" value="12345"/>
</div>

关于html - 为什么具有相同字体大小、行高、填充和高度的文本区域和输入文本垂直对齐方式不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56327798/

相关文章:

html - 使用 rem 调整大小时是否需要设置基本字体大小?

html - iframe 在更改某些值后在 chrome 上工作但在 mozilla 上不工作

javascript - 使用 jQuery 防止在 HTML 文本框中键入空格

html - css 隐藏圆 Angular 后面的背景

javascript - 使用 jQuery 更改一个元素的 CSS 不起作用

javascript - 从 html 表单获取 javascript 变量

html - -webkit-用户选择 : none; - not working

java.util.NoSuchElementException - 扫描器读取用户输入

python - 为什么 Python 看不到文件中的所有行?

jquery - 如何将 JQuery 图像 slider 与母版页一起使用?