Is the “content” attribute valid for the <span> tag > if so is it a good practice?

你离开我真会死。 提交于 2019-12-13 07:33:58

问题


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" />&nbsp;</span>
        <span itemprop="model" content="ACOS5T-B2-SCZ" />&nbsp;</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) and meta 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!