简体   繁体   中英

Which is the best use of lang attribute

HTML syntax - Is one of these versions better or more "correct" than the other?

<p><em lang="fr">Ceci est un paragraphe</em>.  The remainder of this paragraph defaults to  the English language.</p>
<p><em><span lang="fr">Ceci est un paragraphe</span></em>.  The remainder of this paragraph defaults to the English language.</p>

you don't need to use span, lang attribute is a global attribute and it can be added to any element.

Also, we should minimize the use of div and span because they have no special semantic value.

lang is global attribute, it can be used on any element.

You can use it on p, em, span or any other. So if you already have some element around text, you can use same element. Adding extra span will just increase DOM elements count. If you do not use it for anything else - remove it and use 1st sample without extra span .

Couple notices:

For the CSS pseudo-class:lang, two invalid language names are different if their names are different. So while:lang(es) matches both lang="es-ES" and lang="es-419",:lang(xyzzy) would not match lang="xyzzy-Zorp."

Even if the lang attribute is set, it may not be taken into account, as the xml:lang attribute has priority.

xml:lang is deprecated, don't use on anything new.

https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang

https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM