HTML
HTML is the language we use to give a webpage its structure. This week we’ll write a page, open it in a browser, and see how the browser uses our HTML to display it.
Editing Files
An HTML file is plain text. We’ll write it in a text editor, Visual Studio Code (VS Code), and open it in a browser to see the page. After you edit the file, save it and reload the page in the browser to see your change.
A website usually has several files: pages, images, and eventually code that controls its appearance and behavior. Keep them in one project folder so you can find them and move them together. A file extension, such as .html, identifies what kind of file it is.
Complete Course Setup to install VS Code and Git and connect your GitHub account. Git records changes to your files, while GitHub stores a copy online.
We will build a recipe page with ingredients, instructions, and a picture. Inside the csci40 folder you made during setup, create a folder named recipe-practice. Open it in VS Code with File → Open Folder…. The Explorer panel on the left shows the files in that folder.
Use the Explorer’s New File button to create index.html, then enter:
<p>California burritos for dinner.</p>Save with File → Save (Ctrl+S or Cmd+S). Find the file in your file manager and double-click it, or drag it into a browser window. After each change, save the file and reload the browser.
Nesting
HTML elements can sit inside one another. This is called nesting. A list contains its items, for instance, and a paragraph can contain emphasized text. Close each child element before closing its parent.
Add an ingredients list below the heading:
<ul>
<li>carne asada</li>
<li>fries</li>
<li>shredded cheese</li>
</ul>Each list item opens and closes inside the list. For the directions, use an ordered list because the steps need to happen in sequence:
<h2>Directions</h2>
<ol>
<li>Cook the steak and fries.</li>
<li>Fill a tortilla with steak, fries, and cheese.</li>
<li>Fold in the sides and roll the burrito.</li>
</ol>Nesting also works within a sentence:
<p>Serve <strong>very <em>hot</em></strong>.</p>The em element closes before strong, because it opened inside strong.
The nested structure forms a tree: every element sits inside one parent, which may contain several children. The browser builds this representation of the page, called the Document Object Model, or DOM. A parent directly contains an element, and its children are the elements directly inside it. Elements with the same parent are siblings. A descendant can be farther down: a child, a child’s child, and so on.
<div>
<h2>Serving</h2>
<p>Allow <strong>five minutes</strong> to cool.</p>
</div>The div has two direct children, h2 and p; they are siblings. The strong element is a child of p and a descendant of div. It is not a direct child of div.
To draw the tree, make a node for each element and connect it to the element that directly contains it:
div
├── h2
└── p
└── strong
Indentation makes nesting easier for a person to read, but the tags determine the tree. In ordinary HTML text, extra spaces and line breaks collapse to a single space. Pressing Enter in the source does not create a new paragraph or list item. Use another element when you want another paragraph or item.
These two versions describe the same paragraph:
<p>Serve with salsa.</p>
<p>Serve
with salsa.</p>For two paragraphs, write two elements:
<p>Serve with salsa.</p>
<p>Refrigerate the leftovers.</p>Page Structure
A complete HTML document separates information about the page from the content on the page. The head holds information such as the page title and text encoding. The body holds the headings, paragraphs, and other content shown in the browser window. Both sit inside one outer html element.
The first line, <!DOCTYPE html>, tells the browser to use modern HTML. It is a declaration, not an element, and has no closing tag. We also specify the page’s language with lang="en" and its text encoding with <meta charset="utf-8">. Keep these in the documents you create.
Put the recipe inside this structure:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Weeknight Recipes</title>
</head>
<body>
<h1>California Burritos</h1>
<p>Carne asada, fries, and cheese wrapped in a warm tortilla.</p>
</body>
</html>The browser tab is named Weeknight Recipes, while the heading on the page is California Burritos. They can say different things because title and h1 serve different purposes. Put the ingredients and directions lists from earlier inside body, after the introduction.
A browser will try to display a page even if its HTML contains mistakes. It may repair the structure in a way you did not intend. To check the source, upload the saved file to the W3C HTML Validator using Validate by File Upload. Check the browser view too: valid structure cannot tell you whether the words, link destinations, or pictures are the ones you wanted.
Attributes
An attribute supplies extra information about an element. It is written inside the opening tag as name="value". The tag identifies the kind of element; its attributes describe details such as a link’s destination or an image’s source. Closing tags contain only the tag name.
The table below shows common HTML attributes and their purposes.
| Attribute | Used on | Purpose |
|---|---|---|
href |
<a> |
Destination of a link |
src |
<img> |
Location of an image file |
alt |
<img> |
Text describing the image’s information |
lang |
<html> |
Language of the page, such as en for English |
charset |
<meta> |
Text encoding, which we set to utf-8 |
A link has two separate parts: the destination in href and the clickable content between its tags. Changing one does not change the other.
Add a link to another recipe page:
<a href="salsa.html">Salsa to serve with the burritos</a>The visitor clicks Salsa to serve with the burritos, and the browser opens salsa.html. For a page on another website, use its full address:
<a href="https://example.com/recipes/salsa.html">Salsa recipes</a>An image gets its content from the file named by src. Its alt attribute provides a text description for someone who cannot see that image, including a person using a screen reader. The description should communicate the information the picture adds. It can also help when the image fails to load.
Save a photo you have permission to use as burrito.jpg beside index.html. Add:
<img src="burrito.jpg" alt="A California burrito cut in half, showing steak and fries">The image is a void element, so it does not have </img>. A link can contain an image instead of words:
<a href="salsa.html">
<img src="salsa.jpg" alt="Salsa recipe">
</a>Clicking the picture opens the page named by href; src chooses the picture itself.
File Paths
A path tells the browser where to find a file. A relative path starts in the folder containing the HTML page. A filename points to a file in that folder. To reach a file in a subfolder, put the folder name and a slash before the filename. These links still work when you move the whole site together.
Suppose the project folder contains:
recipe-practice/
├── index.html
├── salsa.html
└── images/
└── burrito.jpg
From index.html, the link is href="salsa.html" and the image source is src="images/burrito.jpg". If you move the photo into images, update its src too. Compare the path, spelling, and capitalization when an image fails to appear.
Opening a page from your disk gives it a file: address that points to your computer. Publishing puts the files on a web server, where other people can request them through an https: address. Relative links keep working when the files move together with the same folder structure.
Here is the recipe assembled into a complete page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Weeknight Recipes</title>
</head>
<body>
<h1>California Burritos</h1>
<p>Carne asada, fries, and cheese wrapped in a warm tortilla.</p>
<h2>Ingredients</h2>
<ul>
<li>carne asada</li>
<li>fries</li>
<li>shredded cheese</li>
</ul>
<h2>Directions</h2>
<ol>
<li>Cook the steak and fries.</li>
<li>Fill a tortilla with steak, fries, and cheese.</li>
<li>Fold in the sides and roll the burrito.</li>
</ol>
<p><a href="salsa.html">Salsa to serve with the burritos</a></p>
<img src="images/burrito.jpg" alt="A California burrito cut in half, showing steak and fries">
</body>
</html>Save and reload, then check the picture and follow the link. For the local link to work, create salsa.html in the same folder using the document structure above.
HTML describes the content’s structure, and the browser supplies a default appearance. Next we will use CSS to choose colors, fonts, spacing, and layout.