简体   繁体   English

使用CSS对齐表格中的文本

[英]Aligning text in a table with CSS

I have a really simple table with two rows and two columns. 我有一个非常简单的表,有两行和两列。 I want the text in the first column to be right-aligned and the second column to be left-aligned. 我希望第一列中的文本右对齐,第二列左对齐。 I'm sure this is really easy but I just can't figure it out. 我确信这很容易,但我无法理解。

Here's the HTML: 这是HTML:

<table>
 <tr><td>Previous:</td><td>Link 1</td></tr>
 <tr><td>Next:</td><td>Link 2</td></tr>     
</table>

How would I do this? 我该怎么做?

I would use classes in this instance. 我会在这个例子中使用类。 You could get fancy but this is the best way for supporting. 你可能会喜欢,但这是最好的支持方式。

CSS CSS

.alignright { text-align: right; }
.alignleft { text-align: left; }

HTML HTML

<td class="alignright"> </td>
<td class="alignleft"> </td>

You could go further by adding padding, margins and further classes. 你可以通过添加填充,边距和更多类来进一步。 Depending on your TABLE css you might need to add some padding so that the cells aren't all padding: 0 and not showing any alignment. 根据您的TABLE css,您可能需要添加一些填充,以便单元格不是所有填充:0并且不显示任何对齐。

You can either use :first-child to target the cells in the first column: 您可以使用:first-child来定位第一列中的单元格:

td {
    text align: left;
}

td:first-child {
    text-align: right;
}

But :first-child : doesn't work in IE 6 , so you might want to add a class to the cells in the first column, and use that instead: 但是:first-child :在IE 6中不起作用 ,所以你可能想在第一列的单元格中添加一个类,而是使用它:

<table>
 <tr><td class="first">Previous:</td><td>Link 1</td></tr>
 <tr><td class="first">Next:</td><td>Link 2</td></tr>     
</table>

td {
    text align: left;
}

td.first {
    text-align: right;
}

As written, this will apply to all <td> elements, so you might also want to add a class to your table and limit the styles to <td> s in that table: 如上所述,这将适用于所有<td>元素,因此您可能还想在表中添加一个类,并将样式限制为该表中的<td>

<table class="simple">
 <tr><td class="first">Previous:</td><td>Link 1</td></tr>
 <tr><td class="first">Next:</td><td>Link 2</td></tr>     
</table>

table.simple td {
    text align: left;
}

table.simple td.first {
    text-align: right;
}

As an alternative and given your scenario and if you are able to - why don't you replace the <td> 's in your second column with <th> 's and then the CSS will be really simple: 作为替代方案并给出您的方案,如果您能够 - 为什么不用<th>替换第二列中的<td> ,那么CSS将非常简单:

td { text-align:right; }
th { text-align:left; }

set different classes on each td element 在每个td元素上设置不同的类

<style>
    td.raligned {text-align: right;} 
    td.leftaligned {text-align: left;} 
</style>

<table>
    <tr>
        <td class="raligned">blah</td>
        <td class="leftaligned">blah</td>
    </tr>
</table>

It's easy. 这很简单。 Create a class of CSS 创建一个CSS类

<style>
.aleft {text-align: left;}
.aright {text-align: right;}
</style>

Now add the classes to your table, it's easy. 现在将类添加到表中,这很容易。

<table>
<tr><td class="aright">Previous:</td><td>Link 1</td></tr>
<tr><td class="aleft">Next:</td><td>Link 2</td></tr>     
</table>

HTML HTML

<table>
   <tr>
     <td class="right-align">Previous:</td>
     <td class="left-align">Link 1</td>
   </tr>
   <tr>
     <td class="right-align">Next:</td>
     <td class="left-align">Link 2</td>
   </tr>     
</table>​

And your stylesheet.. 和你的样式表..

td.right-align {
    text-align: right;
}

td.left-align {
    text-align: left;
}

Use 使用

<col align=right>

inside the <table> element, to right-align the first column on IE, and <table>元素内,右对齐IE上的第一列,和

td:first-child { text-align: right; }

to do the same on more standards-conforming browsers. 在更符合标准的浏览器上做同样的事情。

Data cells ( td elements) are left-aligned by default, so you need not do anything with the second column. 默认情况下,数据单元格( td元素)是左对齐的,因此您无需对第二列执行任何操作。

Personally I would recommend not using tables and use a CSS solution that being said here's a jsfiddle option. 我个人建议不要使用表并使用CSS解决方案,这里说的是一个jsfiddle选项。 basically the same as other have said but with jsfiddle you can manipulate it and make changes so you can see them immediately. 与其他人说的基本相同但是使用jsfiddle你可以操纵它并进行更改,这样你就可以立即看到它们。

http://jsfiddle.net/rhoenig/rnAVH/ http://jsfiddle.net/rhoenig/rnAVH/

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

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