CSS 如何为表格定义一个类来控制表格行的样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21291762/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 01:42:41  来源:igfitidea点击:

How to define a class for table to control the style of table rows

cssrowcss-tables

提问by newbie

I'm wondering if I can define a class that could also control the row styles within the table.

我想知道是否可以定义一个还可以控制表中行样式的类。

Lets say I define a class "aTable"

假设我定义了一个类“aTable”

.aTable
{
  width:800px;
  border-spacing:2px;
}

I also want to define the row styles. So when I assign this class to a table, all the rows in that table would follow the design, let say background-color:#e9e9e9;

我还想定义行样式。因此,当我将此类分配给一个表时,该表中的所有行都将遵循设计,比如 background-color:#e9e9e9;

回答by dfsq

You can achieve it like this:

你可以这样实现:

.aTable {
    width: 800px;
    border-spacing: 2px;
}
.aTable tr {
    background-color: #DDD;
}

回答by mboldt

To give a whole row a background-color your would assign the background-color to each cell in that row, i.e.

要为整行提供背景颜色,您可以为该行中的每个单元格分配背景颜色,即

.aTable tr td { background-color: #e9e9e9: }

You can always select specific classes or elements within a css class definition like that.

您始终可以像这样在 css 类定义中选择特定的类或元素。

回答by apohl

I love to use lessto make CSS a lot easier and cleaner. Then you could do this:

我喜欢用更少的东西来让 CSS 变得更简单、更干净。那么你可以这样做:

.aTable {
    width: 800px;
    border-spacing: 2px;
    tr {
        background-color: #DDD;
    }
}