HTML - What's in the head? Metadata in HTML
Tags: mdn, html, programming
Published: 2022-05-12 04:31:23

## What's in the head? Metadata in HTML

#### What is the HTML head?

The HTML head is the contents of the `<head>` element. <mark>It is the part that is not displayed in the web browser when the page is loaded.</mark> It contains information such as the page `<title>`, links to `CSS`, links to `custom favicons`(favorite icons), <mark>and other metadata(data about the HTML, such as the author, the important keywords that describe the document). Web browsers use information contained in the head to render the HTML document correctly.</mark>
We will introduce some major elements that are included in the head.

- `<title>`
  This sets the title of the page, which is the title that appears in the browser tab the page is loaded in. <mark>The page title is also used to describe the page when it is bookmarked. This contents are also used in search results.</mark>
  <img src="https://developer.mozilla.org/en-US/docs/Learn/HTML/Introduction_to_HTML/The_head_metadata_in_HTML/bookmark-example.png">

* `<meta>`
  <mark>Metadata is data that describes data</mark>, and HTML has an "official" way of adding metadata to a document -- the `<meta>` element.
* Specifying your document's character encoding

  ```html
  <meta charset="utf-8" />
  ```

  <mark>This element specifies the document's character encoding -- the character set that the document is permitted to use.</mark> `utf-8` is a universal character set that includes pretty much any character from any human language. This means that your web page will be able to handle displaying any language:
  <img src="https://developer.mozilla.org/en-US/docs/Learn/HTML/Introduction_to_HTML/The_head_metadata_in_HTML/correct-encoding.png">
  If you set your character encoding to `ISO-8859-1`, for example(the character set for the Latin alphabet), your page rendering may appear all messed up:
  <img src="https://developer.mozilla.org/en-US/docs/Learn/HTML/Introduction_to_HTML/The_head_metadata_in_HTML/bad-encoding.png">
  <mark>Note: Some browsers(like Chrome) automatically fix incorrect encodings, so depending on what browser you use, you may not see this problem. You should still set an encoding of utf-8 on your page anyway to avoid any potential problems in other browsers.</mark>

* <mark>Adding an author and description</mark>
  Many `<meta>` elements include `name` and `content` attributes:

  - <mark>`name` specifies the type of meta element it is; what type of information it contains.</mark>
  - <mark>`content` specifies the actual meta content.</mark>

    ```html
    <meta name="author" content="Chris Mills" />
    <meta
      name="description"
      content="The MDN Web Docs Learning Area aims to provide
    complete beginners to the Web with all they need to know to get
    started with developing web sites and applications."
    />
    ```

    <mark>Specifying an author is beneficial in many ways: it is useful to be able to understand who wrote the page, if you have any questions about the content and you would like to contact them.</mark> Some content management systems have facilities to automatically extract page author information and make it available for such purposes.
    <mark>Specifying a description that includes keywords relating to the content of your page is useful as it has the potential to make your page appear higher in relevant searches performed in search engines.</mark>

* Adding custom icons to your site
  A favicon can be added to your page by:
  - Saving it in the same directory as the site's index page, saved in `.ico` format(most browsers will support favicons in more common formats like `.gif` or `.png`, but using the `.ico` format will ensure it works as far back as Internet Explorer 6.)
  - Adding the following line into your HTML's `<head>` block to reference it:
    ```html
    <link rel="icon" href="favicon.ico" type="image/x-icon" />
    ```
* Applying CSS and JavaScript to HTML

  ```html
  <!-- CSS -->
  <link rel="stylesheet" href="my-css-file.css" />
  <!-- JavaScript -->
  <script src="my-js-file.js" defer></script>
  ```

- <mark>Setting the primary language of the document</mark>
  You can(and really should) set the language of your page. This can be done by adding the lang attribute to the opening HTML tag.

  ```html
  <html lang="en-US"></html>
  ```

  You can also set subsections of your document to be recognized as different languages.

  ```html
  <p>Japanese example: <span lang="ja">ご飯が熱い。</span>.</p>
  ```

  This is useful in many ways. Your HTML document will be indexed more effectively by search engines if its language is set(allowing it to appear correctly in language-specific results, for example), and it is useful to people with visual impairments using screen readers(for exmple, the word "six" exists in both French and English, but is pronounced differently.)
  These codes are defined by the <a href="https://en.wikipedia.org/wiki/ISO_639-1">ISO 639-1</a> standard. You can find more about them in <a href="https://www.w3.org/International/articles/language-tags/">Language tags in HTML and XML</a>.