1  HTML

learning goals
  • Understand the fundamental structure of web pages and how HTML creates content
  • Learn essential HTML tags for building psychology experiments
  • Master HTML document structure and organization principles
  • Apply HTML attributes, classes, and IDs for element identification and styling preparation
  • Create well-structured, accessible web content using semantic markup

1.1 How Websites Work: The Basics

Think of a website like a digital magazine. Just as a magazine is made up of text, images, and layout instructions, a website consists of files that tell your browser what to display and how to display it.

Every website you visit is built from simple text files that contain instructions written in special languages:

HTML (HyperText Markup Language) is like the skeleton of a webpage. It defines the structure and content like headings, paragraphs, buttons, and forms.

CSS (Cascading Style Sheets) is like the clothing and makeup. It controls how things look, including colors, fonts, spacing, and layout.

JavaScript is like the nervous system. It makes things interactive and responsive to user actions.

The remarkable thing is that these are all just plain text files, no different from a document you might write in Notepad. The “magic” happens when your web browser reads these text files and interprets the instructions to create the visual, interactive experience you see.

When you create an online behavioural study, here’s the simplified journey your experiment takes to reach participants:

First, you create your experiment files using HTML for structure, CSS for appearance, and JavaScript for interactivity. These files are then stored on a web server, which is essentially a computer that’s always connected to the internet, ready to share your files.

When participants visit your experiment’s web address (URL), their browser requests your files from the server. The server sends your files to their browser instantaneously, like mailing a package but much faster. Their browser then interprets your files and transforms your text instructions into the interactive experiment they see. Finally, when participants respond, their answers are sent back to be stored for your analysis.

1.2 Introduction to HTML

All the web experiments you might encounter are simply websites, built using the same building blocks that all websites are made with. To start, we’re going to be using the language HTML, or hypertext markup language.

HTML isn’t a programming language since it doesn’t actually do anything. It doesn’t, for example, perform calculations, make decisions, or respond to user input. Think of HTML like the blueprint or skeleton of a house: it defines the structure and layout, but it doesn’t make the lights turn on or the water flow.

More specifically, HTML is a markup language. Just like how you might use a highlighter to mark up a printed document like underlining important parts, circling key terms, or adding notes in the margin, HTML “marks up” text to give it meaning and structure. It tells the browser “this text is a heading,” “this text is a paragraph,” “this text is a link,” and so on.

You can think of HTML as providing the content and structure of a webpage. It contains the actual text, images, and media that appear on the page. It defines how that content is organized through headings, paragraphs, lists, and other elements. It establishes the basic layout and hierarchy of information that users will see.

HTML creates static content, meaning it doesn’t change or respond on its own. It’s like a printed page—the words and pictures stay exactly where you put them. Later, we’ll add CSS to make it look better and JavaScript to make it interactive, but HTML provides the foundation that everything else builds upon.

In psychology experiments, HTML creates the basic structure: the instructions participants read, the buttons they click, the areas where stimuli appear, and the forms where they enter responses.

1.3 HTML Tags

HTML’s base building block is the tag. A tag is a building block. It describes what’s inside it. Every tag has an opening and a closing tag (some tags open and close at the same point.) I think the easiest way to learn it is just to show a bunch of examples.

You can see the <h1> and the </h1> surrounding the text “This is the title to my document”. This is how HTML operates. You can have an opening tag which has information or more tags inside of it. In this case we have an h1 tag which is a heading tag: it’s used for the biggest title on the page, typically a title. If you rendered that using the browser, it looks like:

<h1>This is the title to my document</h1>

It’s bigger and bolder because that’s what browsers do with h1s, it makes them look important on the page. However it does more than that too. Browsers aren’t the only thing reading websites. Blind and people who can’t see well will use screen readers to read web pages out loud to them; it uses things like headers to understand what information is important to read to the users. It’s also how Google and Bing will understand the important details of your website. In other words, it’s important which type of tag you use. More than just the visual aesthetic is using those tags.

A tag, whether it’s opening or closing, is surrounded by angle brackets, < and >. Closing tags always have a slash, /, after the opening angle bracket, so it looks like </h1>. There are things called “self-closing tags” or “void tags” that open and close themselves. These will look like this: <input /> (I’ll explain in a sec what inputs are.) That slash at the end means it is self-closing. To make it more confusing, that last slash is optional, so <input> (with no closing tag ever) is valid too since input tags are always self-closing.

