问题
Is the "content" attribute valid for span
tag? If so is it a good practice?
I'll be applying microdata (schema.org) on my site pages.
I want to add microdata on some elements of my page.
This is my current code:
<span itemscope itemtype="http://schema.org/Product">
<a itemprop="url" class="list-items" href="/product/286/cryptomate64-usb-cryptographic-token/" title="CryptoMate64 USB Cryptographic Token">
<span itemprop="name">CryptoMate64 USB Cryptographic Token</span>
<span class="hidden">
<span itemprop="productid"/>286</span>
<span itemprop="model" content="ACOS5T-B2-SCZ" />ACOS5T-B2-SCZ</span>
</span>
</a>
</span>
As you can see, I have a div
with class "hidden" there because the model and id shouldn't be displayed on the page.
I want to minify the code by doing this:
<span itemscope itemtype="http://schema.org/Product">
<a itemprop="url" class="list-items" href="/product/286/cryptomate64-usb-cryptographic-token/" title="CryptoMate64 USB Cryptographic Token">
<span itemprop="name">CryptoMate64 USB Cryptographic Token</span>
<span itemprop="productid" content="286" /> </span>
<span itemprop="model" content="ACOS5T-B2-SCZ" /> </span>
</a>
</span>
I can use meta
instead of span
so that the content is not visible. But I think it won't be a good practice since I'll be having a lot of items. What can you suggest? Thanks.
回答1:
I tested your updated code with Google's richsnippets test tool and it works fine with the content
attribute on the span
.
However, this isn't good practice since there is no content inside the last 2 spans anyway. It is perfectly appropriate in this case to use metas.
<meta itemprop="productid" content="286">
<meta itemprop="model" content="ACOS5T-B2-SCZ">
回答2:
No, it’s not valid.
Neither HTML5 nor Microdata define a content
attribute for span
. (RDFa does, but you are not using it.)
If you want to markup content with Microdata that should not be visible, either
- use usual HTML and hide it with CSS, or
- use
link
elements (for URIs) andmeta
elements (for anything else); both elements are allowed in the body, and typically hidden by default browser stylesheets.
I’d prefer the latter variant (meta
/link
), but it’s not possible everytime (e.g., if you need to add new items with itemscope
).
来源:https://stackoverflow.com/questions/25134818/is-the-content-attribute-valid-for-the-span-tag-if-so-is-it-a-good-practic