The Creative Coder’s Field Guide

Everything You Need to Survive and Thrive with p5.js

Creative Coding · Week 8

View-SourceHow the web works, plus HTML and CSS for beginners

Every sketch you’ve made has lived inside a web page — you just never had to look at the page itself. This week we pull back the curtain: how a browser fetches a page from another computer, and the two languages that page is written in. HTML says what’s on the page. CSS says how it looks.

HTML + CSS · any browser + editor.p5js.org · Understand → Know → Do
01The idea

The web is files, sent on request

Strip away the apps and the animations, and the web is a simple arrangement: some computers hold files, other computers ask for them, and a program called a browser turns those files into the pages you see.

When you open a website, nothing “goes” anywhere. Your browser sends a short message to another computer saying please send me this file. That computer sends the file back. Your browser reads it, top to bottom, and draws the result on your screen — the same way p5 reads your sketch top to bottom and draws the result on the canvas (Week 1).

The files are plain text. You could open one in any text editor and read it. That’s the most important thing to take from this week: a web page is just a text file written in a language the browser understands. Once you can read that language, every site on the internet becomes something you can open up and learn from.

02Requests

Clients, servers, and the round trip

Every page load is a conversation between two computers with two jobs:

  • The client is the computer that asks. Usually that’s your browser.
  • The server is the computer that answers. It stores the site’s files and sends copies to anyone who asks.
CLIENT your browser 3 · renders the page SERVER a computer that stores the site index.html 1 · request: "send /index.html" 2 · response: 200 OK + the file One page often needs several trips: the HTML, then its CSS, images, scripts.
The rules for this conversation are called HTTP (HyperText Transfer Protocol). The s in https means the conversation is encrypted so nobody in between can read it.

The URL is the address of one file

The text in your address bar, the URL, tells the browser exactly what to ask for and who to ask.

https://example.com/gallery/index.html protocol how to talk domain which computer path which file, in which folder
The path works like folders on your own computer. If a URL ends in a folder name with no file, most servers send that folder’s index.html by default.

A domain is a nickname. Computers on the internet find each other by numeric addresses (like 93.184.215.14). The domain name is a human-friendly label, and a lookup service called DNS translates it to the number before the request goes out. Think of it as the internet’s contacts app.

03Three languages

HTML, CSS, and the JavaScript you already know

A browser understands three languages, and each one has a single job. Keeping those jobs separate is what makes web pages easy to change.

LanguageIts jobThink of it asFile
HTMLwhat’s on the page: headings, paragraphs, images, linksthe nouns.html
CSShow it looks: color, size, spacing, layoutthe adjectives.css
JavaScriptwhat it does: reacting, changing, animatingthe verbs.js

You’ve been writing the third one all semester. Look at the file list in the p5 web editor (click the > arrow above your code): next to sketch.js sit index.html and style.css. They were there from your very first sketch. Open index.html and you’ll find something like this:

index.html — from a new p5 sketch

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://cdn.jsdelivr.net/npm/p5@.../p5.js"></script>
    <link rel="stylesheet" type="text/css" href="style.css">
    <meta charset="utf-8" />
  </head>
  <body>
    <main></main>
    <script src="sketch.js"></script>   <!-- your code! -->
  </body>
</html>

That’s the secret behind every sketch: an HTML page loads the p5 library, loads a stylesheet, and then loads your sketch.js. p5 creates a <canvas> element and adds it to the page. Your sketch was always a guest inside a web page; this week you learn to build the house.

04Elements

Tags, attributes, and content

HTML is a markup language: you take ordinary text and wrap pieces of it in tags that say what each piece is. A heading, a paragraph, a link. One wrapped piece, start to finish, is called an element.

opening tag <a href="week4.html">Loops</a> attribute name="value" content closing tag note the / one element: a link that reads “Loops”
The browser never shows the tags themselves. It shows the content, styled and behaving according to what the tags say it is: here, clickable text that opens week4.html.
  • Tags live in angle brackets. Most come in pairs: an opening tag <p> and a closing tag </p>, which is the same name with a slash in front.
  • Attributes go inside the opening tag and add extra information, always as name="value". A link needs to know where it goes (href); an image needs to know which file to show (src).
  • Content is everything between the two tags: text, or more elements.

Some elements have nothing inside

An image doesn’t wrap any text; it just is the picture. Elements like this are called empty (or void) elements, and they have no closing tag:

<img src="cat.jpg" alt="A grey cat asleep on a keyboard">

The alt attribute describes the picture in words, for people using screen readers and for anyone whose image fails to load. Always include it.

Close what you open. A forgotten closing tag is the HTML version of a missing } (Week 3). Browsers are forgiving and will guess where you meant the element to end, but they often guess wrong, and then half your page turns bold or vanishes into a link.

05The document

Every page has the same skeleton

Elements can sit inside other elements, the same way a loop body sits inside a loop’s braces (Week 4). That’s called nesting, and every HTML page is built from the same nested skeleton:

