样式<a>没有href属性

kno*_*opx 52 html css css-selectors

是否可以匹配所有链接而无需href通过CSS指定?

例:

<a>Invalid link</a>
Run Code Online (Sandbox Code Playgroud)

我知道可以将所有链接与href匹配,但我只是在寻找相反的东西.

Bol*_*ock 97

要么使用CSS3 :not():

a:not([href]) {
    /* Styles for anchors without href */
}
Run Code Online (Sandbox Code Playgroud)

或者为所有人指定一般样式a,并a[href]为具有该属性的人指定一个样式.

a {
    /* Styles for anchors with and without href */
}

a[href] {
    /* Styles for anchors with href, will override the above */
}
Run Code Online (Sandbox Code Playgroud)

为了获得最佳的浏览器支持(包括古老但不完全被遗忘的浏览器),请使用:link:visited伪类而不是属性选择器(两者的组合将匹配相同a[href]):

a {} /* All anchors */
a:link, a:visited {} /* Override */
Run Code Online (Sandbox Code Playgroud)

另请参阅此答案以深入解释a[href]对比a:link, a:visited.