Reputation: 24140
Context: Currently, Wt (Web Toolkit) will render a WAnchor object as a <span>
tag rather than an <a>
tag if the link that the anchor should point to, is empty.
However, there are some problems with this approach, notably that it adds unnecessary complexity to CSS styling (when using Twitter Bootstrap, for example).
I've been involved in a discussion with one of the Wt developers proposing a patch to change this behavior: http://redmine.emweb.be/issues/1348
He noted that the current behavior was implemented as a result of certain browsers having problems rendering <a>
tags without an href
attribute (possibly older mobile browsers?).
According to the official HTML specifications, there is no problem with this:
And as far as I am aware...
<a>Link</a> // OK
<a href>Link</a> or <a href="">Link</a> // Undefined? (but common behavior is to navigate to the current URL)
<a href="/items">Link</a> // OK
I also checked the following document with the W3C HTML5 validator:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<a>Example of an anchor with no href attribute.</a>
<a href>Example of an anchor with a null href attribute.</a>
<a href="">Example of an anchor with an empty string href attribute.</a>
<a href="http://www.google.com/">Example of an anchor that links to Google.</a>
</body>
</html>
I received one warning:
Line 9, Column 8: Attribute href without an explicit value seen. The attribute may be dropped by IE7. <a href>Example of an anchor with a null href attribute.</a>
So my question is: are there any known situations where the total lack of an href
attribute on an <a>
tag causes problems in certain browsers? IE? Mobile Safari?
Upvotes: 2
Views: 1989
Reputation: 201528
An a
element without an href
attribute has always been valid in HTML, and there is no reason to expect any browser to have an issue with this. Such an element is effectively similar to span
, i.e. lacks any semantics. Traditionally <a name="...">...</a>
has been used to set up a destination anchor, usually without an href
attribute, and such elements work across browsers, though it is more modern and recommendable to use the id
attribute (on any element).
Using href=""
is formally correct, with an empty URL, which is interpreted as same-document reference.
Using href
without any value is invalid according to current HTML specs. By HTML5 drafts, it can be used in HTML serialization and it is equivalent to href=""
. There is no guarantee that browsers behave that way.
Upvotes: 3