HTML, CSS or JavaScript?

Three languages, three jobs. How to tell them apart at a glance — and which one to open when something breaks.

Three jobs

Spelled outIts jobIn a house
HTMLHyperText Markup Languagestructure — what each part isthe walls, rooms and doors
CSSCascading Style Sheetspresentation — what it looks likepaint, furniture, light
JavaScript— no relation to Javabehaviour — what it doesplumbing and electricity

A house with no paint is still a house. A page with no CSS still works.


How to tell them apart

Look for the shape first. Each language has a shape you can recognise from across the room.

HTML is tags in angle brackets. An opening tag, content, a closing tag.

<a href="about.html" class="action">About me</a>

CSS is a selector, then braces, with property: value; pairs inside.

.quote {
  padding: 1.5rem;
  border-left: 4px solid #c2410c;
}

JavaScript is instructions. Words like let, const, function and document, round brackets, and arrows =>.

document.getElementById('go')
  .addEventListener('click', () => alert('Hello'));

The clues, side by side

ClueHTMLCSSJavaScript
Shape<tag>…</tag>selector { … }statements, ( ), =>
Typical wordshref src alt class idcolor margin display font-sizelet const function document fetch
A comment<!-- … -->/* … */// … or /* … */
Its own file.html.css.js
Written inside a page—<style>…</style> or style="…"<script>…</script>
Linked from a page—<link rel="stylesheet" href="style.css"><script src="nav.js"></script>

/* … */ is the one comment that belongs to two languages: it works in CSS and in JavaScript. <!-- … --> is only HTML. // is only JavaScript — in CSS it is not a comment at all, and it breaks the rule after it.


Where they meet

One HTML file can hold all three languages. This is where it gets confusing — and where exam questions like to look.

<head>
  <link rel="stylesheet" href="style.css">  <!-- HTML that points to a CSS file -->
  <style>
    p { color: #333; }                        /* CSS, inside an HTML element */
  </style>
</head>
<body>
  <p style="color: red;">Warning</p>          <!-- HTML, with CSS in its style attribute -->
  <script src="nav.js"></script>              <!-- HTML that points to a JavaScript file -->
  <script>
    let likes = 0;                            // JavaScript, inside an HTML element
  </script>
</body>

Look at the comments: each one is written in the language of the place it sits in.

It works the other way round too. JavaScript often carries the other two languages inside its quotes:

document.querySelector('.quote').style.color = 'red';
document.getElementById('site-header').innerHTML = '<nav>…</nav>';

Both lines are JavaScript. '.quote' is a CSS selector and color a CSS property, used from JavaScript. '<nav>…</nav>' is HTML, held as text.

Name the language of the line, not of what sits inside its quotes.


Why they are kept apart

Separation of concerns.

  • Change the look of every page by editing one CSS file
  • A screen reader reads the HTML and ignores the paint
  • When something breaks, you know which of the three to open

And they are built in that order — progressive enhancement:

  1. It works. Plain HTML, readable by anything — a screen reader, a search engine.
  2. It looks right. CSS on top. Better, but nothing depends on it.
  3. It feels good. JavaScript last. If it fails, the page still works.

When it breaks: which one?

The first question when something goes wrong is not how do I fix it, but which layer is it in.

What you seeWhere to look
text in the wrong place, tags showing on the pageHTML — an unclosed tag or quote
a paragraph suddenly big and boldHTML — a heading that was never closed
no styling at all, plain black on whitethe <link> to the stylesheet — the file name or the path
one CSS rule ignored, the rest fineCSS — a typo, a missing ; or a missing unit. DevTools → Styles shows it struck through
a button that looks right but does nothingJavaScript — open the Console and read the red message
the nav and the footer are missingthe page was opened with file:// — fetch needs a server. Open it with Live Server
an image is missingthe path, almost always. DevTools → Network shows a red 404
中文 shows as ���HTML — the charset line is missing
a tiny page on a phoneHTML — the viewport line is missing

Practice

1 · Which language?

Name the language of each line. If a line holds two languages, name both. The code is shown without colours, as it will be on paper.

1   <img src="images/hero.webp" alt="The campus at sunrise">
2   main { max-width: 48rem; margin: 0 auto; }
3   let count = 0;
4   <!-- the nav used to be here -->
5   nav a:hover { color: #c2410c; }
6   <p style="font-weight: bold;">Open every day</p>
7   fetch('nav.html').then(r => r.text());
8   /* the content column */
9   <script src="likes.js"></script>
10  document.querySelector('.quote').classList.add('liked');
Show the answers
  1. HTML — an image element.
  2. CSS — a rule for main.
  3. JavaScript — a variable.
  4. HTML — an HTML comment.
  5. CSS — a rule for the links inside the nav, while the mouse is over them.
  6. HTML and CSS — an HTML paragraph, with a CSS declaration in its style attribute.
  7. JavaScript — it fetches an HTML file, but the line itself is JavaScript.
  8. CSS or JavaScript — /* … */ is a comment in both. On its own you cannot tell; look at what surrounds it.
  9. HTML — an element that loads a JavaScript file.
  10. JavaScript — '.quote' inside it is a CSS selector, used from JavaScript.

2 · Which layer?

Five things have gone wrong on five different sites. For each one, say which layer you would look at first — and why.

  1. Every page is styled except contact.html, which is plain black on white.
  2. On the about page, everything below the first heading is huge and bold.
  3. The "random quote" button does nothing. The page looks perfect.
  4. The site looks fine on your laptop, but on the live site every photo is missing.
  5. You changed the colour in the .quote rule in style.css, but the quotes stay the same colour.
Show the answers
  1. The <link> in the head of contact.html. The other pages are styled, so the CSS file itself is fine — this page is not loading it. Check its href: spelling, capitals, path.
  2. HTML. The heading was never closed, so everything after it became part of the heading.
  3. JavaScript. The appearance is fine; the behaviour is not. Open the Console and read the red message.
  4. The paths. Almost certainly capitals — Hero.JPG on disk, hero.jpg in the HTML — or a path to your own disk, such as C:\…. Your laptop ignores capitals; the server does not. DevTools → Network shows each image as a 404.
  5. CSS. Check that rule: a misspelled property, a missing ; on the line above, or a selector that does not match the class in the HTML. DevTools → Styles shows a broken declaration struck through.