Is there any way to center certain columns in table?

牧云@^-^@ 提交于 2019-12-10 20:26:19

问题


I'm widely using css formatting and define class for table with subclasses for thead, tbody, tfoot and another level of subclasses for tr, th, td....

In some cases I want to make whole column for table to have class with certain style... but don't know how to do that.

Is there any way (using HTML5, css3, anything else?) to make all cells in certain column of tbody to have the same class?

I can specify class explicitly (manually), but I would like to avoid that kind of duplication.

Any thoughts are welcome.

P.S. Probably I should not bother myself with that and just explicitly specify class for each cell?


回答1:


You can use the nth-child css pseudo-selector to target the middle column of your table.

Example:

#myTable tr td:nth-child(3) {
  text-align: center;
}

This would target the 3rd column in a row, just adjust your X accordingly. It is also possible to use "n" as a variable for every nth child. Check out http://css-tricks.com/how-nth-child-works/ for a comprehensive example.




回答2:


Have a look at the col and colgroup tags. They can be used to define columns. There are some restrictions on which styles apply though.

http://www.quirksmode.org/css/columns.html



来源:https://stackoverflow.com/questions/10255588/is-there-any-way-to-center-certain-columns-in-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!