首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在文本区域中垂直对齐占位符文本?

如何在文本区域中垂直对齐占位符文本?
EN

Stack Overflow用户
提问于 2016-03-24 12:49:03
回答 7查看 37K关注 0票数 30

我正在尝试垂直对齐文本区域(文本框)中的占位符文本。我使用文本区域而不是文本输入,因为我需要使用多行。

代码语言:javascript
复制
.textbox1 { 
  width: 440px;
}
代码语言:javascript
复制
<textarea class="textbox1"name="mytextarea"placeholder="Name"></textarea>

EN

回答 7

Stack Overflow用户

发布于 2016-03-24 13:34:32

这适用于最新的Firefox,IE/Edge,Chrome的纯CSS:

代码语言:javascript
复制
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;
}

请参阅此fiddle。关键是将文本区域的height设置为与占位符相同的line-height

遗憾的是,vertical-align: middle;似乎还没有得到支持。

票数 6
EN

Stack Overflow用户

发布于 2016-03-24 13:25:33

您最好的选择是使用padding,因为line height不能在多条线路上工作。

此外,在计算填充时,请确保将行高/字体大小考虑在内。

票数 3
EN

Stack Overflow用户

发布于 2020-08-22 21:58:37

代码语言:javascript
复制
.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; 
}

你可以在这里查看CSS jsfiddle

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36193459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档