使用属性选择器按类进行选择,而不是使用适当的类选择器

时间:2019-05-31 13:21:14

标签: html css css-selectors

我只是在处理电子邮件html模板,所以遇到了这个问题:

*[class=width100pc] { width: 100% !important; }

我以前从未见过这样的东西。是否有理由使用这种语法按类选择而不是仅仅使用

.width100pc {width: 100% !important;}

我知道CSS在电子邮件客户端中是受限制的,与它有某种联系吗?

2 个答案:

答案 0 :(得分:1)

您将使用*[class=width100pc]来设置width100pc only class 的任何元素的样式。

*[class=width100pc] { color: red; }
<div class="width100pc">Hello</div>

<div class="width100pc another-class">world!</div>

无论其他类别如何,都将使用标准的类别选择器。

.width100pc { color: red; }
<div class="width100pc">Hello</div>

<div class="width100pc another-class">world!</div>

答案 1 :(得分:0)

我想到的一个原因是,在使用此代码的特定环境中不支持类选择器,但是可以通过使用属性选择器来绕过它。

相关问题