Besides title, meta and link, the head can hold three more elements you should recognise: style, script and base.
The style element#
<style> holds CSS directly in the page:
<head>
<style>
body { font-family: system-ui, sans-serif; color: #2b1d14; }
h1 { color: #6b3e26; }
</style>
</head>This is handy for quick experiments and for tiny pages. For real projects, prefer an external file linked with <link>, which the browser can cache and reuse across pages. A third option, the inline style attribute on one element, is best avoided because it is difficult to override and maintain.
The script element#
JavaScript can be written inline or loaded from a file:
<!-- inline -->
<script>
console.log("Welcome to Brew and Byte");
</script>
<!-- external file -->
<script src="js/main.js"></script>By default, the browser stops reading the page while it downloads and runs a script. If the script is in the head and manipulates elements that have not been parsed yet, it will fail. Two attributes solve this:
| Attribute | Behaviour |
|---|---|
defer | Download while parsing, run after the whole HTML is parsed, in order |
async | Download while parsing, run as soon as ready, in any order |
<script src="js/menu.js" defer></script>
<script src="js/analytics.js" async></script>Use defer for your own scripts that need the page, and async for independent ones such as analytics. A classic alternative is placing the script just before </body>, but defer in the head is the modern approach because the download starts earlier.
The noscript element#
<noscript>
<p>Please enable JavaScript to use the live menu. Our <a href="menu.html">plain menu</a> works without it.</p>
</noscript>Content inside <noscript> is shown only when scripts are disabled or fail. It reminds you to design pages that still work without JavaScript, a principle called progressive enhancement.
The base element#
<base> sets the base URL used to resolve every relative URL in the page. A page may have only one, and it must come before any elements that use relative URLs.
<head>
<base href="https://example.com/cafe/" target="_blank">
</head>
<body>
<!-- resolves to https://example.com/cafe/menu.html -->
<a href="menu.html">Menu</a>
</body>The optional target sets a default for all links. This power is also a hazard: it affects in-page links such as href="#top", which will now point to the base URL. Most modern projects do not use <base>, but you may meet it in older sites, and knowing it exists will save you hours of debugging.
Common mistakes#
- Placing a script that needs the DOM in the head without
defer. - Embedding large amounts of CSS and JavaScript in every page.
- Using two
<base>elements, or placing it after links. - Using inline
styleattributes everywhere. - Writing
<script src="..." />with a self-closing slash, which does not work because the closing tag is required.
Try it yourself#
Create js/hello.js that changes the text of an element with the id greeting. Load it first without defer in the head and check the console for an error, then add defer and confirm it works. Add a <noscript> message. Then experiment with <base> in a test copy of your page and see what happens to your links.
Recap#
Use style for small experiments, and external files for real work. Load scripts with defer or async, provide noscript fallbacks, and use base only when you truly need to change how relative URLs resolve.
Check your understanding
Lesson quiz
OptionalA short quiz to check what you just learned.
- Questions
- 4
- Time limit
- 5 min
- Pass mark
- 70%
- Best score
- -