Tags are also opened and closed in a specific order too. The most recently opened tag must be the next closed tag. For example, if I have an h1 instead of a div, the h1 must be closed first.

<div>
  <h1>Hi</h1>
</div>

The above is correct.

<div>
  <h1>
    Hi
  </div>
</h1>

The above is incorrect. I can’t close the div before I close the h1 since the h1 was the last one I opened.

1.4 Comments vs. Code

One important note before going through the essential HTML tags: All programming languages allow us to write ‘comments’ inside our code. These comments are ignored when the code is run. It’s a way for us to make notes to ourselves or future readers of our code to help document our code as we write it. Each language has it’s own way of marking something as a comment. In HTML, the way you mark some text as a comment (and not code) is by putting the text inside a <!-- and -->.

<!-- This is a comment that will be ignored by the browser -->
<h1>This is HTML code that the browser will run</h1>

<!-- 
Comments 
can 
be 
multiple 
lines
-->

Commenting is also a useful way of removing some code without deleting it. Let’s say you want to see what the page looks like without some HTML, you can ‘comment it out’ like this:

<!-- This is a comment that will be ignored by the browser -->
<h1>This is HTML code that the browser will run</h1>

<!-- 
<p>This HTML will be ignored because it's inside a comment</p>
-->

Ok, now that we have that settled, let’s explore some of the essential tag types that we will be using to make our experiments.

1.5 Essential Tags

1.5.1 Headings

“h1”, “h2”, “h3”, “h4”, “h5”, and “h6” – Headings. These are the six levels of headings and subheadings you can have. You can see up top of this page we have “HTML” which is an h1 and then below that we have “Types of Tags” which is an h2. An h2 is a subheading to an h1. An h3 is a subheading to a h2. Some schools of thought say each page should only have one h1. I’m of the opinion that just use these as it feels appropriate to. Like formatting a Microsoft Word document, there’s no “correct” way to do it, just different ways that make more or less sense.

<h1>This is an h1!</h1>
<h2>This is an h2!</h2>
<h3>This is an h3!</h3>
<h4>This is an h4!</h4>
<h5>This is an h5!</h5>
<h6>This is an h6!</h6>

1.5.2 Paragraphs

“p” – Paragraph. You’ll put a paragraph of text together inside of a “p” tag. Only text goes in “p” tags. Each one of these paragraphs is a “p” tag.

<p>
  Lorem ipsum dolor sit amet consectetur adipisicing elit. Incidunt modi est
  sapiente in optio quia inventore quis maxime ullam tenetur?
</p>
<p>
  Maxime quibusdam, dolorum quaerat ducimus inventore sunt pariatur et dolore
  ipsam. Distinctio eum nobis officiis quam quasi exercitationem eaque?
</p>

1.5.3 Div

“div” – Short for division. A div is sort of like a cardboard box. It’s not really anything by itself; it’s more defined by what’s in it. It’s a generic container tag for grouping together other things. You’ll use a lot of divs. Very useful with CSS. In general, you want to group together “like” things into a containing tag (like a div) to keep them together. If you have a website with a list of blog posts that each have paragraphs, titles, images, etc. you’ll group each post together in a div or other container-type tag typically.

Below are a few examples of using divs for organization. I’ve colored in the div backgrounds so that we can see then. You don’t need to pay attention to the styling, as we haven’t learned that part yet. But do pay attention to how divs provide structure

1.5.3.1 Simple Div Example

First, here is a simple div that creates an empty container.

<div style="width: 100px; height: 100px; background-color: red;"></div>

1.5.3.2 Nested Containers for Organization

Divs become powerful when you nest them inside each other to create organized layouts. Think of it like organizing items in boxes within boxes:

<div style="background-color: lightgray; padding: 20px;">
  <div style="background-color: lightblue; height: 60px; margin-bottom: 10px;"></div>
  <div style="background-color: lightyellow; height: 80px; margin-bottom: 10px;"></div>
  <div style="background-color: lightgreen; height: 40px;"></div>
</div>

1.5.3.3 Side-by-Side Layout with Divs

Divs are essential for creating layouts where content appears side-by-side:

<div style="background-color: lightgray; padding: 20px; display: flex; gap: 15px;">
  <div style="background-color: lightcoral; width: 150px; height: 120px;"></div>
  <div style="background-color: lightsteelblue; width: 150px; height: 120px;"></div>
</div>

1.5.3.4 Complex Nested Layout

You can combine vertical and horizontal organization by nesting divs in different ways:

<div style="background-color: #f0f0f0; padding: 15px;">
  <div style="background-color: #ffcccb; height: 50px; margin-bottom: 10px;"></div>
  <div style="display: flex; gap: 10px; margin-bottom: 10px;">
    <div style="background-color: #add8e6; width: 100px; height: 80px;"></div>
    <div style="background-color: #90ee90; flex: 1; height: 80px;"></div>
    <div style="background-color: #ffd700; width: 60px; height: 80px;"></div>
  </div>
  <div style="background-color: #dda0dd; height: 40px;"></div>
</div>

This nested approach makes your HTML much more maintainable and your CSS much more powerful!

1.5.4 Span

“span” – A container for small pieces of text. If a div is like a cardboard box, a span is like a Ziploc bag. It doesn’t change the styling of anything by itself but it allows you use CSS and JavaScript later to make that text different in some way.

<p>This is <span style="text-decoration: underline">important</span> text</p>

1.5.5 Images

“img” – An image. You use this to load images onto the page. This can be confusing because you can use CSS to bring in images too. The key difference is that when the image is apart of the content, like a diagram that shows data you’re talking about or a picture that shows something from the article, it should be an “img” tag. If it’s a nice background image or something that’s for decoration of your website, use CSS. An “img” tag needs a “src” to say where the image is coming from and “alt” to say what is in the image for screen readers so that the image will still be useful to blind people, people who are hard of seeing, and Google and Bing search engines. “img” are always self-closing tags.

Note that the image src can point to an online source which it fetches when the page loads or it can refer to a local image stored in your folder. When you want to refer to the online image, you need to provide the link: <img src="http://pets-images.dev-apis.com/pets/dog25.jpg" alt="an adorable puppy" />

When you want to refer to the local image in your folder, you need to point to where it is located. If it’s located in the same folder than this will suffice: <img src="dog25.jpg" alt="an adorable puppy" />

But if it’s in another folder, you’ll need to point there: <img src="myImageFolder/dog25.jpg" alt="an adorable puppy" />.

<img
  src="http://pets-images.dev-apis.com/pets/dog25.jpg"
  alt="an adorable puppy"
/>
<img
  src="http://pets-images.dev-apis.com/pets/dog26.jpg"
  alt="an adorable puppy"
/>
<img
  src="http://pets-images.dev-apis.com/pets/dog27.jpg"
  alt="an adorable puppy"
/>

1.5.7 Lists

“ol”, “ul”, and “li” – Both “ol” and “ul” represent lists. In fact, this list of various tags is a “ul”! A “ul” is an unordered list: it’s a list of things that could be shuffled and still mean the same thing. If I asked you to list all the teams in a sports league, or all the characters on a TV show, those could be presented in any order. An “ol” is an ordered list: what comes first matters. If I ask you to list out the ten most populous cities in the world, there is an order to that and changing the order makes the list incorrect. In either list, each item in the list is an “li”.

<ol>
  <li>The First One</li>
  <li>The Second One</li>
  <li>The Third One</li>
</ol>

<ul>
  <li>An Item in the List</li>
  <li>Another Item in the List</li>
  <li>A Yet Different Item in the List</li>
</ul>

There are many more tags (or HTML elements) that you can use to build a website. The ones we’ve just reviewed will be the essential tags needed to generate our experiments.

To see the full list of HTML elements, you can go here: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements

1.6 Attributes

Attributes allow you to modify behavior of an HTML tag. You’ve already seen a few of them but we’ll go into a few more examples of them.

A really good example we have seen already is the href attribute of the anchor tag:

<a href="www.frontendmasters.com"></a>

By modifying the href we’re modifying what the tag does. It’s contextual as well: href only works on an anchor tags. You can’t add a href to a div and expect it to suddenly work as a link.

Another example is the src attribute on images. The src tells the browser where to find the image file:

