Html 中间(垂直)对齐 <textarea> 内的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5677633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:01:25  来源:igfitidea点击:

Middle (vertically) align text inside a <textarea>

htmlcss

提问by Daniel Birowsky Popeski

It's a multiline search-box, so i want everything aligned in the middle. Is it possible?

这是一个多行搜索框,所以我希望所有内容都在中间对齐。是否可以?

采纳答案by Daniel Birowsky Popeski

Back in the days when i was asking this, i tried achieving this with textarea which is really not possible without scripting. The answer was adding HTML5's contenteditable="true" on a table cell with style="vertical-align: middle". If one's project allows modern HTML, this is a rather simple and effective solution.

回到我问这个问题的日子里,我尝试使用 textarea 实现这一点,这在没有脚本的情况下是不可能的。答案是在 style="vertical-align: middle" 的表格单元格上添加 HTML5 的 contenteditable="true"。如果一个项目允许使用现代 HTML,这是一个相当简单有效的解决方案。

If instead of adding <table>and <tr>and <td>to your markup, you would like something regular like a <div>, you would still need two of them:

如果不是将<table>and 和<tr>and添加<td>到您的标记中,而是想要像 a 这样的常规内容<div>,您仍然需要其中的两个:

<div style="display: table">
  <div 
    style="display: table-cell; vertical-align: middle"
    contenteditable="true">
    I am vertically aligned
  </div>
</div>

回答by alex

You can fake it with vertical padding.

您可以使用垂直填充来伪造它。

textarea {
    padding: 30px 0;
}

Generally you could use the line-heightproperty, but you can not with more than one line of text.

通常您可以使用该line-height属性,但不能使用多于一行的文本。

回答by Petryk P'yatochkin

There is the HTML5 contenteditableattribute. http://www.w3schools.com/tags/att_global_contenteditable.asp

有 HTML5contenteditable属性。 http://www.w3schools.com/tags/att_global_contenteditable.asp

Maybe the divtag instead of textareacan solve your problem.

也许div标签而不是textarea可以解决您的问题。

回答by David Houde

You might be able to make a script that determines the elements height, line height, and lines of text and calculate a padding-top, or even \n to prepend the text.

您也许能够制作一个脚本来确定元素高度、行高和文本行数,并计算 padding-top,甚至 \n 以添加文本。

I do not think this is possible with just CSS though.

不过,我认为仅使用 CSS 是不可能的。