是否可以在不指定每行中的类的情况下对表列进行全局文本对齐? [英] Is it possible to globally text-align a table column without specifying a class in each row?

查看:97
本文介绍了是否可以在不指定每行中的类的情况下对表列进行全局文本对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道什么是最好的方式使这更高效,也许与jQuery。我确定解决方案不符合ie7,甚至缺乏支持ie8可能会确定如果必要。

Wondering what the best way to make this more efficient, perhaps with jQuery. I am ok with solutions not compliant with ie7, even lack of support for ie8 might be ok if necessary.

<style type="text/css">
.cal {text-align:center}
.ral {text-align:right}
</style>
<table>
 <th>
  <td class="cal">center</td>
  <td>left</td>
  <td class="cal">center</td>
  <td>left</td>
  <td class="ral">right</td>
 </th>
 <tr>
  <td class="cal">center</td>
  <td>left</td>
  <td class="cal">center</td>
  <td>left</td>
  <td class="ral">right</td>
 </tr>
 <tr>
  <td class="cal">center</td>
  <td>left</td>
  <td class="cal">center</td>
  <td>left</td>
  <td class="ral">right</td>
 </tr>
</table>


推荐答案

或者(我的方法是使用第n个子选择器

Alternatively (and the way I do it) is using the nth child selector.

tr td:nth-child(odd) {
    text-align: center;
}

tr td:nth-child(5) {
    text-align: right;
}

它与IE 8.0不兼容,但它比JavaScript

it's not compatible with IE 8.0, but it is a lot simpler than a JavaScript based solution.

这篇关于是否可以在不指定每行中的类的情况下对表列进行全局文本对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