交替颜色HTML表PHP

时间:2015-03-28 02:52:46

标签: php css html-table

我试图让我的桌子换色,但我遇到了一些困难。

 if ($i % 2 == 0)
    $color = "grey";
    else
    $color = "white"; $i++;
    $table .= "<tr style=backround-color=$color>"; 

这不起作用。我也尝试了这个,但它也没有用。

$table .= "<tr:nth-child(even) {background: #CCC};  tr:nth-child(odd) {background: #FFF}; >"; 

2 个答案:

答案 0 :(得分:2)

您拼错了background而您在CSS中没有使用=,而是使用:。我还在属性值周围添加了引号,因为这是最佳做法:

$table .= "<tr style='background-color:$color'>";

您问题的最后一行甚至不接近有效的HTML或CSS。看起来有点整洁。

答案 1 :(得分:0)

我发现了一个有趣的链接here,可以满足您的需求。我将链接中的代码放入jsfiddle,这是我从链接中获得的CSS样式:

.TFtableCol{
    width:100%; 
    border-collapse:collapse; 
}
.TFtableCol td{ 
    padding:7px; border:#4e95f4 1px solid;
}
/* improve visual readability for IE8 and below */
.TFtableCol tr{
    background: #b8d1f3;
}
/*  Define the background color for all the ODD table columns  */
.TFtableCol tr td:nth-child(odd){ 
    background: #b8d1f3;
}
/*  Define the background color for all the EVEN table columns  */
.TFtableCol tr td:nth-child(even){
    background: #dae5f4;
}