【问题标题】:HTML outer and inner table with different border color using CSS使用CSS具有不同边框颜色的HTML外部和内部表格
【发布时间】:2013-07-27 11:31:26
【问题描述】:

我在另一个表中有一个 HTML 表。两个表格的样式都在 CSS 中定义。我想要具有不同边框颜色的内表和外表。但不知何故,内表正在采用外表的边框颜色。下面是代码-

HTML:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="common.css">
</head>
<body>
    <table width="100%" class="OuterTableStyle1"><tr><td>
    <table width="100%" class="CommonTableStyle1" >
            <tr>
                <th>Title 1</th>
                <th>Title 2</th>
            </tr>
            <tr>
                 <td>Data 1</td>
                 <td>Data 2</td>
            </tr>
    </table>
    </td></tr></table>
</body>
</html>

CSS:

/*======= Start : Common data table style =========*/
table.CommonTableStyle1 td
   {
   vertical-align:middle;
   padding:5px;
   font-size:11px;
   font-family:Arial;
   font-weight:normal;
   color:#000000;
   }

table.CommonTableStyle1 th, table.CommonTableStyle1 td
   {
   border: 1px solid #525272    ;
   }

table.CommonTableStyle1
   {
   border-collapse:collapse;
   }
/*======= End : Common data table style =========*/


/*=== Start : This table is used as border of another scrollable table ===*/
table.OuterTableStyle1 
   {
   border-collapse:collapse;
   }
table.OuterTableStyle1 th, table.OuterTableStyle1 td
   {
   border: 1px solid red;
   }
/*=== End : This table is used as border of another scrollable table ===*/

请帮忙。

[编辑的 CSS]

/*======= Start : Common data table style =========*/
table.CommonTableStyle1 td
   {
   vertical-align:middle;
   padding:5px;
   font-size:11px;
   font-family:Arial;
   font-weight:normal;
   color:#000000;
   }

table.CommonTableStyle1 th, table.CommonTableStyle1 td
   {
   border: 1px solid #525272    ;
   }

table.CommonTableStyle1
   {
   border-collapse:collapse;
   }
/*======= End : Common data table style =========*/


/*=== Start : This table is used as border of another scrollable table ===*/
table.OuterTableStyle1 
   {
   border-collapse:collapse;
   border: 1px solid red;
   }
/*=== End : This table is used as border of another scrollable table ===*/

【问题讨论】:

    标签: html css html-table


    【解决方案1】:

    选择器

    table.CommonTableStyle1 table, th, td
    

    不能正常工作,因为表中没有“CommonTableStyle1”类的表。

    我认为你的本意是:

    table.CommonTableStyle1 th, table.CommonTableStyle1 td
    

    fiddle

    此外,最底部的选择器确实有效,但不是您想象的那样。 th, td 部分与上述部分具有相同的特异性,因此所有ths 和tds 都将采用这种样式,因为这是后来出现的。不过现在可以了,因为我们已经把上面那一个的特异性放大了。

    【讨论】:

    • 您好,Lister 先生,您的回答对我有用。但是,如果我按照我的问题中所示更改代码,同样的事情会再次发生。你能帮我解决这个问题吗?
    • 已编辑的 css 部分中的代码有效。感谢 Lister 先生指出我的错误。
    猜你喜欢
    • 1970-01-01
    • 2011-08-18
    • 2012-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多