我想要垂直对齐textarea(文本框)中的占位符文本。我使用textarea而不是文本输入,因为我需要使用多行。
.textbox1 {
width: 440px;
}
<textarea class="textbox1"name="mytextarea"placeholder="Name"></textarea>
我想要垂直对齐textarea(文本框)中的占位符文本。我使用textarea而不是文本输入,因为我需要使用多行。
.textbox1 {
width: 440px;
}
<textarea class="textbox1"name="mytextarea"placeholder="Name"></textarea>
不要使用padding-top来增加文本域的高度和额外的下边距,而是使用line-height。这里有一个示例,您可以变化行高。
textarea::placeholder {
line-height: 90px;
你还可以像这样使用transform属性:
}
textarea::placeholder {
transform: translateY(-20px);
}
一种选择是使用 line-height
:
.textbox1 {
width: 440px;
height:30px;
line-height:30px;
}
.textbox1 {
width: 440px;
height:30px;
line-height:30px;
}
<textarea class="textbox1"name="mytextarea"placeholder="Name"></textarea>
你也可以使用 padding
控制文本的位置。
这里是一个使用 padding-top
的例子。
.textbox1 {
width: 440px;
padding-top:15px;
}
<textarea class="textbox1"name="mytextarea"placeholder="Name"></textarea>
更新
由于要求包括多行支持,建议设置顶部和底部填充,即:
padding-top
和 padding-bottom
.textbox1 {
width: 440px;
height:6px;
padding: 30px 5px;
}
.textbox1 {
width: 440px;
height:60px;
padding: 30px 5px;
}
<textarea class="textbox1"name="mytextarea"placeholder="Name"></textarea>
textarea {
width: 440px;
height:600px; /* Note this is the same height as the placeholders line-height */
}
::-moz-placeholder { /* Mozilla Firefox 19+ */
line-height:600px;
}
::-webkit-input-placeholder { /* Webkit */
line-height:600px;
}
:-ms-input-placeholder { /* IE */
line-height:600px;
}
查看此示例。关键是将
padding
,因为在多行情况下,line height
不起作用。padding
和行高
看起来都不太合适。 - Manohar Reddy Poreddy.textbox1{
width: 440px;
height:100px;
line-height: calc(100 * 7.5%);
}
.textbox1:focus{
line-height: 14px;
}
在这里查看。pure CSS jsFiddle
注意:CSS3的calc()函数仅适用于现代浏览器。如果您想在旧版浏览器上使用,可以手动更改/计算行高。
或者您确实需要使用jQuery
我在这里制作了一个jQuery帮助 jQuery jsFiddle
.textbox1 {
width: 440px;
height:70px
}
::-webkit-input-placeholder { /* Chrome/Opera/Safari */
line-height:70px;
text-align: center;
}
:-moz-placeholder { /* Firefox 18- */
line-height:70px;
text-align: center;
}
::-moz-placeholder { /* Firefox 19+ */
line-height:70px;
text-align: center;
}
:-ms-input-placeholder { /* IE 10+ */
line-height:70px;
text-align: center;
}
作为另一种解决方案 - 您可以将行数设置为奇数,并在换行符中添加向下舍入的一半行数,以将占位符放置在中间...
.textbox1 {
width: 440px;
}
<textarea class="textbox1"name="mytextarea"placeholder=" Name" rows=3></textarea>
<textarea name="example" onFocus={onFocus} onBlur={outFocus} />
<label className={styles.Teaxarea_label}>Label</label>
然后你有那些函数:
const outFocus = (event) => {
if (event.target.value !== '') {
event.target.nextSibling.style = "display:none;"
}
else {
event.target.nextSibling.style = "display:block;"
}
}
const onFocus = (event) => {
event.target.nextSibling.style = "display:none;"
}
CSS:
.Teaxarea_label{
position: absolute;
user-select: none;
pointer-events: none;
display: block;
}
textarea {
position: relative;
}
.textbox1 {
--title-modal--textarea--height: 100px;
/* font-size variable for calculations cannot be em, otherwise calculations become doubly-dependent on font-size */
--title-modal--textarea--font-size: 1rem;
--title-modal--textarea--line-height: 1.5;
height: var(--title-modal--textarea--height) !important;
font-size: var(--title-modal--textarea--font-size);
line-height: var(--title-modal--textarea--line-height);
padding-top: calc((var(--title-modal--textarea--height) - (var(--title-modal--textarea--line-height) * var(--title-modal--textarea--font-size)))/2);
/* I used these to make sure the border doesn't take away from the height, */
/* affecting the padding calculation, */
/* but you could similarly turn them into variables and subtract */
border-bottom: 0;
border-top: 0;
}
line-height
属性使占位符垂直居中。