简体   繁体   English

在表格单元格中垂直对齐文本跨度

[英]Align span of text vertically in table cell

How can I align vertical text, that is generated by two spans, inside a div inside a table cell.如何在表格单元格内的 div 内对齐由两个跨度生成的垂直文本。 I've tried many combinations of text-align,display but nothing worked.我尝试了多种文本对齐、显示的组合,但没有任何效果。 I have this html segment我有这个 html 段

<table>
    <tr>
        <td>
            <div class="container">
                <span>This is span-sentence-1</span>
                 <span>This is span-sentence-2</span>
            </div>
        </td>
    </tr>
</table>

and the output is输出是

This is span-sentence-1 This is span-sentence-2

while I want to be rendered like this虽然我想被渲染成这样

This is span-sentence-1
This is span-sentence-2

fiddle: http://jsfiddle.net/hjuxdd1b/1/小提琴: http : //jsfiddle.net/hjuxdd1b/1/

You can use following:您可以使用以下内容:

.container {
    width: 100%;
    line-height: 50px;
    border: 1px solid black;
    /*height: 50px; Remove height*/
}

.container span{
    display: block;/*Set display to  block*/
}

fiddle小提琴

Give display: block to .container spandisplay: block.container span

.container span {display: block;}

Remove the height and line-height in the .container删除.containerheightline-height

Fiddle: http://jsfiddle.net/praveenscience/hjuxdd1b/7/小提琴: http : //jsfiddle.net/praveenscience/hjuxdd1b/7/

You may not need that div:你可能不需要那个 div:

  • Set those spans to display: block so they are each given their own line.将这些跨度设置为display: block以便它们每个都有自己的行。

  • Give vertical-align: middle to the td so that its content will stay vertically centred.给 td 设置vertical-align: middle以便其内容保持垂直居中。

Have a fiddle有一个小提琴

By default a span is display: inline so they will line up next to each other.默认情况下,跨度为display: inline因此它们将彼此相邻排列。 You could read more about the span element over on the MDN .您可以 在 MDN 上阅读有关 span 元素的更多信息

CSS CSS

td {
    vertical-align: middle;
    height: 100px;
    border: 1px solid black;
}
td span {
    display: block;
}

HTML HTML

<table>
    <tr>
        <td> 
            <span>This is span-sentence-1</span>
            <span>This is span-sentence-2</span>
        </td>
    </tr>
</table>

在第一个 span 后添加 br 标签或用 div 替换 span。

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

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