html - 无论屏幕分辨率如何,都将文本垂直对齐到跨度/标签的中间

标签 html css vertical-alignment

我可以在 span(位于 div 内)中垂直对齐文本,但我需要我的页面独立于屏幕分辨率。当我更改分辨率时,已经使用行高功能来阻止我的文本超出 div。但是我看到它在所有分辨率中都没有垂直对齐为中间。我应该研究 CSS 中的哪些功能?

<div>
 <span style="width:49%; height:50%; top:25%; left:0%; line-height:50%;font-size:x-small;font-family:Verdana;font-weight:bold; position:absolute; display:block;">
      Random Text
    </span>
</div>

上面的 span 标签在一个 div 中。另外 line-height 的推荐值应该是多少。它应该与 span 标签的高度相同吗? 我看到了this link用于理解单行文本的垂直对齐。

> Some other CSS doubts>> 1. a textbox inside a div containing single line of text also goes out of the control while changing screen resolution. what could I possibly do to solve this?I am about to control it in a button

最佳答案

删除那些 position 除非非常需要和使用,display:table-cell

Fiddle Demo

div  span {
    border:1px solid red;
    width:49%;
    height:50%;
    display:table-cell; /*check this */
    vertical-align:middle; /**aligns where you want it */
    font-size:x-small;
    font-family:Verdana;
    font-weight:bold;
}

您可能也想查看此线程:Align multiple divs in one line and center text veticaly and horizontally

关于html - 无论屏幕分辨率如何,都将文本垂直对齐到跨度/标签的中间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25461483/

相关文章:

html - 如何让 html 元素覆盖并超出 CSS "column"

css - 修复居中 div 左侧的 div

html - 如何让我的旋转木马仅填充屏幕尺寸的 60%,以便其余部分可供其正下方的面板使用

html - 页脚未粘在页面底部

html - CSS样式错误

html - 防止文本向左移动一点

html - 如何附加页脚?

ios - 设置 UILabel 顶部对齐在单元格中不起作用

delphi - VirtualStringTree - 多行节点和垂直居中文本

html - 使用变换 : translateY(-50%); inside absolutely positioned div 垂直对齐