: Does itemprop have a different meaning on links vs. span tags? I'm using Schema.org markup for a Person, but I get inconsistent results in Google's Rich Snippet Testing Tool when using an <a>
I'm using Schema.org markup for a Person, but I get inconsistent results in Google's Rich Snippet Testing Tool when using an <a> tag vs. a <span> tag. Take these two examples:
<span itemprop="author" itemscope itemtype="http://schema.org/Person">
<a href="http://example.com/user/username">
<span itemprop="name">username</span>
</a>
</span>
<span itemprop="author" itemscope itemtype="http://schema.org/Person">
<a href="http://example.com/user/username" itemprop="name">username</a>
</span>
The first one shows the name is parsed as username, but when I tried the second one to save on HTML, it shows the username is example.com/user/username - i.e. the URL of the link.
Why does this happen, and is it correct behaviour? I can't find any resources that make a distinction between itemprop on a link vs. a span.
More posts by @Cofer257
1 Comments
Sorted by latest first Latest Oldest Best
What the spec says
Yes, that behaviour is correct. Section 5.4 of the W3C Microdata spec describes which value gets used.
In most cases, the element’s content gets used as itemprop value, but in some cases, an attribute’s value gets used as itemprop value.
This is the case for these six attributes¹: content², src, href, data, value, datetime.
What this means in practice
If you want to use the content of an a element (instead of the URL in its href attribute) as property value, you have to use/add a suitable parent or child element:
<a href="ignored-url"><span itemprop="property">property value</span></a>
<span itemprop="property"><a href="ignored-url">property value</a></span>
If you want to provide a URL as property value, you must use itemprop on one of the URL property elements (i.e., elements that can³ have a href, src, or data attribute). So this would be invalid (unless you want to provide a string as value which just looks like a URL):
<!-- INVALID --> <span itemprop="property">http://example.com/foo</span>
¹ It must be valid for the element to have this attribute. So for an (invalid!) <div href="" itemprop=""></div>, the element content gets used, not the href value.
² In WHATWG’s and W3C’s old spec of Microdata, only the meta element can have the content attribute, but the new W3C Microdata spec (currently a Working Draft) allows content on any element. If an element has a content attribute and also one of the other listed attributes, content gets used.
³ Microdata makes it invalid to have one of the URL property elements without their respective URL attribute (i.e., href, src, data), except for the link element (but it’s already required by HTML to have the href attribute).
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.