简体   繁体   English

在宽度和高度固定的范围内垂直对齐文本

[英]Vertically align text in a span with fixed width and height

I have a span with fixed width and height, but the length of the text inside it is not fixed, which can be either a single word or very long. 我的跨度具有固定的宽度和高度,但是其中的文本长度不是固定的,可以是单个单词,也可以很长。 How can I always vertically align the text to the middle of the span? 如何始终将文本垂直对齐到跨度的中间? For example, in my http://jsfiddle.net/w2cfkocv/1/ , the second span only contains 1 word, but I want it to align in the middle of the span vertically. 例如,在我的http://jsfiddle.net/w2cfkocv/1/中 ,第二个跨度仅包含1个单词,但我希望它在跨度的中间垂直对齐。 Thanks. 谢谢。

<table summary="dialogTitle" class="pageTitle">
<tr>
    <td style="">         
        <span id="headertitle"> title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title </span><br>
        <span id="headertitle"> title </span>
    </td>
</tr>

#headertitle {
    border: 1px solid #000;
    font-size:14px;
    color:#000000;
    font-weight:bold;  
    width:200px;
    height:100px;
    line-height: 25px;
    display:inline-block;
    overflow:hidden;
    float: left;
    word-wrap: break-word;
    vertical-align: middle;
}

First don't use duplicate ID's(#) , and then change your display:inline-block to display:table-cell 首先不要使用重复的ID(#),然后将display:inline-block更改为display:table-cell

here is a snippet: 这是一个片段:

 .headertitle { border: 1px solid #000; font-size: 14px; color: #000000; font-weight: bold; width: 200px; height: 100px; line-height: 25px; display: table-cell; word-wrap: break-word; vertical-align: middle; } 
 <table summary="dialogTitle" class="pageTitle"> <tr> <td style=""> <span class="headertitle"> title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title title </span> <span class="headertitle"> title </span> </td> </tr> </table> 

This is the result: 结果如下:
在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM