HTML Tags: Metadata
This chapter is quite technical/advanced. If this is your first time learning HTML, just remember these three:
<title>...</title>
is used to change the browser's title bar.<link rel="stylesheet" href="...">
is used to load external CSS file<style>...</title>
is used write CSS directly in HTML file
This is the list of HTML tags you would normally put inside <head>
.
Tag | Type | Description |
---|---|---|
<base> | Other | Defines the base URL for all relative URLs in the page |
<title> | Other | Defines the document's title shown in the browser's title bar |
<link> | Other | Specifies relationship with other document. (Normally used to load CSS.) |
<meta> | Other | Represents metadata that cannot be represented with other tags. |
<style> | Other | Defines the style (CSS) for the page. |
<base>
Defines the base URL for all relative URLs in the page (you will rarely use this).
Note that relative URLs are URLs without the protocol and host, such as /some/path
, ./some/path
(same as some/path
), and ../some/path
.
href=[...]
— the base URLtarget=[...]
— where to open it (read here)
<html>
<head>
<base href="https://another-site.com" />
</head>
<body>
<!-- Will point to https://another-site.com/#anchor -->
<a href="#anchor">Click here!!</a>
</body>
</html>
<title>
Defines the document's title shown in the browser's title bar
<head>
<title>About Us - Pelicin!</title>
</head>
<link>
Specifies relationship with other document. (Normally used to load CSS.)
Common attributeshref=[...]
— URL of the linked resourcerel=[...]
— relation of the linked resource to this document. Click here for the more elaborate list. Common values:stylesheet
(it's a CSS for this page)icon
(it's a favicon for this page)canonical
(it's the preferred URL of this page; i.e., to tell search engines that duplicates of this page should use this URL instead)alternate
(it's the alternate version of the page; e.g. in different language)preload
(this resource is needed soon, fetch it ASAP)prefetch
(this resource may be needed soon, fetch it when idle)
media=[...]
— only load this resource if the media query matches (e.g.print
orscreen and (min-width: 600px)
)type=[...]
— MIME type of the linked resource
integrity=[...]
— hash of resource to make sure it hasn't been tamperedcrossorigin=[...]
— read herereferrerpolicy=[...]
— read here
rel="alternate"
hreflang=[...]
— indicates the language of the linked resource (e.g.en-US
)
rel="preload"
OR rel="prefetch"
as=[...]
— what you would have put as "rel" if you didn't put preload/prefetch
rel="preload"
AND as="image"
imagesrcset=[...]
—srcset
value of an<img srcset="..." sizes="...">
you want to preloadimagesizes=[...]
—sizes
value of an<img srcset="..." sizes="...">
you want to preload
<head>
<!-- Loading CSS and favicon -->
<link rel="stylesheet" href="style.css" />
<link rel="icon" href="favicon.ico" />
<!-- The following URL is the preferred version of this page -->
<link rel="canonical" href="https://www.mycompany.com/en-id/" />
<!-- We also have alternate versions of this page -->
<link rel="alternate" hreflang="id-id" href="https://www.mycompany.com/id-id/" />
<link rel="alternate" hreflang="en-us" href="https://www.mycompany.com/en-us/" />
</head>
<meta>
Represents metadata that cannot be represented with other tags.
You may want to take a look at Open Graph meta specification to configure how social media sites display the thumbnail of your page.
Possible "key-value" pair attributesname=[...]
— Key likeauthor
ordescription
(see here for the list)http-equiv=[...]
— Key to simulate HTTP response header, likecontent-security-policy
orcontent-type
content=[...]
— value forname
orhttp-equiv
charset=[...]
— What character encoding to use. Onlyutf-8
is valid.
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- Author and description -->
<!-- Description will appear on search engine result -->
<meta name="author" content="Ferdinand Antonius" />
<meta name="description" content="Pelicin makes flight booking easier" />
<!-- Open graph (for social media thumbnail) -->
<meta property="og:title" content="Pelicin" />
<meta property="og:description" content="Pelicin makes flight booking easier." />
<meta property="og:image" content="http://pelicin.netlify.app/thumbnail.jpg" />
<meta property="og:url" content="http://pelicin.netlify.app/index.html" />
</head>
<style>
Defines the style (CSS) for the page. You can place this anywhere (even in <body>
).
media=[...]
— The media to which the style is applied, e.g.screen and (max-width: 600px)
nonce=[...]
— Nonce for CSP
<head>
<!-- Make texts red by default -->
<style>
body {
color: red;
}
</style>
<!-- On small screens (width <= 600), they become blue -->
<style media="screen and (max-width: 600px)">
body {
color: blue;
}
</style>
</head>
References
- HTML elements reference (MDN) — https://developer.mozilla.org/en-US/docs/Web/HTML/Element
- Link types (MDN) — https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types
- What are the differences between html preload and prefetch? (Stack Overflow) — https://stackoverflow.com/questions/52764401/what-are-the-differences-between-html-preload-and-prefetch
- How to preload responsive images with imagesizes and imagesrcset — https://www.stefanjudis.com/today-i-learned/how-to-preload-responsive-images-with-imagesizes-and-imagesrcset/
- HTTP-Equiv: What Is It Used For? — https://www.keycdn.com/support/http-equiv
- The Essential Meta Tags for Social Media (CSS-Tricks) — https://css-tricks.com/essential-meta-tags-social-media/