The Creative Coder’s Field Guide
Everything You Need to Survive and Thrive with p5.js
Creative Coding · Week 8
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.
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.
Every page load is a conversation between two computers with two jobs:
s in https means the conversation is encrypted so nobody in between can read it.The text in your address bar, the URL, tells the browser exactly what to ask for and who to ask.
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.
A browser understands three languages, and each one has a single job. Keeping those jobs separate is what makes web pages easy to change.
| Language | Its job | Think of it as | File |
|---|---|---|---|
| HTML | what’s on the page: headings, paragraphs, images, links | the nouns | .html |
| CSS | how it looks: color, size, spacing, layout | the adjectives | .css |
| JavaScript | what it does: reacting, changing, animating | the 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.
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.
week4.html.<p> and a closing tag </p>, which is the same name with a slash in front.name="value". A link needs to know where it goes (href); an image needs to know which file to show (src).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.
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.
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 files in the p5 editor too.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.
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.
| Where | How | Use it when |
|---|---|---|
| A separate file | <link rel="stylesheet" href="style.css"> in the head | almost always; one file can style many pages |
A <style> tag | rules written inside <style>…</style> in the head | a 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.
The selector decides which elements a rule applies to. Three kinds will get you a long way:
| Selector | Matches | In the HTML | In the CSS |
|---|---|---|---|
| Element | every element of that type | <p> | p { … } |
| Class | every element with that class; reuse freely | <p class="note"> | .note { … } |
| ID | the 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 */
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:
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.
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.
.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.
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.
Ctrl-U / Cmd-Option-U). You’ll see the raw HTML file exactly as the server sent it.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.
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.
<tag attribute="value">content</tag>
<img src="file.jpg" alt="description"> <!-- empty element: no closing tag -->
<!-- a comment, ignored by the browser -->
selector {
property: value;
property: value;
}
/* a comment, ignored by the browser */
| Property | Controls | Example value |
|---|---|---|
color | text color | crimson, #d21e5c |
background-color | fill behind the element | lightyellow |
font-family | typeface, with fallbacks | Georgia, serif |
font-size | text size | 18px |
text-align | left / center / right | center |
width / height | content area size | 300px |
padding | space inside the border | 20px |
border | the edge | 2px solid black |
margin | space outside the border | 16px |
p every paragraph · .note anything with class="note" · #gallery the one element with id="gallery"
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/?
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">
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?
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?
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.
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.