index.html

<!DOCTYPE html>                    <!-- "this is a modern HTML page" -->
<html lang="en">                   <!-- wraps everything -->
  <head>                           <!-- info ABOUT the page; not shown -->
    <meta charset="utf-8">
    <title>My First Page</title>   <!-- text on the browser tab -->
  </head>
  <body>                           <!-- everything you SEE goes here -->
    <h1>Hello, web</h1>
    <p>This is my first page.</p>
    <img src="cat.jpg" alt="A grey cat">
  </body>
</html>

The split to remember is head vs. body. The head holds information about the page: its title, its character set, which stylesheets and scripts to load. The body holds everything that actually appears in the window.

The browser reads it as a tree

When the browser reads your file, it builds a family tree of elements. Each element is a parent to the elements nested directly inside it, and those are its children. This tree is called the DOM (Document Object Model), and it’s what your browser’s developer tools show you (section 10).

html head body title h1 p img not shown on the page drawn in the window, top to bottom
Indentation in the file mirrors this tree: each level of nesting gets one more indent. Same rule as your p5 code (Week 2’s Style section), and Tidy Code works on .html files in the p5 editor too.
06Common tags

Know about: the everyday elements

HTML has more than a hundred elements, but a handful cover most pages. Pick tags for what the content means, not how you want it to look. A heading is an <h1> because it’s the main heading, not because you want big text; CSS handles the size.

TagWhat it isExample
<h1>–<h6>headings, most to least important<h1>My Portfolio</h1>
<p>a paragraph of text<p>I make sketches.</p>
<a>a link; href says where it goes<a href="about.html">About</a>
<img>an image (empty element)<img src="me.jpg" alt="Me">
<ul> / <ol>a bulleted / numbered listwraps <li> items
<li>one list item<li>Loops</li>
<strong> / <em>important / emphasized text<strong>Due Friday</strong>
<div>a generic box for grouping things<div class="card">…</div>
<span>a generic wrapper for a few words<span class="pink">hi</span>
<canvas>a drawing surface; p5 makes one for youcreated by createCanvas()

Elements fall into two rough families. Block elements (h1, p, div, ul) start on a new line and stretch the full width, stacking like bricks. Inline elements (a, strong, em, span) sit inside a line of text and only take up as much room as their content.

07CSS rules

CSS: pick something, then describe it

HTML alone gives you black text on white in the browser’s default font. CSS (Cascading Style Sheets) changes how elements look. A stylesheet is a list of rules, and every rule has the same two-step shape: first say which elements, then say how they should look.

a declaration h1 { color: crimson; } selector which? property what to change value change it to Colon between property and value; semicolon after each declaration.
Read it as a sentence: for every h1, set the color to crimson. A rule can hold as many declarations as you like inside its braces, one per line.

A realistic rule usually sets several properties at once:

style.css

body {
  background-color: #f2f4f7;
  font-family: Georgia, serif;
  margin: 40px;
}

h1 {
  color: crimson;
  font-size: 48px;
}

Some of this should look familiar. Color values work the way they did in p5 (Week 1): named colors like crimson, hex codes like #f2f4f7, or rgb(210, 30, 92). Sizes are usually given in pixels, written with the unit attached: 48px, not 48.

Three places CSS can live

WhereHowUse it when
A separate file<link rel="stylesheet" href="style.css"> in the headalmost always; one file can style many pages
A <style> tagrules written inside <style>…</style> in the heada quick one-page experiment
Inline<p style="color: red;">rarely; it mixes style back into content

The p5 editor already uses the first option: that <link> line in section 03 is what connects index.html to style.css.

08Selectors

Pointing at the right elements

The selector decides which elements a rule applies to. Three kinds will get you a long way:

SelectorMatchesIn the HTMLIn the CSS
Elementevery element of that type<p>p { … }
Classevery element with that class; reuse freely<p class="note">.note { … }
IDthe one element with that id; once per page<div id="gallery">#gallery { … }

The punctuation is the whole difference: no mark for an element, a dot for a class, a hash for an ID. Classes are the workhorse. You invent the name yourself (same camelCase-or-hyphen naming care as your variables), put it on as many elements as you want, and style them all with one rule.

index.html

<p>A normal paragraph.</p>
<p class="note">A paragraph that's also a note.</p>
<p class="note">Another note.</p>

style.css

