7

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 http://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.

  • And this is why I prefer to use JSON-LD. – Simon Hayter Dec 24 '17 at 15:38
  • 1
    @Simon why repeat all your content in a JSON block? It makes sense to me to use the existing HTML. – DisgruntledGoat Dec 28 '17 at 20:43
  • Because often writing your content around schema is not natural because more than often you start organising and displaying page content for Google and Bing and not Visitors. JSON-LD is easier to manage and does not suffer from having to work your UX around your code. Furthermore, JSON-LD can be added in Google Tag Manager making it even faster and saves your server having to serve this so called SAME CONTENT :) – Simon Hayter Dec 28 '17 at 21:45
  • That’s never happened to me. I add attributes to existing elements, occasionally add wrapper elements, but I’ve never needed to change the design or UX to accommodate Schema. – DisgruntledGoat Dec 28 '17 at 22:47
9

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).

  • As always(!), Excellent! Cheers!! – closetnoc Dec 23 '17 at 1:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.