<img src="happy-face.jpg" alt="A smiling face" />
<img src="sad-face.jpg" alt="A frowning face" />

1.6.1 Classes and IDs: Reference Labels

There are other attributes like class and id that are universal and can be applied to (nearly) all tags. These themselves don’t change how things look or behave - they’re like reference labels that allow other parts of your code to find and work with specific HTML elements later.

Think of classes and IDs like name tags at a conference. The name tag doesn’t change what you look like, but it helps other people find and identify you. Similarly, classes and IDs help CSS (for styling) and JavaScript (for interactivity) find and work with specific HTML elements.

<div class="experiment-instructions">
  <h2 id="main-title">Memory Task Instructions</h2>
  <p class="important-note">Please read carefully!</p>
  <p class="important-note">This experiment will take 15 minutes.</p>
  <a class="start-button" href="experiment.html">Begin</a>
</div>

In this example:

  • The experiment-instructions class labels the entire instruction section
  • The main-title ID labels the specific heading (there’s only one main title)
  • The important-note class labels both important paragraphs (reusable!)
  • The start-button class labels the link

You can give a single element multiple classes by separating them with spaces. This is really powerful because it lets you combine different labels:

<div class="trial-display centered-content">
  <p class="stimulus-text large-font">BLUE</p>
  <img class="stimulus-image rounded-corners" src="face1.jpg" alt="Happy face" />
  <a class="response-button primary-button" href="next-trial.html">Continue</a>
</div>

In this example:

  • The div has both trial-display and centered-content classes
  • The p has both stimulus-text and large-font classes
  • The img has both stimulus-image and rounded-corners classes
  • The a has both response-button and primary-button classes

1.6.2 Classes vs IDs: When to Use Which?

Classes are like group labels - you can use the same class on multiple elements:

  • Use when you might have multiple elements that should be treated the same way
  • Perfect for things like “all warning messages” or “all response buttons”
  • Can be reused as many times as you want on a page

IDs are like unique identifiers - each ID can only be used once per page:

  • Use when you have exactly one of something special
  • Good for unique elements like “the main experiment area” or “the final results section”
  • Must be unique - you can’t have two elements with the same ID
<!-- Good use of classes - multiple elements that should be grouped -->
<p class="instruction-text">Read the word, ignore the color.</p>
<p class="instruction-text">Respond as quickly as possible.</p>
<p class="instruction-text">Press any key to begin.</p>

<!-- Good use of ID - unique element -->
<div id="experiment-container">
  <!-- All experiment content goes here -->
</div>

<!-- Combining classes and IDs -->
<div id="trial-area" class="centered-content trial-display">
  <p class="stimulus-word large-text">RED</p>
</div>

Rule of thumb: Use classes 95% of the time. Only use IDs when you’re absolutely sure there will only ever be one of that element on the page, and you need to specifically target that unique element later in your code.

For psychology experiments, classes are perfect for things like trial displays, response buttons, and instruction text that you’ll reuse across different parts of your experiment.

1.7 Organizing HTML

Let’s talk about organizing HTML and how to make the most of it. This will help a ton once you get to the CSS section.

HTML is like building with blocks in that you can put smaller elements inside bigger containers to organize your content. The <div> element is your most versatile container. It doesn’t have any special meaning by itself, but it’s perfect for grouping related elements together.

1.7.1 Basic Organization with Divs

Let’s say you’re creating an instruction page for a psychology experiment. You need to welcome participants, explain the task, show them what they’ll see, and give them a button to start. Let’s put that together using HTML:

<div>
  <h1>Welcome to the Experiment!</h1>
  <p>
    In this experiment, you will see images of faces and need to identify 
    the emotion being displayed. Each face will appear for 2 seconds.
  </p>
  <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/79/Face-smile.svg/120px-Face-smile.svg.png" alt="Example of a happy face" />
  <p>Click the emotion you see as quickly and accurately as possible.</p>
  <button>Start Experiment</button>
</div>

Let’s walk through what we did here and why.

We wrapped everything in a <div> element. This creates a container that holds all our related content together. Think of it like putting all the instruction materials in one folder.

Notice how every tag that we opened gets closed. We have: - <div> ... </div> - <h1> ... </h1> - <p> ... </p> - <button> ... </button> - Note that the <img> tag is self-closing, so it doesn’t need a separate closing tag.

