Skip to content
AITermsLab
–points–day streak

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 compared with JavaScript
CSSJavaScript
Its jobHow the page looksWhat the page does
What the code looks likeStyle rules, like color: navy;Steps, like: when clicked, add 1 to the basket
Without itA plain page: default font, blue links, everything stackedThe 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 sizeYes: if this happens, do that, in as many steps as needed
A programming language?No, a style sheet languageYes
Where it runsIn the browserIn the browser, and on servers too
In a houseThe paint and the furnitureThe 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.

Screen size

Switches

This lesson
rosas-bakery.example
Rosa's Bakery
Basket (0)

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.

Screen size

Switches

This lesson
rosas-bakery.example
Rosa's Bakery
Basket (0)

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.