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

前端 未结 5 2194
北海茫月
北海茫月 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: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.

提交回复
热议问题