CSS子元素选择器 - HTML电子邮件

MeV*_*MeV 3 html css html-email

我试图找到一种方法来改变一个类的CSS,只有它在一个4 td的表中.代码可能会更好地解释我的问题是什么.

HTML

<table class="myclass">
<tr>
  <td>1 <span class="myclass2">xxx</span></td>
  <td>2 <span class="myclass2">xxx</span></td>
  <td>3 <span class="myclass2">xxx</span></td>
</tr>
</table>

<table class="myclass">
<tr>
  <td>1 <span class="myclass2">xxx</span></td>
  <td>2 <span class="myclass2">xxx</span></td>
  <td>3 <span class="myclass2">xxx</span></td>
  <td>4 <span class="myclass2">xxx</span></td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)

CSS

.myclass td:first-child:nth-last-child(3),td:first-child:nth-last-child(3) ~ td {
        background-color:white !important;
    }
.myclass td:first-child:nth-last-child(4),td:first-child:nth-last-child(4) ~ td {
        background-color:red !important;
    }
.myclass2 {
  color:blue;
}
Run Code Online (Sandbox Code Playgroud)

一个JSFiddle准备就绪:JSFIDDLE在这里

我想要做的是改变"myclass2"的样式仅针对表中包含的4个TD的元素,而不是具有3个TD的表.这有可能吗?

Sha*_*ggy 6

这是可能的,是的,但你需要使用多个选择器,检查第一个单元格也是第四个最后一个单元格,第二个单元格也是第三个单元格,依此类推:

.myclass2{
    background:#000;
    color:#fff;
}
td:first-child:nth-last-child(4)>.myclass2,
td:nth-child(2):nth-last-child(3)>.myclass2,
td:nth-child(3):nth-last-child(2)>.myclass2,
td:nth-child(4):last-child>.myclass2{
    background:#f00;
}
Run Code Online (Sandbox Code Playgroud)
<table class="myclass">
    <tr>
        <td>1 <span class="myclass2">xxx</span></td>
        <td>2 <span class="myclass2">xxx</span></td>
        <td>3 <span class="myclass2">xxx</span></td>
    </tr>
</table>
<table class="myclass">
    <tr>
        <td>1 <span class="myclass2">xxx</span></td>
        <td>2 <span class="myclass2">xxx</span></td>
        <td>3 <span class="myclass2">xxx</span></td>
        <td>4 <span class="myclass2">xxx</span></td>
    </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

或者,或者,只需选择第一个孩子也是第四个孩子以及跟随它的任何细胞:

.myclass2{
    background:#000;
    color:#fff;
}
td:first-child:nth-last-child(4)>.myclass2,
td:first-child:nth-last-child(4)~td>.myclass2{
    background:#f00;
}
Run Code Online (Sandbox Code Playgroud)
<table class="myclass">
    <tr>
        <td>1 <span class="myclass2">xxx</span></td>
        <td>2 <span class="myclass2">xxx</span></td>
        <td>3 <span class="myclass2">xxx</span></td>
    </tr>
</table>
<table class="myclass">
    <tr>
        <td>1 <span class="myclass2">xxx</span></td>
        <td>2 <span class="myclass2">xxx</span></td>
        <td>3 <span class="myclass2">xxx</span></td>
        <td>4 <span class="myclass2">xxx</span></td>
    </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

另一种方法,如果你想要定位任意数量的单元格大于3的行,那就是使用否定伪类来选择第一个不是最后一个,第二个最后一个或第三个最后一个单元格的单元格和所有单元格跟着它:

.myclass2{
    background:#000;
    color:#fff;
}
td:first-child:not(:nth-last-child(-n+3))>.myclass2,
td:first-child:not(:nth-last-child(-n+3))~td>.myclass2{
    background:#f00;
}
Run Code Online (Sandbox Code Playgroud)
<table class="myclass">
    <tr>
        <td>1 <span class="myclass2">xxx</span></td>
        <td>2 <span class="myclass2">xxx</span></td>
        <td>3 <span class="myclass2">xxx</span></td>
    </tr>
</table>
<table class="myclass">
    <tr>
        <td>1 <span class="myclass2">xxx</span></td>
        <td>2 <span class="myclass2">xxx</span></td>
        <td>3 <span class="myclass2">xxx</span></td>
        <td>4 <span class="myclass2">xxx</span></td>
    </tr>
</table>
<table class="myclass">
    <tr>
        <td>1 <span class="myclass2">xxx</span></td>
        <td>2 <span class="myclass2">xxx</span></td>
        <td>3 <span class="myclass2">xxx</span></td>
        <td>4 <span class="myclass2">xxx</span></td>
        <td>5 <span class="myclass2">xxx</span></td>
        <td>6 <span class="myclass2">xxx</span></td>
    </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

注意:鉴于某些电子邮件客户端对CSS的支持不足,这些可能不是最佳解决方案.

  • 由于在执行HTML电子邮件时缺乏[支持](https://www.campaignmonitor.com/css/),因此不会对所有电子邮件客户端起作用,建议在"head"上使用内联css而不是"常规css"其他[东西](https://www.campaignmonitor.com/dev-resources/will-it-work/guidelines/) (2认同)