简体   繁体   English

使用CSS,如何设置单个单元格或单个列的格式?

[英]Using CSS, how do I set the formatting of just a single cell or a single column?

使用CSS,如何设置单个单元格或单个列的格式?

Use the ID selector to target just that one element of the page, in this case, a table cell: 使用ID选择器仅将页面的一个元素作为目标,在这种情况下为表格单元:

 <tr>
  <td id="foo"></td>
 </tr>

Then in the CSS: 然后在CSS中:

#foo
{
 //-- CSS styling goes here just for this element only
}

That hash symbol ( # ) marks that name selector as belonging to an id . 该哈希符号( # )将名称选择器标记为属于id You can further lock it down in the stylesheet to apply only to a TD cell with that ID like so: 您可以将其进一步锁定在样式表中,以仅应用于具有该ID的TD单元,如下所示:

td#foo
{
 //-- CSS styling goes here just for this element only
}

If you add in the rowspan attribute into the TD , you'll be able to turn that into a column and keep the styling you may have set out. 如果将rowspan属性添加到TD ,则可以将其变成一列,并保留您可能设置的样式。

<td id="foo" rowspan="3"></td>

If you mark the CSS selector name with a preceding period (.), like this: 如果将CSS选择器名称标记为前面的句点(。),则如下所示:

.foo
{
  //-- CSS styles
}

that will target class selectors in the HTML and you can style more than one matching element if you apply the CSS class attribute to the tag, like so: 它将以HTML中的class选择器为目标,并且如果将CSS class属性应用于标记,则可以设置多个匹配元素的样式,如下所示:

 <tr>
  <td class="foo"></td>
  <td class="foo"></td>
  <td class="foo"></td>
 </tr>

Don't use CLASS unless it will appear more than once on the page. 除非它在页面上出现多次,否则不要使用CLASS

Give the cell a class name and style the class. 给单元格一个类名,并设置类的样式。

<td class="cellClass">test</td>

.cellClass { color: #a9a9a9 }

检出HTML <col><colgroup>标记,它们使您可以一次将格式应用于整个列或相邻的列组。

You can style the COLGROUP that applies: 您可以设置适用的COLGROUP样式:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
    "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <style type="text/css">
            #special { background:yellow }
        </style>
    </head>
    <body>
        <table>
            <colgroup></colgroup>
            <colgroup id="special"></colgroup>
            <tr>
                <td>a</td>
                <td>1</td>
            </tr>
            <tr>
                <td>b</td>
                <td>2</td>
            </tr>
        </table>
    </body>
</html>

给单元格/列一个类或ID,并使用CSS将格式应用于该单元格/列

For table cells, you'll need to give it some sort of identifier such that you can refer to it. 对于表格单元格,您需要为其提供某种标识符,以便您可以对其进行引用。 Depending on your needs, this will be either a class or an id. 根据您的需要,它可以是类或ID。

<td class="specialCell">...</td>

In your CSS you can then apply different formatting: 然后,您可以在CSS中应用不同的格式:

.specialCell { color: red; }

If you want to apply different styles to a column, there is the <col> tag, but browser support is limited. 如果要对列应用不同的样式,则有<col>标记,但是浏览器支持受到限制。 You're probably better to apply that class to all elements manually (or by using Javascript) 您最好手动(或使用Javascript)将该类应用于所有元素

you can assign two names to a column. 您可以为列分配两个名称。 ex

<div class="foo"></div>
<div class="foo bar"></div>
<div class="foo"></div>

div.foo { color: #fff; }
div.bar { background-color: green; }

perhaps that could solve your problem? 也许可以解决您的问题?

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

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