The Differences Between HTML, CSS, and JavaScript — What the Web Server Tier Draws

This article is part of the IT Foundations course, which builds up from scratch the practical IT knowledge you need at a minimum for programming and vibe coding.
HTML sets what is on the screen, CSS sets how it looks, and JavaScript sets how it reacts to what you do. Diagrams show how the three become one screen.

This article covers the three that make up the web server tier: HTML, CSS, and JavaScript.

They are written in separate places — index.html, style.css, and script.js — because what they decide is different.

What each of the three files decides
index.htmlstyle.cssscript.jsWhat is therepart types, orderHow it lookscolor/size/layoutHow it reactsto what you doPlace the"Book" buttonMake it a bluerounded buttonOn press,show a messageArrives 1stFetched 2ndFetched 2nd
The left column is the file, the middle is what it decides, and the right is an example for the "Book" button. The far right is the order in which the browser reads them. The three rows decide different things about the same single button.

The three files do not arrive together.

The browser receives all three files, and the browser is what loads them and what runs them.

HTML describes what is on the screen as part types and their order

HTML — the language that describes what is on the screen as part types and their order — goes in index.html.

Each individual part of the screen is an element, the names and settings attached to an element are attributes, and the marks that show where an element begins and ends are tags.

The index.html for the booking form is made of these three.

<form>
  <input type="date">
  <button type="button" id="reserve" class="action">Book</button>
  <p id="message"></p>
</form>

id and class are the names CSS and JavaScript use later to select a part.

An id is used only once per page, while a class can be attached to any number of elements.

Elements can nest, and the button element sits inside the form element.

Nesting inside index.html
html element — the whole page
head element — not shown on screen
  • The page title
  • The file names style.css and script.js
body element — shown on screen
  • The heading "Booking"
form element — the booking form
  • input element — the date field
  • button element — the "Book" button
  • p element — a spot with empty contents
The outer frame is the html element for the whole page. The head element holds information that does not appear on the screen, and only what you write inside the body element appears.

The head element holds the page title and the names of the files it uses, and only what is inside the body element appears on the screen.

index.html only says what is there

All you write in index.html is what is on the screen.

Tags show the type of each part, you write the contents and the order they line up in, and the id and class on an opening tag are the names another file uses later to select that part.

CSS collects how elements look into a separate file

CSS — the language that sets how HTML elements are shown, as rules for color, size, and layout — decides only the appearance.

style.css takes this form.

.action {
  background-color: #2a6f97;
  color: white;
  border-radius: 8px;
}

The .action at the start specifies which elements it applies to.

The three lines inside the braces apply to every element whose class is action.

Inside style.css you line up many rules like this.

The rules lined up inside style.css
style.css
Rules for body
  • Background color of the whole page
  • Text size
Rules for .action — elements whose class is action
  • Background color #2a6f97
  • Text color white
  • Corner radius 8px
Rules for #message — the element whose id is message
  • Text color
  • Top margin
One file holds a rule for each thing it selects, lined up one after another. What is inside each pair of braces is the appearance applied to that target.

When one screen has three buttons, how many places you fix to change the color depends on where you wrote the appearance.

Where the appearance is writtenPlaces to fix to change the colorResult for the three buttons
The style attribute on each buttonFix 3 placesMiss one and the colors do not match
.action in style.cssFix 1 placeAll three change to the same color
#reserve in style.cssFix 1 placeOnly the one with the id changes

You can also write the appearance in an element's style attribute, but then you repeat the same change once per element.

Collect it in style.css and there is one place to fix.

style.css selects by name and decides the appearance

All style.css does is list which part is shown how.

The part is chosen by the class or id name given in index.html, and it does not add parts or rewrite text.

JavaScript reacts to what you do and rewrites the screen

A screen made only of HTML and CSS does not change its contents after it is displayed.

Showing text when the button is pressed is what JavaScript handles in the web server tier.

script.js takes this form.

const button = document.querySelector("#reserve");
button.addEventListener("click", () => {
  document.querySelector("#message").textContent = "Your booking was sent";
});

The document on the first line is the name for the whole page the browser loaded.

The #reserve that follows is how you select the element whose id is reserve, and the mark before the name says whether it is a class or an id.

The three files are connected by the names given in index.html.

Names given in index.html, and how you write them to select
===class="action".actionAppearance rulesin style.cssid="reserve"#reserveClick registrationin script.jsid="message"#messageRewrite targetof script.js
The two boxes joined by a double line are the same name. The left is the name written in index.html, the middle is how it is written when selecting, and the right is where that form is used.

The addEventListener on the second line registers what to do on a click.

The () => { } that follows is the form where you write, inside the braces, the code to run when it is pressed.

An occurrence like this click, where the browser tells the program what the user did, is an event.

Clicks are not the only kind of event.

Whatever the action, the browser is what notices
Press a buttonType text intoan input fieldSubmit a formThe browsernoticesThe registeredcode runsclickinputsubmit
Arrows from the three on the left gather in the center. The text on each arrow is the event name, and the code in script.js is what gets called afterward.

You register in advance what to do on which event.

A color change when the mouse is over an element comes from CSS, and submitting a form is done by the browser.

script.js also reaches the user's browser and its contents can be read, so you do not write values in it that others must not see.

script.js runs only when it is called

All script.js does is register in advance: when this is pressed, do this.

The code the browser calls when it is pressed rewrites parts of the screen, but the contents of index.html do not change, and since script.js reaches the user's browser, you do not write values in it that others must not see.

The browser builds the DOM from HTML, applies CSS and draws, then rewrites it with JavaScript

The browser does not display the HTML text as it is; it assembles the nesting of elements as data.

That is the DOM — data that lines up the elements with their nesting intact.

What you see on the screen is a drawing of this DOM.

The code registered by script.js also waits in the same browser memory as the DOM.

The DOM built in the browser's memory
The user's browser memory
DOM — element nesting built from index.html
body element — the part shown on screen
  • button element — contents are "Book"
  • p element — contents are empty
  • Both still carry their id and class
Code registered by script.js
  • On click, rewrite the contents of p
The same nesting as index.html is assembled inside the browser's memory. The code registered by script.js waits in that same memory.

The three files arrive separately and come together as one DOM inside the browser.

The page you see on the screen is a drawing of this DOM.

Applying the CSS to the DOM, calculating positions and sizes, and drawing is rendering.

What the browser does with the DOM on each arrival and each press
index.htmlarrivesstyle.cssarrivesThe buttonis pressedReloadBuild the DOM fromelement nestingApply rules to theDOM and drawscript.js rewritesthe DOMRebuild the DOMfrom the HTMLNothing isdrawn yetBlue button appearsText appears in pp is empty again
Top to bottom is time order. The left is what happened, the middle is what the browser does then, and the right is the screen afterward. At the very bottom, the rewritten part is gone.

Only the DOM in the browser's memory is rewritten.

The index.html that arrived and the files on the public server stay as they are.

Content you want to keep is sent to the application server tier and stored in the database tier.

How to write JavaScript, and practice running it in the browser, are covered in the JavaScript introduction course.

The screen is a drawing of the DOM

The browser assembles the nesting of parts from the index.html it received, then adds color and size from the rules in style.css and draws it.

What script.js rewrites is that assembled version, so when you reload, it is assembled again from the files that arrived and the rewritten content is gone.

QUIZ

Knowledge Check

Answer each question one by one.

Q1Which file do you fix when you want to change the background color of the booking button?

Q2What do CSS and JavaScript use to select an HTML element?

Q3When JavaScript rewrites the text on a button, which of these changes?