The three layers of the web
Every website is built from three technologies that each have a distinct role. Understanding the boundary between them is fundamental to writing clean, maintainable code.
HTML's job: Structure and content
HTML defines what exists on the page and what it means. It answers the question: what is this?
<article>
<h1>Getting started with HTML</h1>
<p>HTML is the backbone of every web page.</p>
<img src="html-diagram.png" alt="HTML document structure">
</article>
HTML should contain no styling and minimal behaviour. Presentational attributes like bgcolor, align, font, and border are obsolete โ all styling belongs in CSS.
CSS's job: Presentation and layout
CSS controls how things look and where they're positioned. It answers: how does this appear?
article {
max-width: 780px;
margin: 0 auto;
padding: 24px;
}
h1 {
font-family: 'Syne', sans-serif;
font-size: 2.4rem;
color: #1f2937;
}
JavaScript's job: Behaviour and interactivity
JavaScript makes pages dynamic and interactive. It answers: what does this do?
const btn = document.getElementById('formatBtn');
btn.addEventListener('click', function() {
const input = document.getElementById('htmlInput').value;
// format and display result
});
JavaScript should not be used to set styles directly (other than dynamic values like scroll position). Use CSS classes instead โ JavaScript adds/removes classes, CSS defines what those classes look like.
Why separation matters
- Maintainability: Change the look of every button by editing one CSS rule instead of hunting through 50 HTML files
- Performance: CSS and JS files are cached by the browser โ inline styles and scripts aren't
- Accessibility: Screen readers and search engines read HTML structure, not CSS
- Collaboration: Designers can work in CSS while developers write HTML logic simultaneously
When to use what
- Want to show text or an image? โ HTML
- Want to change colour, font, or layout? โ CSS
- Want to animate on hover? โ CSS (transitions/animations)
- Want to hide/show something on click? โ CSS class toggled by JS
- Want to fetch data from an API? โ JavaScript
- Want to validate a form? โ HTML attributes first, JS as fallback