Look at how we formatted the HTML with indentation and line breaks. This makes it easier to read, but the browser ignores most of this whitespace. We could write it all on one line and it would look the same to users, but that would be much harder for us to read and edit! Good formatting makes your code maintainable.

All of our content elements (h1, p, img, button) are inside the div container. This is called nesting. The div is the parent, and everything inside it are its children.

1.7.2 Adding Labels for Future Reference

Now, this HTML works perfectly fine, but when we get to CSS (in the next chapter), we’ll want to style different parts differently. Maybe we want the title to be bigger, or the button to be blue. To do that, we need a way to refer to specific elements. That’s where classes and IDs come in:

<div id="instruction-page">
  <h1 class="page-title">Welcome to the Experiment!</h1>
  <p class="instruction-text">
    In this experiment, you will see images of faces and need to identify 
    the emotion being displayed. Each face will appear for 2 seconds.
  </p>
  <img class="example-image" src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/79/Face-smile.svg/120px-Face-smile.svg.png" alt="Example of a happy face" />
  <p class="instruction-text">Click the emotion you see as quickly and accurately as possible.</p>
  <button class="start-button">Start Experiment</button>
</div>

Notice that it looks exactly the same! The classes and IDs don’t change how things appear. They’re just labels that we can use later. Think of them like putting sticky notes on different parts of your content.

We gave id="instruction-page" as a unique label for this specific page. The class="page-title" labels the main title. We used class="instruction-text" for instruction paragraphs (notice two elements have this same class). The class="example-image" labels the example image, and class="start-button" labels the start button.

In the CSS chapter, we’ll use these labels to make the title bigger, color the button blue, or center the image. The key principle is that HTML defines the structure and content, while CSS (which we’ll learn next) handles the appearance. By organizing our HTML well and adding meaningful labels, we set ourselves up for success when we start styling our experiments.

1.8 HTML Document Structure

So far we’ve been discussing snippets of HTML, using things like divs to build up “components” (or a group of tags that represents a higher level concept like a post or a navigation bar). But let’s talk about the overall structure of an HTML document.

Every HTML page needs a basic skeleton that tells the browser important information about how to display and handle your content. Here’s the standard structure you should use for every HTML document:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>My Psychology Experiment</title>
  </head>
  <body>
    <!-- All your visible content goes here -->
    <h1>Welcome to the Study</h1>
    <p>This is where participants will see your experiment content.</p>
  </body>
</html>

Let’s break down each part of this structure:

1.8.1 Document Type Declaration

<!DOCTYPE html>

This tells the browser we’re using HTML5 (the current standard). There have been several versions of HTML over the years, and this declaration ensures the browser interprets your code using the latest, most reliable standards.

1.8.2 Root HTML Element

<html lang="en">

This is the container for your entire document. The lang=“en” attribute tells browsers and search engines that your content is in English, which helps with accessibility and search indexing.

1.8.3 The Head Section

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>My Psychology Experiment</title>
</head>

The <head> contains metadata that is information about your page that doesn’t appear in the browser window.

The charset="UTF-8" tells the browser which character set to use. UTF-8 supports all modern characters, emojis, and international text. Always include this.

The viewport meta tag is essential for mobile devices. Without this, phones might display your page zoomed way out and tiny. This ensures your experiment works properly on all screen sizes.

The <title> sets the text that appears in the browser tab and in search results.

1.8.4 The Body Section

<body>
  <!-- All your visible content goes here -->
</body>

Everything participants actually see and interact with goes inside the <body> tags. This is where you’ll put your experiment instructions, stimuli, response buttons, and all other visible content.

1.9 Putting it All Together: A Live Coding Exercise

Throughout this textbook, you will find live coding exercise where you can directly edit some code and see the result right here in the book.

Below is our simple HTML welcome page. Go ahead and edit the HTML below. The webpage, on the right, will update as you change the code. None of these changes are permanent. If you refresh the page, the codepen will revert back to the original code it started as.

Some things to try:

  1. Change the paragraph text inside <p> </p>
  2. Add a second header using <h2> </h2>
  3. Add an ordered list below the <p>
  4. Add the smiley face image using the wikicommons link from the earlier examples.
Live Coding Exercise