简体   繁体   English

表格中所有单元格的CSS样式

[英]CSS styling for all cells in a table

I want to apply a style to all cells (td elements) inside a given table, and not any other tables. 我想将样式应用于给定表内的所有单元格(td元素),而不应用于任何其他表。 Is there a way to do this in CSS without having to specify a class for each td in the table I want to apply the style to? 有没有一种方法可以在CSS中执行此操作,而不必为要应用样式的表中的每个td指定一个类?

For example, let's say I want to make all cells in a particular table have a padding of 3px on all four edges. 例如,假设我要使特定表中的所有单元格在所有四个边缘上的填充均为3px。 To do this for every td I could declare: 为此,我可以声明:

td { padding: 3px 3px 3px 3px; }

Problem: this applies to every single td on the page. 问题:这适用于页面上的每个td。 I just want to apply to this a single, named table, or alternatively, to all tables of a given class. 我只想将其应用于单个命名表,或者将其应用于给定类的所有表。

Any ideas? 有任何想法吗?

#someTable td { padding: 3px 3px 3px 3px; }

This will apply your style to only a specific table 这会将您的样式仅应用于特定的表格

.someTable td { padding: 3px 3px 3px 3px; }

This will apply the change to all tables with the given class 这会将更改应用于给定类的所有表

例如,将类或id添加到要定位的表中

.yourTableClass td{Your Style here}

As others have answered, having the td element's styles declared as a descendant selector (learn more here ) or a child selector (learn more here ) belonging to a parent selector, instead of declaring them without the hierarchy would solve your problem. 正如其他人回答的那样,将td元素的样式声明为属于父选择器的后代选择器在此处了解更多信息 )或子选择器在此处了解更多信息 ),而不是在没有层次结构的情况下声明它们就可以解决您的问题。

The only thing I'd like to add is that the descendant and child selectors operate with a slight difference as mentioned below in the comments below: 我唯一要添加的是,后代选择器和子选择器的操作稍有不同,如以下注释中所述:

.my_table_class td { padding: 3px; } // will apply to all td elements underneath
                                     // .my-table-class (also to tables nested
                                     // below)


.my_table_class > td { padding: 3px; } // will apply to td elements immediately
                                       // under .my-table-class 

Use This. 用这个。 td.tbl_class{your style;} td.tbl_class {您的风格;}

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

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