我看过很多 CSS 样式表,我遇到过至少两种样式化元素的方法。我想知道的是 CSS 元素的样式如何不同,如下面的样式
a:link {
color: green;
background-color: transparent;
text-decoration: none;
}
<a href="#">Link</a>
到
.link {
color: green;
background-color: transparent;
text-decoration: none;
}
<a href="#" class="link">Link1</a>
一个比另一个有什么优势?
最佳答案
这两种情况都没有优势。它们执行两个独立的功能,因此有点像苹果到橘子。
如果您只想将其应用于未访问的链接,请使用:link
。
如果您不关心链接是否被访问过,请使用.link
。
:link
选择器仅适用于尚未访问过的链接。
根据 MDN :
The :link CSS pseudo-class represents links (such as an anchor) that have not yet been visited.
.link
选择器是一个简单的 class selector将应用于任何应用了 link
类的元素。
根据 MDN :
The CSS class selector matches elements based on the contents of their class attribute.
关于html - 为什么有两种样式元素样式,有优势吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45986866/