What is the 'lang' attribute of the <html> tag used for?

前端 未结 5 2181
北海茫月
北海茫月 2020-12-07 22:59

In HTML, it\'s good to have a lang attribute in , e.g. .

How is this useful?

If th

相关标签:
5条回答
  • 2020-12-07 23:07

    The difference between lang and custom attributes is that lang is inherited, so even a child element of an element with attribute lang=en can be selected with the selector div:lang(en){}.

    0 讨论(0)
  • 2020-12-07 23:28

    As far as I can tell, for a single-language website (hopefully you can surmise its utility for multi-language websites), the only real, actual, tangible use is that it makes the 'hyphens' CSS property work as expected... which is not much, but more than nothing. (I'm afraid I haven't actually tested this in a browser, however, which is something we should all do to know things for sure.)

    Via: http://blog.adrianroselli.com/2015/01/on-use-of-lang-attribute.html (which is also full of irrelevant "reasons" to use it, save that mentioned).

    0 讨论(0)
  • 2020-12-07 23:28

    The lang attribute tells the client what language the document (or part of document) is written in. This is useful for any software which cares about language.

    A key use is for accessibility. It is mentioned in WCAG:

    This Success Criterion helps:

    • people who use screen readers or other technologies that convert text into synthetic speech;
    • people who find it difficult to read written material with fluency and accuracy, such as recognizing characters and alphabets or decoding words;
    • people with certain cognitive, language and learning disabilities who use text-to-speech software
    • people who rely on captions for synchronized media.

    Adrian Roselli describes some benefits:

    Hyphens

    By using lang, you get the benefits of hyphen support in your (modern) browser that you otherwise would not get (assuming you use hyphens: auto in your CSS).

    Accessibility

    At the very least, lang is a benefit for screen reader users, particularly when your users don’t have the same primary language as your site. It allows proper pronunciation and inflection when the page is spoken.

    … as well as referencing WCAG and pointing at this document from the W3C which lists benefits such as being able to write CSS which styles elements based on the language they are written in (so different fonts can be used for different languages), automatically selecting fonts with the right version of a glyph for a language, to aid search engines, spell checks and translation tools, to help speech synthesizers and Braille translators, and for custom scripting.

    0 讨论(0)
  • 2020-12-07 23:32

    You asked "how is this useful".

    "The <lang=> attribute can be used to declare the language of a Web page or a portion of a Web page. This is meant to assist search engine spiders, page formatting and screen reader technology"

    Source: http://symbolcodes.tlt.psu.edu/web/tips/langtag.html

    No mention of translation - but often a search engine spider will not want to parse through a document "in the wrong language" - its index file will grow (lots of new words), and the results will not be useful to the user (who cannot read the language, and who is using the wrong search terms).

    The advent of smart translation technology (like Google's, referred to above) means that some search engines can see a page in one language, translate it, and figure out that someone searching for "cow" may be interested in this page that mentions "vache" and has <lang="fr">.

    0 讨论(0)
  • 2020-12-07 23:33

    I am quoting this from W3C:

    Declaring language in HTML

    Always use a language attribute on the html tag to declare the default language of the text in the page. When the page contains content in another language, add a language attribute to an element surrounding that content.

    Use the lang attribute for pages served as HTML, and the xml:lang attribute for pages served as XML. For XHTML 1.x and HTML5 polyglot documents, use both together.

    Use language tags from the IANA Language Subtag Registry.

    Also a good read is Why use the language attribute?.

    0 讨论(0)
提交回复
热议问题