Styling <a> without href attribute

HtmlCssCss Selectors

Html Problem Overview

Is it possible to match all links without href specified via CSS?


<a>Invalid link</a>

I know its possible to match all links with href but I'm just looking for the opposite.

Html Solutions

Solution 1 - Html

Either use CSS3's :not():

a:not([href]) {
    /* Styles for anchors without href */

Or specify a general style for all a, and one for a[href] to override it for those with the attribute.

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

a[href] {
    /* Styles for anchors with href, will override the above */

For the best browser support (includes ancient but not quite forgotten browsers), use the :link and :visited pseudo-classes instead of the attribute selector (combining both will match the same as a[href]):

a {} /* All anchors */
a:link, a:visited {} /* Override */

Also see this answer for an in-depth explanation of a[href] versus a:link, a:visited.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionknoopxView Question on Stackoverflow
Solution 1 - HtmlBoltClockView Answer on Stackoverflow