简体   繁体   English

在左边和右边对齐a <td>

[英]Aligning to left and right inside a <td>

I am writing a page for role-playing with some friends. 我正在写一个与一些朋友角色扮演的页面。 When it comes to the character sheet we would like to have some tables with the statistics. 说到角色表,我们想要一些带有统计数据的表格。 I would like to have inside every cell the name of the characteristic (strength, intelligence, etc.) and the number. 我想在每个单元格中都有特征(强度,智力等)和数字的名称。 Like this: http://jordi.dyndns-at-home.com:3000/characters/2 像这样: http//jordi.dyndns-at-home.com3000/characters/2

I would like to align the names to the left side of the cell and the numbers to the right side. 我想将名称对齐到单元格的左侧,将数字对齐到右侧。

I have tried with <span style="text-align:right;"> and it will not work. 我尝试使用<span style =“text-align:right;”>并且它不起作用。 I have tried with <div style="text-align:right;"> and it does work but it "jumps a line", if I use display:inline it will not work. 我尝试过使用<div style =“text-align:right;”>并且它确实有效,但如果我使用display,它会“跳出一条线”:内联它将无效。

It is possible to have both alignments on a <td> ? 可以在<td>上进行两个对齐吗?

BTW position:absolute; BTW位置:绝对; right:0 won't work. 右:0不起作用。 it will align to the end of the not the end of the 它将对齐到结束而不是结束

Use definition lists and be semantic. 使用定义列表并且是语义的。

HTML HTML

<table><tr><td>
    <dl>
        <dt>Life:</dt>
        <dd>15</dd>
    </dl>
</td></tr></table>

CSS CSS

dl {
    width: 200px;
}
dl dt {
    width: 160px;
    float: left;
    padding: 0;
    margin: 0;
}
dl dd {
    width: 40px;
    float: left;
    padding: 0;
    margin: 0;
}

This way you can drop the whole table. 这样你就可以放弃整个表格了。

Here is an example: 这是一个例子:

<table>
    <tr>
        <td>
            <span class='name'>name 1</span><span class='number'>100</span>
        </td>
    </tr>
</table>

With the following CSS: 使用以下CSS:

.name{
    width: 200px;
    display: inline-block;
}
.number{
    width: 200px; 
    display: inline-block;  
    text-align: right;
}

Hope you find this helpful. 希望你觉得这很有帮助。 Here is a jsFiddle for you to mess with. 这是一个让你搞砸的jsFiddle。

http://jsfiddle.net/K4fGq/ http://jsfiddle.net/K4fGq/

Bob 短发

虽然我同意Oli的评论,但我猜你可以通过使用float:leftfloat:right来实现它。

If you put another table inside the TD with two TD's, one left and one right aligned, it will work and I will get down voted for such a suggestion. 如果你把另一张桌子放在TD里面有两个TD,一个左边和一个右边对齐,它会起作用,我会投票给这样一个建议。

Another mechanism is to use display: inline-block as suggested by @rcravens -- my personal choice. 另一种机制是使用display:inline-block,如@rcravens所建议的那样 - 我个人的选择。

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

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