双向 HTML 表格对齐方式

mor*_*rck 1 html css html-table

我有一个包含两列的表:

<table border="1" bordercolor="#FFCC00" style="background-color:#FFFFCC" width="250" cellpadding="3" cellspacing="3">
    <tr>
        <td>Table Cell</td>
        <td>Table Cell</td>
    </tr>
    <tr>
        <td>Table Cell</td>
        <td>Table Cell</td>
    </tr>
    <tr>
        <td>Table Cell</td>
        <td>Table Cell</td>
    </tr>
    <tr>
        <td>Table Cell</td>
        <td>Table Cell</td>
    </tr>
    <tr>
        <td>Table Cell</td>
        <td>Table Cell</td>
    </tr>
    <tr>
        <td>Table Cell</td>
        <td>Table Cell</td>
    </tr>
    <tr>
        <td>Table Cell</td>
        <td>Table Cell</td>
    </tr>
    <tr>
        <td>Table Cell</td>
        <td>Table Cell</td>
    </tr>
    <tr>
        <td>Table Cell</td>
        <td>Table Cell</td>
    </tr>
    <tr>
        <td>Table Cell</td>
        <td>Table Cell</td>
    </tr>
    <tr>
        <td>Table Cell</td>
        <td>Table Cell</td>
    </tr>
    <tr>
        <td>Table Cell</td>
        <td>Table Cell</td>
    </tr>
    <tr>
        <td>Table Cell</td>
        <td>Table Cell</td>
    </tr>
    <tr>
        <td>Table Cell</td>
        <td>Table Cell</td>
    </tr>
</table>
<p style="font-family:verdana,arial,sans-serif;font-size:10px;"><a href="http://www.quackit.com/html/html_table_tutorial.cfm" target="_top">HTML Tables</a></p>
Run Code Online (Sandbox Code Playgroud)

我的文字有问题。如何将左侧文本与左侧对齐,右侧文本与右侧对齐?还有其他方法吗?

Lev*_*lho 5

下面的 CSS 将采用td每行中的第二个并将text-alignCSS 属性设置为向右对齐。

td:first-child+td {
    text-align: right;
}
Run Code Online (Sandbox Code Playgroud)

您当然可以(并且可能应该)向您的表格和此规则添加一个类,以便它仅适用于您想要以这种方式对齐的特定表格。

您还可以使用它来选择第二列中的单元格:

td:nth-child(2)
Run Code Online (Sandbox Code Playgroud)

但请记住,由于它是一个 CSS3 属性,因此它不像我的第一个示例那样受到广泛支持。