CSS vs JavaScript: what's the difference?
CSS controls how a web page looks: colours, fonts, spacing and layout. JavaScript controls what it does: it reacts when you click, type or scroll, like opening a menu or updating a basket.
Last updated
What each one means
- CSS
- CSS is the code that controls how a web page looks: colours, fonts, spacing and layout.
- JavaScript
- JavaScript is the code that makes a web page react: opening menus, updating a basket or checking a form as you type.
Side by side
| CSS | JavaScript | |
|---|---|---|
| Its job | How the page looks | What the page does |
| What the code looks like | Style rules, like color: navy; | Steps, like: when clicked, add 1 to the basket |
| Without it | A plain page: default font, blue links, everything stacked | The page still looks right, but buttons and menus stop working |
| Can it make decisions? | Barely: it reacts to a few things, like hover or screen size | Yes: if this happens, do that, in as many steps as needed |
| A programming language? | No, a style sheet language | Yes |
| Where it runs | In the browser | In the browser, and on servers too |
| In a house | The paint and the furniture | The electricity |
An example: a menu button
On a shop's website, CSS gives the menu button its colour and rounded corners, and keeps the list of categories hidden. When you tap the button, JavaScript notices and shows the list. Switch JavaScript off and the button still looks perfect, but tapping it does nothing.
See CSS: switch it off
Switch CSS off on the toy bakery page and it falls back to the browser's plain defaults: a Times-style font, blue underlined links and everything stacked. Switch it back on and the colours, spacing and card layout return.
Switches
Fresh bread, baked at 6am
Order by 10am, collect after lunch.
Sourdough loaf
£4.00
Cinnamon bun
£2.50
Lemon cake
£3.00
The code behind it (simplified)
HTML: what's on the page
<h1>Fresh bread, baked at 6am</h1>
<p>Order by 10am, collect after lunch.</p>
<li class="card">
<img src="sourdough.svg" alt="Sourdough loaf">
<h2>Sourdough loaf</h2>
<p>£4.00</p>
<button>Add to basket</button>
</li>CSS: how it looks
.card {
border: 2px solid navy;
border-radius: 20px;
}
button {
background: yellow;
font-weight: bold;
}JavaScript: how it reacts
button.onclick = () => {
basket = basket + 1;
showBasketCount(basket);
};Switch CSS off to see how much of the look comes from it.
See JavaScript: switch it off
Press Add to basket on the toy bakery page and the basket count goes up. Switch JavaScript off and press it again: the button looks exactly the same, but nothing happens, and the menu button stops opening too.
Switches
Fresh bread, baked at 6am
Order by 10am, collect after lunch.
Sourdough loaf
£4.00
Cinnamon bun
£2.50
Lemon cake
£3.00
The code behind it (simplified)
HTML: what's on the page
<h1>Fresh bread, baked at 6am</h1>
<p>Order by 10am, collect after lunch.</p>
<li class="card">
<img src="sourdough.svg" alt="Sourdough loaf">
<h2>Sourdough loaf</h2>
<p>£4.00</p>
<button>Add to basket</button>
</li>CSS: how it looks
.card {
border: 2px solid navy;
border-radius: 20px;
}
button {
background: yellow;
font-weight: bold;
}JavaScript: how it reacts
button.onclick = () => {
basket = basket + 1;
showBasketCount(basket);
};Press Add to basket, then switch JavaScript off and press it again.
Common questions
Can CSS animate things without JavaScript?
Yes. CSS can fade, slide and change colour on hover or when the page loads. JavaScript is needed when the change depends on what you've done, like a counter going up.
Should I learn CSS or JavaScript first?
CSS, right after HTML. It's quicker to pick up and you see results straight away. JavaScript makes more sense once you know what you want to make react.
Do I need both for a website?
For a simple site, HTML and CSS are enough. You need JavaScript once the page has to react: menus, forms that check as you type, baskets or live updates.