简体   繁体   English

使用CSS和HTML的表格单元格的背景色

[英]background color of table cells using css and html

I've coded a table I want it to be like this : even and odd rows have different colors and first row and column have the same color ; 我已经编写了一张表,希望它像这样:偶数和奇数行具有不同的颜色,而第一行和列具有相同的颜色; I used colgroup for coloring first column but it doesn't work. 我使用colgroup为第一列着色,但是它不起作用。

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Untitled Document</title>

    <style type="text/css">
        tr.odd{background-color:#f9f;}
        tr.even{background-color:#fcf;}
    </style>

</head>

<body>
    <table width="200" border="1">
        <colgroup span="1" style="background:purple;"></colgroup>

        <tr style="background:purple;">
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <tr class="odd">
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <tr class="even">
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <tr class="odd">
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
    </table>

</body>
</html>

Maybe this is what you wanted. 也许这就是您想要的。

CSS CSS

table tr td:first-of-type {background-color: purple !important;}

 <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> <style type="text/css"> tr.odd{background-color:#f9f;} tr.even{background-color:#fcf;} table tr td:first-of-type {background-color: purple !important;} </style> </head> <body> <table width="200" border="1"> <colgroup span="1" style="background:purple;"></colgroup> <tr style="background:purple;"> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> </tr> <tr class="odd"> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> </tr> <tr class="even"> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> </tr> <tr class="odd"> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> </tr> </table> </body> </html> 

You can use the even and odd rules of the nth-child pseudoclass 您可以使用第n个子伪类的偶数和奇数规则

tr:nth-child(even) {background: #FCF}
tr:nth-child(odd) {background: #F9F}

Followed by: 其次是:

td:first-of-type,
tr:first-of-type {background: purple}

You can use CSS nth-child (IE9+), which also removes the need for even and odd classes in the HTML. 您可以使用CSS nth-child (IE9 +),它也消除了HTML中对偶数和奇数类的需求。

  <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> <style type="text/css"> td {background-color:#f9f;} tr:nth-child(even) > td {background-color:#fcf;} tr > td:first-child {background-color:purple;} </style> </head> <body> <table width="200" border="1"> <tr> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> </tr> <tr> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> </tr> <tr> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> </tr> <tr> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> </tr> </table> </body> </html> 

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

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