Is an empty href valid?
Solution 1
Although this question is already answered (tl;dr: yes, an empty href
value is valid), none of the existing answers references the relevant specifications.
An empty string can’t be a URI. However, the href
attribute doesn’t only take URIs as value, but also URI references. An empty string may be a URI reference.
HTML 4.01
HTML 4.01 uses RFC 2396, where it says in section 4.2. Same-document References (bold emphasis mine):
A URI reference that does not contain a URI is a reference to the current document. In other words, an empty URI reference within a document is interpreted as a reference to the start of that document, and a reference containing only a fragment identifier is a reference to the identified fragment of that document.
RFC 2396 is obsoleted by RFC 3986 (which is currently IETF’s URI standard), which essentially says the same.
HTML5
HTML5 uses (valid URL potentially surrounded by spaces → valid URL) W3C’s URL spec, which has been discontinued. WHATWG’s URL Standard should be used instead (see the last section).
HTML 5.1
HTML 5.1 uses (valid URL potentially surrounded by spaces → valid URL) WHATWG’s URL Standard (see the next section).
WHATWG HTML
WHATWG’s HTML uses (valid URL potentially surrounded by spaces) the definition of valid URL string from WHATWG’s URL Standard, where it says that it can be a relative-URL-with-fragment string, which must at least be a relative-URL string, which can be a path-relative-scheme-less-URL string, which is a path-relative-URL string that doesn’t start with a scheme string followed by :
, and its definition says (bold emphasis mine):
A path-relative-URL string must be zero or more URL-path-segment strings, separated from each other by U+002F (/), and not start with U+002F (/).
Solution 2
It is valid.
However, standard practice is to use href="#"
or sometimes href="javascript:;"
.
Solution 3
As others have said, it is valid.
There are some downsides to each approach though:
href="#"
adds an extra entry to the browser history (which is annoying when e.g. back-buttoning).
href=""
reloads the page
href="javascript:;"
does not seem to have any problems (other than looking messy and meaningless) - anyone know of any?
Solution 4
While it may be completely valid HTML to not include an href, especially with an onclick handler, there are some things to consider: it will not be keyboard-focusable without having a tabindex value set. Furthermore, this will be inaccessible to screenreader software using Internet Explorer, as IE will report through the accessibility interfaces that any anchor element without an href attribute as not-focusable, regardless of whether the tabindex has been set.
So while the following may be completely valid:
<a class="arrow">Link content</a>
It's far better to explicitly add a null-effect href attribute
<a href="javascript:void(0);" class="arrow">Link content</a>
For full support of all users, if you're using the class with CSS to render an image, you should also include some text content, such as the title attribute to provide a textual description of what's going on.
<a href="javascript:void(0);" class="arrow" title="Go to linked content">Link content</a>
Solution 5
The current HTML5 draft also allows ommitting the href attribute completely.
If the a element has no href attribute, then the element represents a placeholder for where a link might otherwise have been placed, if it had been relevant.
To answer your question: Yes it's valid.
Related videos on Youtube
matthew
Updated on May 31, 2020Comments
-
matthew almost 4 years
One of our web developers uses the following html as a placeholder for styling a drop down list.
<a href="" class="arrow"></a>
Is this considered anchor tag valid?
Since there is no href value, it shows up as broken on some of our link checker reports.
-
ZhongYu about 10 yearsIt does not work in IE though! Well it works in IE but with a completely different behavior, against the spec. So it is valid per spec, but not in practice :(((
-
Nate almost 9 yearsExpanding on what bayou.io said, IE links to the directory above the document: stackoverflow.com/questions/7966791/…
-
Ringo over 7 yearsYes, an empty href in older versions of IE (7/8, etc.) could have bad consequences, such as directory listing. There is much documented on the subject if you google it
-
Ringo over 7 yearsfor example: gtmetrix.com/avoid-empty-src-or-href.html. Never mind just old browsers, it affects current IE, Edge, and also Webkit browsers.
-
-
Programmer97 about 13 yearsAs Stated in RFC 2396: A URI reference that does not contain a URI is a reference to the current document. In other words, an empty URI reference within a document is interpreted as a reference to the start of that document,
-
diachedelic over 11 yearsAnother interesting one is href="//:0", which will not make a request to the server nor leave any history.
-
David Grenier over 11 yearsThe //:0 makes some of my images fail to load in Chrome. Seems Chrome interprets that as receiving a cancel command.
-
Deckard over 11 yearswhen I click
href="#"
, focus moves to top of the page in IE, so I thkinkhref="javascript:;"
is better -
c24w about 11 yearsAlthough:
<a href></a>
→ Attribute href without an explicit value seen. The attribute may be dropped by IE7. -
tosh almost 11 yearshref="#" is an anti-pattern. It adds an extra entry to the browser history and in some cases makes the browser scroll to top, you can just leave out the href attribute if you are not using it. it will default to "" and therefore reload the page if not prevented.
-
dumazy over 10 yearshref:"javascript:;" was just what I needed to make stuff work in both Android an iOS webviews
-
rinat.io almost 10 yearsI like this solution too. Do you know of any other disadvantages except IE7 behavior?
-
Gui Prá over 9 years@tosh, no, I'm pretty sure if you leave out the href attribute, the <a> element isn't styled as a link, isn't focusable, and doesn't create a link as <a href=""> or <a href> does. If it does work for you, could you share a JSFiddle showing it?
-
tosh over 9 years@n2liquid-GuilhermeVieira thanks a lot for your precision. I did not mean that omitting the href attribute is the solution. I did not word it right. I meant to use the empty string as the href attribute value.
-
Gui Prá over 9 years@tosh, you know what's funny? Apparently since HTML5, omitting the attribute should work that way, see w3.org/TR/html5/text-level-semantics.html#the-a-element (Thanks halfdan for submitting that answer below.) Apparently this hasn't been implemented by vendors yet, and since it's still just candidate recommendation, it's hard to tell whether it'll be kept in the final standard or whether vendors will comply before that.
-
Alex from Jitbit about 9 yearsyes, BUT - an
<a>
element with nohref
is styled dirrefently in some browsers. For example, ":hover" styles do not work in Chrome (and other weird things) -
Nate almost 9 yearsIE links to the directory above the document: stackoverflow.com/questions/7966791/…
-
Nate almost 9 yearsIE links to the directory above the document: stackoverflow.com/questions/7966791/…
-
Kobi over 8 yearsAn
<a>
tag without ahref
represents something else, not a link (traditionally an anchor). Also, there is a difference between an optional attribute and an attribute that can be empty. -
Mark Good over 8 yearsI have not tested this, but I suspect that href="javascript:;" would violate the Content Security Policy that disallows inline JavaScript. If you intend to turn on this policy, I would use one of the alternatives.
-
Daniel Sokolowski about 8 yearsThanks for mentioning that omitting
href
will mess with accessibility interfaces. -
aij about 8 yearsIndeed. Unfortunately, as you quoted,leaving off the
href
makes it not a hyperlink, which makes theonclick
functionality inaccessible to keyboard users. -
Michel Hébert about 7 yearsI'd also add – from an accessibility perspective – that setting the
href
value to anything else than a relative/absolute path will cause screen reader issues. Avoid using a hash/pound symbol as workaround for this issue as it is not meant for this. Screen readers may (currently VoiceOver does) announce your link to be a fragment identifier (link to somewhere in the current page) and is also valid from a standards point-of-view. see w3.org/2001/tag/2011/01/HashInURI-20110115#NakedHash -
EML about 4 yearsI'm finding that
href=""
is reloading the page in a new tab - no idea why. I have to explicitly addtarget="_self"
-
Robert Siemer about 4 yearsThis is illegal syntax. According to the spec “[Empty Attribute] syntax is permitted only for boolean attributes.”
-
doug65536 over 2 yearsWhy take so much pity on screen readers though? They have if statements too.