01 — The question
The question
Learning outcomes
By the end of this lesson, you can set a base URL, use module scripts, verify an external script with integrity, add resource hints, recognize custom elements and slots, and add app metadata.
02 — The idea
The idea
Terminology
base: sets the base URL for every relative URL in the document.
Module script: a script with type="module". It is deferred by default and runs in strict mode.
Subresource integrity (integrity): a hash that the browser checks against a fetched file before running it.
Resource hint: preload or modulepreload, which asks the browser to fetch a resource early.
Custom element: a new element with a name that contains a hyphen, defined by script.
Slot: a placeholder inside a custom element that receives the content placed by the page.
base changes every relative URL
A base element sets where relative links and resources resolve. There can be only one, and it must come before any element that uses a URL. It is useful when a page is served from an unusual path, but it also changes every link on the page, so use it carefully.
Module scripts
Module scripts can import other modules and are deferred by default. Use type="module" for new scripts. Older browsers used a separate nomodule script for legacy support; modern browsers all support modules, so new pages rarely need it.
Integrity and CORS
When you load a script or stylesheet from another server, integrity lets the browser check the file against a hash. The attribute crossorigin="anonymous" lets the browser make the request with CORS, which integrity requires for cross-origin files. Only use integrity with a hash you generated from the exact file you intend to load.
Resource hints
preload fetches a resource that the current page needs soon, such as a font. It needs as to name the type. modulepreload fetches a module before the script that imports it. Hints are optional and can waste bandwidth if overused, so add them only for resources you know are needed early.
Custom elements and slots
A custom element extends HTML with a new element such as a price display. Its name must contain a hyphen, and script defines its behavior. A shadow root can hold its internal structure, and slot places content from the page inside it. Ordinary HTML should still carry the content, because a custom element that never loads leaves nothing behind.
App metadata
A manifest describes an installable web app: its name, icons, and colors. theme-color tints the browser toolbar. apple-touch-icon gives a home-screen icon on iOS. These belong in the head and only need the values for your site.
03 — Worked example
Worked example
Head metadata for a bakery site
The example below uses placeholder paths and an example domain. The integrity hash is left as a placeholder, because a real hash must come from the file itself.
Change the code, then look at the preview. The preview runs in a sandbox.
Run the code to see the preview.
The preview appears here.
Custom elements need a definition
Without script, rina-price is an unknown element. The browser displays its text, so the page still reads, which is why the content must live in the document rather than only in the script.
04 — Try it
Try it
Tiered exercises
Work through the levels in order.
Level 1: name
Decide which feature fits each job: a base URL for a site under a subpath, a checked script from a CDN, a font needed early, and an installable app icon.
Level 2: integrity
Explain why integrity without crossorigin fails for a cross-origin script, and what each attribute contributes.
Level 3: review
Review a head section with a base element placed after a link. Find the problem and fix it.
Complete solutions
05 — Check yourself
Check yourself
Recap and exit questions
The platform gives each page an explicit base URL, module scripts, integrity checks, resource hints, custom elements, and app metadata. Verify external files you do not control.
06 — What’s next
What’s next
Official references
MDN: base element
MDN: Modules
MDN: Subresource Integrity
MDN: link rel=preload
MDN: Using custom elements
MDN: Web app manifests
WHATWG HTML Living Standard: The base element
The idea to take with you
The platform gives each page an explicit base URL, module scripts, optional integrity checks, resource hints, custom elements, and app metadata. Use each one for its job, and verify any external file you do not control.
A small marker for this page. Sign in to save your progress.