CSS 如何在第一列中应用文本左对齐并在其他列中应用文本右对齐
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11984767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to apply text-align left in the first column and text-align right in the others
提问by Koala7
I have a Table to style, how can I apply text-align:left;
just for the first column and text-align:right;
for the other columns?
我有一个表格样式,我如何text-align:left;
只申请第一列和text-align:right;
其他列?
Here my Table example:
这是我的表示例:
http://jsfiddle.net/gianlucaguarini/hAv7P/
http://jsfiddle.net/gianlucaguarini/hAv7P/
At the moment the text-align is left for all the columns.
目前,所有列都保留了 text-align 。
采纳答案by keaukraine
You need to specify the following CSS:
您需要指定以下 CSS:
.table td,
.table th
{
text-align:left;
}
#right,
.table td + td,
.table th + th
{
text-align:right;
}
Example here: http://jsfiddle.net/mWeYM/
这里的例子:http: //jsfiddle.net/mWeYM/
You can read more about adjacent siblings CSS selector here: http://www.w3.org/TR/CSS2/selector.html#adjacent-selectors
您可以在此处阅读有关相邻兄弟 CSS 选择器的更多信息:http: //www.w3.org/TR/CSS2/selector.html#adjacent-selectors
回答by cloudfeet
You might want to look at the :first-child
pseudo-class (W3C definition) - this is exactly the kind of thing it was designed for. You could use it like this:
您可能想查看:first-child
伪类(W3C 定义)——这正是它的设计目的。你可以这样使用它:
.table td {
text-align: right;
}
.table td:first-child {
text-align: left;
}?
The :first-child
pseudo-class means the selector only matches the first <td>
in each <tr>
.
的:first-child
伪类是指只选择匹配的第一个<td>
中的每个<tr>
。
Example here: http://jsfiddle.net/xv5Cn/1/
这里的例子:http: //jsfiddle.net/xv5Cn/1/
回答by Sandeep Rao
Hey Manna i have updated your fiddle just follow this link:
嘿,曼娜,我已经更新了你的小提琴,只需点击以下链接:
All you need to do is add a class to the which yu want aligned and then add css as in the fiddle
您需要做的就是将一个类添加到您想要对齐的类中,然后像小提琴一样添加 css