p     { color: #333; }                 /* all three paragraphs */
.note { background-color: lightyellow; } /* only the two notes */

When two rules disagree: the cascade

The “cascading” in CSS is the set of tie-breaking rules for when two rules try to set the same property on the same element. For now, two ideas are enough:

  • More specific wins. An ID beats a class, and a class beats a plain element selector.
  • If they’re equally specific, the later rule wins. The browser reads the stylesheet top to bottom, just like p5 reads draw(), and the last word counts (Week 1’s order section).

Some properties, like color and font-family, are also inherited: set them on body and every child in the tree picks them up unless something closer says otherwise.

09Box model

Every element is a box

Here’s the idea that unlocks layout: the browser draws every element as a rectangle, even a single word. You can’t usually see the rectangle, but it’s always there, and CSS controls its four layers from the inside out.

margin — space outside, pushes neighbors away border — the visible edge padding — space inside the border content the text or image itself width & height set the content area; the layers add on around it
Padding is inside the border, so it takes on the element’s background color. Margin is outside, so it’s always transparent.
.card {
  width: 300px;
  padding: 20px;                /* breathing room inside */
  border: 2px solid crimson;    /* thickness, style, color */
  margin: 16px;                 /* gap from everything else */
  background-color: white;
}

You already think in boxes. rect(x, y, w, h) in p5 places a rectangle by giving it numbers. CSS boxes usually aren’t placed by coordinates at all: the browser stacks them in the order they appear in the HTML and you nudge them with padding and margin. It’s a flow, not a canvas.

10View source

Every page is open for reading

Because web pages are sent to you as text, you can read the HTML and CSS behind any site. Generations of web designers learned this way, and it’s where this week gets its name.

  • View source: right-click any page and choose View Page Source (or press Ctrl-U / Cmd-Option-U). You’ll see the raw HTML file exactly as the server sent it.
  • Developer tools: right-click any element and choose Inspect. A panel opens showing the live DOM tree (section 05) with the element highlighted, plus every CSS rule applied to it and a diagram of its box model (section 09).

In the developer tools you can double-click any text or CSS value and change it. The page updates instantly. Nothing you change there touches the real site: you’re editing your copy, the one the server sent to your browser. Reload, and it’s back to normal.

This is the same console. The Console tab in developer tools is where console.log() output goes on any page (Week 2). The p5 editor’s console panel is a friendlier window onto the same thing.

11Reference

Quick reference

How a page loads

Type a URL → DNS finds the server → browser requests the HTML → server responds with the file → browser builds the DOM, requests the CSS, images and scripts it mentions → browser renders the page.

HTML element

<tag attribute="value">content</tag>
<img src="file.jpg" alt="description">   <!-- empty element: no closing tag -->
<!-- a comment, ignored by the browser -->

CSS rule

selector {
  property: value;
  property: value;
}
/* a comment, ignored by the browser */

Starter properties

PropertyControlsExample value
colortext colorcrimson, #d21e5c
background-colorfill behind the elementlightyellow
font-familytypeface, with fallbacksGeorgia, serif
font-sizetext size18px
text-alignleft / center / rightcenter
width / heightcontent area size300px
paddingspace inside the border20px
borderthe edge2px solid black
marginspace outside the border16px

Selectors

p every paragraph · .note anything with class="note" · #gallery the one element with id="gallery"

12Practice

Be able to do

Read a URL

For https://www.nyu.edu/students/index.html, name the protocol, the domain, and the path (section 02). Which computer does your browser ask, and which file does it ask for? Then answer: what would the browser most likely ask for if you visited https://www.nyu.edu/students/?

Label an element

Point to the opening tag, closing tag, attribute name, attribute value, and content (section 04). Then say which of these two elements is empty, and why:

<a href="https://p5js.org">The p5 website</a>
<img src="sketch.png" alt="My week 4 grid">

Build a page from scratch

In the p5 editor, create a new sketch and replace everything in index.html with your own page (it’s fine to delete the p5 lines; this page won’t need them). Use the skeleton from section 05 and include at least: an h1, two paragraphs, a list of three things, a link, and an image with alt text. Press play to see it in the preview.

Before you run it, sketch the DOM tree on paper. Then check: does your indentation match your tree?

Style it with a linked stylesheet

Make sure your head includes <link rel="stylesheet" href="style.css">, then open style.css and write rules that use all three selector types (section 08):

body {
  font-family: Georgia, serif;
  background-color: #f2f4f7;
  margin: 40px;
}

h1 { color: crimson; }

.highlight { background-color: lightyellow; }   /* add class="highlight" to one paragraph */

#profile-photo {                                 /* add id="profile-photo" to your img */
  width: 200px;
  border: 4px solid crimson;
  padding: 8px;
  margin-top: 20px;
}

Change one value at a time and press play after each. Can you predict what will move before it does?

Win a cascade argument

Given this HTML and CSS, what color is the text? Explain using section 08, then test it.

<p class="note" id="intro">Which color am I?</p>
#intro { color: teal; }
.note  { color: crimson; }
p      { color: black; }

Teal. All three rules match, but the ID selector is the most specific, so it wins even though it comes first.

Inspect a real site

Open any site you use often. Right-click a heading and choose Inspect (section 10). Find three things: the tag the heading uses, one CSS property applied to it, and its box-model diagram. Then double-click the heading’s text in the Elements panel and rewrite it. Reload the page and notice that your change is gone. Explain in one sentence why, using the words client and server.