简体   繁体   English

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

[英]CSS child's element selector - HTML Email

I am trying to find a way to change the css for a class, only if it's inside a table with 4 td's. 我试图找到一种方法来改变一个类的CSS,只有它在一个4 td的表中。 The code will probably explain better what is my problem. 代码可能会更好地解释我的问题是什么。

HTML 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>

CSS 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;
}

a JSFiddle is ready: JSFIDDLE HERE 一个JSFiddle准备就绪: JSFIDDLE在这里

What I am trying to do is changing the style of "myclass2" only for the elements included in the table with 4 TDs, not with the table with 3 TDs. 我想要做的是改变“myclass2”的样式仅针对表中包含的4个TD的元素,而不是具有3个TD的表。 Is that possible at all? 这有可能吗?

It's possible, yes, but you would need to use multiple selectors, checking the that first cell was also the 4th last cell, the 2nd also the 3rd last, and so on: 这是可能的,是的,但你需要使用多个选择器,检查第一个单元格也是第四个最后一个单元格,第二个单元格也是第三个单元格,依此类推:

 .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; } 
 <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> 

Or, alternatively, just select the first child that is also the fourth last child and any cells that follow it: 或者,或者,只需选择第一个孩子也是第四个孩子以及跟随它的任何细胞:

 .myclass2{ background:#000; color:#fff; } td:first-child:nth-last-child(4)>.myclass2, td:first-child:nth-last-child(4)~td>.myclass2{ background:#f00; } 
 <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> 

Another way to do it, if you wanted to target rows with any number of cells greater than 3 would be to use the negation pseudo-class to select the first cell that isn't the last, 2nd last or 3rd last cell and all cells that follow it: 另一种方法,如果你想要定位任意数量大于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; } 
 <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> 

NOTE: These may not be optimal solutions for you, given the poor support some e-mail clients have for CSS . 注意:鉴于某些电子邮件客户端对CSS的支持不足 ,这些可能不是最佳解决方案。

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

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