[英]CSS styles does not get applied
我想为表格的第一行应用背景色。 我给该行加了一个特殊的类名。 我还想为表的其余行应用其他颜色。 行颜色不会被应用。
.table { font-family: sans-serif; width: auto; overflow: auto; display: block; border: 1; } /*I want the row with class head to be this color*/ .head { background-color: yellow; } /*I want the rest of the table rows this color*/ .table td { background-color: lightblue; }
<html> <head> <link rel="stylesheet" type="text/css" href="new-style.css"> <meta charset="UTF-8"> </head> <body id="body"> <table align='center' cellspacing=0 cellpadding=1 id="table" border=1 class="table"> <tr id="head" class="head"> <td class="head">Name</td> <td class="head">Type</td> </tr> <tr id="initial-row"> <td width=200> text here</td> <td width=200> text here </td> </tr> <tr id="second-row"> <td width=200> text here </td> <td width=200>text here </td> </tr> </table> </body> </html>
您的问题在于特异性和顺序-因为您将浅蓝色放在td
,所以您也需要用黄色在td
上覆盖它。
然后,您需要将黄色声明移到初始声明下方,因为它具有相同的特异性-这意味着语句的顺序很重要。
最后一件事-从表中删除display:block
,否则将破坏表的布局。
.table { font-family: sans-serif; width: auto; overflow: auto; border: 1; width:100%; /* remove display block from here otherwise your table layout will break */ } /*put this first*/ .table td { background-color: lightblue; } /*override with this*/ .head td { background-color: yellow; }
<html> <head> <link rel="stylesheet" type="text/css" href="new-style.css"> <meta charset="UTF-8"> </head> <body id="body"> <table align='center' cellspacing=0 cellpadding=1 id="table" border=1 class="table"> <tr id="head" class="head"> <td class="head">Name</td> <td class="head">Type</td> </tr> <tr id="initial-row"> <td width=200> text here</td> <td width=200> text here </td> </tr> <tr id="second-row"> <td width=200> text here </td> <td width=200>text here </td> </tr> </table> </body> </html>
让Pete着迷的是 ,我想说的是,如果您想创建一个表头以使用正确的标签<th>
<tr>
<th class="head">Name</th>
<th class="head">Type</th>
</tr>
<th>
标记定义HTML表中的标题单元格。HTML表具有两种单元格:
标题单元格-包含标题信息(使用元素创建)
标准单元格-包含数据(使用元素创建)元素中的文本默认为粗体并居中。
.table { font-family: sans-serif; width: auto; overflow: auto; display: block; border: 1; } /*I want the row with class head to be this color*/ th { background-color: yellow; } /*I want the rest of the table rows this color*/ .table td { background-color: lightblue; }
<html> <head> <link rel="stylesheet" type="text/css" href="new-style.css"> <meta charset="UTF-8"> </head> <body id="body"> <table align='center' cellspacing=0 cellpadding=1 id="table" border=1 class="table"> <tr id="head" class="head"> <th class="head">Name</th> <th class="head">Type</th> </tr> <tr id="initial-row"> <td width=200> text here</td> <td width=200> text here </td> </tr> <tr id="second-row"> <td width=200> text here </td> <td width=200>text here </td> </tr> </table> </body> </html>
一个解决方案是增加的特异性为CSS设置的.head
.table { font-family: sans-serif; width: auto; overflow: auto; display: block; border: 1; } /*I want the row with class head to be this color*/ .table .head { background-color: yellow; } /*I want the rest of the table rows this color*/ .table td { background-color: lightblue; }
<html> <head> <link rel="stylesheet" type="text/css" href="new-style.css"> <meta charset="UTF-8"> </head> <body id="body"> <table align='center' cellspacing=0 cellpadding=1 id="table" border=1 class="table"> <tr id="head" class="head"> <td class="head">Name</td> <td class="head">Type</td> </tr> <tr id="initial-row"> <td width=200> text here</td> <td width=200 > text here </td> </tr> <tr id="second-row"> <td width=200 > text here </td> <td width=200 >text here </td> </tr> </table> </body> </html>
顺便说一句,我刚刚注意到您将table
用作类,也许您应该使用其他名称...更具体
删除tr
的类head
,然后添加!important
。 由于某些原因,即使我重新排列了CSS,颜色也不会没有!important
改变
.table { font-family: sans-serif; width: auto; overflow: auto; display: block; border: 1; } /*I want the row with class head to be this color*/ .head { background-color: yellow !important; } /*I want the rest of the table rows this color*/ .table td { background-color: lightblue; }
<body id="body"> <table align='center' cellspacing=0 cellpadding=1 id="table" border=1 class="table"> <tr id="head"> <td class="head">Name</td> <td class="head">Type</td> </tr> <tr id="initial-row"> <td width=200> text here</td> <td width=200> text here </td> </tr> <tr id="second-row"> <td width=200> text here </td> <td width=200>text here </td> </tr> </table> </body>
<tr id="head" class="head">
<th class="head">Name</td> <!-- change to th (table heading) -->
<th class="head">Type</td>
</tr>
CSS:
th{
background-color: yellow;
}
如@Pete所述,您的特异性不正确。 附带一提,您可以改进HTML标记以同时使用<thead>
,然后CSS可以仅将<thead>
内的<thead>
<th>
元素作为目标。 这对于可访问性更好,因为它明确将“ head”定义为表头。
查看<table>
标记上的w3c文档以获取可访问性@ https://www.w3.org/WAI/tutorials/tables/或有关该标记的一般信息,请查看惊人的Mozilla文档@ https:// developer .mozilla.org / en / docs / Web / HTML / Element / table
像这样:
.table {
font-family: sans-serif;
width: auto;
overflow: auto;
display: block;
border: 1;
}
/*I want the row with class head to be this color*/
thead th {
background-color: yellow;
}
/*I want the rest of the table rows this color*/
tbody td {
background-color: lightblue;
}
<html>
<head>
<link rel="stylesheet" type="text/css" href="new-style.css">
<meta charset="UTF-8">
</head>
<body id="body">
<table align='center' cellspacing=0 cellpadding=1 id="table" border=1 class="table">
<thead>
<tr id="head">
<th>Name</td>
<th>Type</td>
</tr>
</thead>
<tbody>
<tr class="initial-row">
<td>Text here</td>
<td>Text here</td>
</tr>
<tr class="second-row">
<td>Text here</td>
<td>Text here</td>
</tr>
</tbody>
</table>
</body>
</html>
/*I want the rest of the table rows this color*/
.table tr {
background-color: lightblue;
}
/*I want the row with class head to be this color*/
.head {
background-color: yellow;
}
我认为应该是上面的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.