HTML, CSS and JavaScript: Who Does What
Understand the three languages of web pages, how a browser combines them and where each kind of job properly belongs.
Key takeaways
- HTML provides structure and meaning, CSS provides presentation and JavaScript provides behavior.
- A page works best when the content is readable from HTML alone and the other two layers improve it.
- Using the right HTML element gives keyboard and screen reader support without extra code.
- CSS handles layout, responsiveness and many animations that once needed scripts.
- Anything running in the browser can be inspected and changed, so security checks belong on the server.
HTML says what is on a page and what each piece means. CSS says how it looks and where it sits. JavaScript says how it behaves when something happens. A heading is HTML, its color and size are CSS, and a button that opens a menu when pressed gets its behavior from JavaScript or, increasingly, from features built into the browser.
Keeping those jobs separate gives you pages that are easier to change, quicker to load and usable by more people.
The three layers
A common comparison is a house: HTML is the frame and rooms, CSS the paint and furniture arrangement, JavaScript the switches and appliances. The comparison breaks down in places, but it captures the order of dependence. Paint needs a wall.
HTML: structure and meaning
HTML (HyperText Markup Language) wraps content in elements, written with tags:
<article>
<h1>Repotting a fern</h1>
<p>Do this in spring, when growth starts.</p>
<ol>
<li>Water the plant the day before.</li>
<li>Choose a pot one size up.</li>
</ol>
<a href="/guides/soil.html">Which soil to use</a>
</article> The tags carry meaning. h1 is the main heading, ol a numbered list, a a link. Browsers, search engines and screen readers rely on that meaning. Extra information goes in attributes, such as href for a link's destination or alt for an image's description.
Choose elements for what they are, not for how they look by default. Use button for actions, a for navigation, nav, main and footer for page regions, label for form fields and table for tabular data. A div is a generic box with no meaning, useful for styling and a poor substitute for a button.
CSS: presentation and layout
CSS (Cascading Style Sheets) consists of rules. Each has a selector, which picks elements, and declarations, which set properties:
article {
max-width: 40rem;
margin: 0 auto;
}
h1 {
color: #14532d;
font-size: 2rem;
}
a:hover {
text-decoration-thickness: 3px;
} "Cascading" refers to how the browser resolves competing rules. Importance, origin, specificity of the selector and order of appearance all play a part. Many beginner frustrations come from a more specific rule quietly winning, and the browser's developer tools show which rule applied and which were overridden.
Every element is also a box with content, padding, border and margin. Flexbox arranges items along a line, and Grid arranges them in rows and columns. Media queries apply rules only under certain conditions:
@media (max-width: 600px) {
h1 { font-size: 1.5rem; }
} That is a building block of responsive web design. When picking colors, check legibility with a contrast checker; color contrast and readability explains what the ratios mean.
JavaScript: behavior
JavaScript is a programming language that runs in the browser. It can react to events, change the page and talk to servers.
const button = document.querySelector("#show-tips");
const tips = document.querySelector("#tips");
button.addEventListener("click", () => {
tips.hidden = !tips.hidden;
button.setAttribute("aria-expanded", String(!tips.hidden));
}); This finds two elements, waits for a click, toggles the visibility of one and tells assistive technology about the change. The browser represents the page as a tree of objects that scripts can read and edit, described in the DOM explained. Loading data without reloading the page is covered in fetch and asynchronous requests.
JavaScript also runs on servers through environments such as Node.js, which is a different setting with access to files and databases. Java is an unrelated language despite the similar name.
How the browser puts them together
The order is more interleaved than a list suggests. A plain script tag in the middle of a page pauses HTML parsing while the script loads and runs. Adding defer lets parsing continue and runs the script after the document is parsed:
<script src="app.js" defer></script> How a browser turns code into pixels follows the whole process.
Putting them in one page
The usual arrangement is three files linked from the HTML:
<head>
<link rel="stylesheet" href="style.css">
<script src="app.js" defer></script>
</head> You can write CSS in a style element or an attribute, and JavaScript inline, but separate files are easier to maintain, can be cached across pages and work better with a strict security policy.
Which one should do the job?
Use the simplest layer that can do it.
| Task | Best home |
|---|---|
| A heading, list or form field | HTML |
| A show and hide section | HTML details and summary |
| Required and email-format checks | HTML attributes, repeated on the server |
| Layout that changes on small screens | CSS |
| Hover and focus effects | CSS |
| Filtering a table as someone types | JavaScript |
| Saving data or checking a password | Server-side code |
Native elements bring behavior you would otherwise have to rebuild: a real button can be focused and activated from the keyboard, while a clickable div cannot without extra work. Keyboard navigation and focus shows what to test.
Progressive enhancement
Progressive enhancement means making the core content and tasks work with HTML, then adding style and script on top. If a script fails to load on a poor connection, the article is still readable and the form can still be submitted.
Applications that build the whole page with JavaScript can be excellent, especially for rich tools such as editors. They carry costs for first load time and for resilience, which many teams reduce by generating HTML on the server or at build time. For a mostly read site, that heavy approach is rarely needed.
A small example with all three
Suppose you want a newsletter sign-up. HTML supplies a form that already works on its own:
<form action="/subscribe" method="post">
<label for="email">Email address</label>
<input id="email" name="email" type="email" required autocomplete="email">
<button type="submit">Subscribe</button>
</form> The type and required attributes give you basic checking, a suitable phone keyboard and browser autofill. The label tells every user, including those with screen readers, what the field is for.
CSS then makes it comfortable to use:
input, button { font: inherit; padding: 0.6rem 0.8rem; }
input:focus-visible, button:focus-visible { outline: 3px solid #1d4ed8; } JavaScript could send the form without leaving the page and show a thank-you message. If you add that, handle failure too: tell the user when the request did not go through, and keep what they typed. The server at /subscribe must still validate the address, because anyone can send it a request without using your form.
When something looks wrong
Open the browser's developer tools, commonly with F12 on Windows and Linux or Option, Command and I on a Mac (some browsers require enabling a developer menu first).
- Elements or Inspector shows the live tree and the styles applied to each item.
- Console lists script errors with file and line numbers.
- Network shows which files loaded, how large they were and which failed.
Structure problems often come from an unclosed or wrongly nested tag. A style that does nothing is usually overridden, misspelled or attached to a selector that matches no element. A script that does nothing often stopped at an earlier error.
Frameworks and libraries
Frameworks such as React, Vue or Svelte organize larger interfaces, and CSS frameworks supply ready styles. All of them end up as HTML, CSS and JavaScript in the browser. Learning the three basics first means you can understand what a tool produces and fix it when it surprises you.
The server is a separate world
Code in the browser runs on the visitor's device. They can read it, alter it and send requests without it. Use JavaScript validation to help honest users, and repeat every check on the server, which also handles databases, payments and secrets. Front end, back end and full stack draws that line.
A learning order that works
- Write a page in plain HTML with headings, lists, links, an image and a form.
- Style it with CSS: type, color, spacing, then Flexbox and Grid.
- Make it work on a narrow screen.
- Add one small JavaScript behavior, such as the toggle above.
- Open the developer tools and experiment. Changes there are temporary, so you cannot break the real site.
- Publish it on a static host and keep improving.
Small pages you finish teach more than large ones you abandon. View the source of simple sites you like, but write your own code instead of pasting what you do not understand.