Web Programming Foundations
Schedule Notes Resources Graded Work MyApps Instructions Vercel Guide Code examplesHTML is the HyperText Markup Language. It allows us to write content in a document, just as we would in a file created by a word processor. Unlike a regular text file, it also includes structural and layout information about this content. We literally mark up the text of our document with extra information.
When talking about HTML’s markup, we’ll often refer to the following terms:
<
and >
characters, for example the image tag <img>
.<h1>Chapter 1</h1>
. Here an element is made up of an <h1>
tag (i.e., opening Heading 1 tag), the text content Chapter 1
, and a closing </h1>
tag. These three together create an h1
element in the document.name
or name="value"
, for example <p id="error-message" hidden>There was an error downloading the file</p>
. Here two attributes are included with the p
element: an id
with value "error-message"
(in quotes), and the hidden
attribute (note: not all attributes need to have a value). Full list of common attributes.&
and end with ;
. For example, if you need to use the <
character in your document, you need to use <
instead, since <
would be interpreted as part of an HTML tag.
is a single whitespace and &
is the &
symbol. Full list of named entities.First HTML page ever created was built by Tim Berners-Lee on August 6, 1991.
Since then, the web has gone through many versions:
Here’s a basic HTML5 web page:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>My Web Page</title>
</head>
<body>
<!-- This is a comment -->
<h1>Hello World!</h1>
</body>
</html>
Let’s break this down and look at what’s happening.
<!doctype html>
tells the browser what kind of document this is, and how to interpret/render it<html>
the root element of our document: all other elements will be included with <html>...</html>
.<head>
provides various information about the document as opposed to providing its content. This is metadata that describes the document.<meta>
an example of some piece of metadata, in this case defining the character set used in the document: utf-8<title>
an example of a specific (named) metadata element: the document’s title, shown in the browser’s title bar. There are a number of specific named metadata elements like this.<body>
the content of the document is contained within <body>...</body>
.<!-- ... -->
a comment, similar to using /* ... */
in C or JavaScript<h1>
a heading element (there are headings 1 through 6), which is a title or sub-title in a document.Now let’s try creating and loading this file in our browser:
During the last couple of classes we learned how to create a simple web server using Node.js with the Express.js module. We will be using this code once again to create a local web server, which will be responsible for returning requests for our HTML document on the default route, ie “/”.
NOTE: Try to familiarize yourself with these steps, as we will be doing this over and over in class, every time we wish to create a new web server. A more detailed guide can be found as a part of the week 4 notes (“Building a simple web server using Node.js with Express.js”) as well as within the Vercel Guide.
test-server
test-server
folderserver.js
`
) or select “View” -> “integrated terminal” from the top menu.package.json
file (you can choose all of the defaults)npm install express --save
Enter the following code for your server.js
file
var HTTP_PORT = process.env.PORT || 8080;
var path = require("path");
var express = require("express");
var app = express();
// setup a 'route' to listen on the default url path
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname,"/views/hello.html"));
});
// setup http server to listen on HTTP_PORT
app.listen(HTTP_PORT, function(){ console.log('server listening on: ' + HTTP_PORT )});
test-server
named views
views
folder, create a file called hello.html
hello.html
file and paste the html code from above and save the filenode server.js
. You should see the message: server listening on: 8080
http://localhost:8080
in the URL barHello World!
in black text.Now let’s make a change to our document:
hello.html
file so that instead of Hello World!
you have This is my web page.
hello.html
file.This is my web page.
Every time we update anything in our web page, we have to refresh the web page in our browser. The web server will serve the most recent version of the file on disk when it is requested.
There are dozens of HTML elements you’ll learn and use, but the following is a good set to get you started.
Information about the document vs. the document’s content goes in various metadata elements:
<link>
- links from this document to external resources, such as CSS stylesheets<meta>
- metadata that can’t be included via other elements<title>
- the document’s titleThese are organizational blocks within the document, helping give structure to the content and provide clues to browsers, screen readers, and other software about how to present the content:
<header>
- introductory material at the top of a document<nav>
- content related to navigation (a menu, index, links, etc)<main>
- the main content of the document. For example, a news article’s paragraphs vs. ads, links, navigation buttons, etc.<h1>, <h2>, ..., <h6>
- (sub) headers for different sections of content<footer>
- end material (author, copyright, links)We organize content into “boxes,” some of which have unique layout characteristics.
<div>
- a generic container we use to attach CSS styles to a particular area of content<ol>
- an ordered list (1, 2, 3) of list items<ul>
- an unordered list (bullets) of list items<li>
- a list item in an <ul>
or <ol>
<p>
- a paragraph<blockquote>
- an extended quotationWe also use elements within larger text content to indicate that certain words or phrases are to be shown differently:
<a>
- an “anchor” element, which will produce a hyperlink, allowing users to click and navigate to some other document.<code>
- formats the text as computer code vs. regular text.<em>
- adds emphasis to the text (often in italics)<span>
- another generic container, used to define CSS stylesIn addition to text, HTML5 also defines a number of rich media elements:
<img>
- an element used to embed images in a document.<audio>
- an element used to embed sound in a document.<video>
- an element used to embed video in a document<canvas>
- a graphical area (rectangle) used to draw with either 2D or 3D using JavaScript.We create dynamic web content and applications through the use of scripting:
<script>
- used to embed executable code in a document, typically JavaScript.Visual HTML elements are categorized into one of two groups:
Consider the following HTML content:
<body>
<p>The <em>cow</em> jumped over the <b>moon</b>.</p>
</body>
Here we have a <p>
paragraph element. Because it is a block-level element, this paragraph will
fill its container (in this case the <body>
element). It will also have empty space added above and below it.
Within this block, we also encounter a number of other inline elements. First, we have simple text.
However, we also see the <em>
and <b>
elements being used. These will affect their content, but not
create a new block; rather, they will continue to flow inline in their container (the <p>
element).
Many of the elements we’ve seen so far begin with an opening tag, and end with a closing tag: <body></body>
.
However, not all elements need to be closed. Some elements have no content, and therefore don’t need
to have a closing tag. We call these empty elements.
An example is the <br>
line break element.
We use a <br>
when we want to tell the browser to insert a newline (similar to using \n
in C):
<p>Knock, Knock<br>Who's there?</p>
Other examples of empty elements include <hr>
(for a horizontal line), <meta>
for including metadata
in the <head>
, and a dozen others.
Often we need to group elements in our page together. We have a number of pre-defined element container options for how to achieve this, depending on what kind of content we are creating, and where it is in the document:
<header>
- introductory material at the top of a<nav>
- content related to navigation (a menu, index, links, etc)<main>
- the main content of the document.<article>
- a self-contained composition, such as a blog post, article, etc.<section>
- a group of related elements in a document representing one section of a whole<footer>
- end material (author, copyright, links)Sometimes there is no appropriate semantic container element for our content, and we need something more generic. In such cases we have two options:
<div>
<p>This is an example of a using a div element. It also includes this <span><em>span</em> element</span>.</p>
<p>Later we'll use a div or span like this to target content in our page with JavaScript or CSS styles.<p>
</div>
Sometimes our data is tabular in nature, and we need to present it in a grid. A number of elements are used to create them:
<table>
- the root of a table in HTML<caption>
- the optional title (or caption) of the table<thead>
- row(s) at the top of the table (header row or rows)<tbody>
- rows that form the main body of the table (the table’s content rows)<tfoot>
- row(s) at the bottom of the table (footer row or rows)We define rows and columns of data within the above using the following:
<tr>
- a single row in a table<td>
- a single cell (row/column intersection) that contains table data<th>
- a header (e.g., a title for a column)We can use the rowspan
and colspan
attributes to extend table elements beyond their usual bounds,
for example: have an element span two columns (colspan="2"
) or have a heading span 3 rows (rowspan="3")
.
<table>
<caption>Order Information</caption>
<thead>
<tr>
<th>Quantity</th>
<th>Colour</th>
<th>Price (CAD)</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Red</td>
<td>$5.60</td>
</tr>
<tr>
<td>3</td>
<td>Blue</td>
<td>$3.00</td>
</tr>
<tr>
<td>8</td>
<td>Blue</td>
<td>$1.50</td>
</tr>
</tbody>
<tfoot>
<tr>
<th colspan="2">Total</th>
<th>$26.60</th>
</tr>
</tfoot>
</table>
<img>
, <audio>
, <video>
HTML5 has built in support for including images, videos, and audio along with text. We specify the media source we want to use, and also how to present it to the user via different elements and attributes
<!-- External image URL, use full width of browser window -->
<img src="https://images.unsplash.com/photo-1502720433255-614171a1835e?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=344dfca9dc8cb137a4b1c2c711752bc5">
<!-- Local file cat.jpg, limit to 400 pixels wide -->
<img src="cat.jpg" alt="Picture of a cat" width="400">
HTML5 has also recently added the <picture>
element
, to allow for an optimal image type to be chosen from amongst a list of several options.
We can also include sounds, music, or other audio:
<!-- No controls, music will just auto-play in the background. Only MP3 source provided -->
<audio src="https://ia800607.us.archive.org/15/items/music_for_programming/music_for_programming_1-datassette.mp3" autoplay></audio>
<!-- Audio with controls showing, multiple formats available -->
<audio controls>
<source src="song.mp3" type="audio/mp3">
<source src="song.ogg" type="audio/ogg">
<p>Sorry, your browser doesn't support HTML5 audio. Here is a <a href="song.mp3">link to the audio</a> instead</p>.
</audio>
Including video is very similar to audio:
<!-- External Video File, MP4 file format, show controls -->
<video src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" controls></video>
<!-- Local video file in various formats, show with controls -->
<video width="320" height="240" controls>
<source src="video.mp4" type="video/mp4">
<source src="video.ogg" type="video/ogg">
<source src="video.webm" type="video/webm">
<p>Sorry, your browser doesn't support HTML5 video</p>
</video>
NOTE: the <audio>
and <video>
elements must use source URLs that point to actual audio or video files
and not to a YouTube URL or some other source that is actually an HTML page.
JavaScript is an extremely versatile language. So far, we have been using it in the context of creating a web server using Node.js and the Express.js framework. However, it can also be executed within the context of a web browser (Chrome, Firefox, etc.). While we will not be focusing on writing client-side JavaScript in this course, it is still valuable to know how to include scripts in your HTML code. For example, the JavaScript code included with the Bootstrap Framework.
To reference an “external script” within your HTML code, the following <script> code can be used. This JavaScript file can be located either on your working server, or somewhere on the web (accessible via it’s full URL).
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Web Page with Script</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
Notice how we include a src="script.js"
attribute. Much like the <img>
element, a <script>
can include a src
URL to load at runtime.
The browser will begin by loading your .html
file, and when it encounters the <script src="script.js">
element,
it will begin to download script.js
from the web server, and then run the program it contains.
Using this syntax, we can include as many scripts as we need. The scripts we include can be:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Web Page with Scripts</title>
</head>
<body>
<script src="https://scripts.com/some/other/external/script/file.js"></script>
<script src="local-script.js"></script>
<script>
// Use functions and Objects defined in the previous two files
doSomethingAmazing();
</script>
</body>
</html>
It’s clear that learning to write proper and correct HTML is going to take practice. There are lots of elements to get used to, and learn to use in conjunction. Also each has various attributes that have to be taken into account.
Browsers are fairly liberal in what they will accept in the way of HTML. Even if an HTML file isn’t 100% perfect, a browser can often still render something. That said, it’s best if we do our best to provide valid HTML.
In order to make sure that your HTML is valid, you can use an HTML Validator. There are a few available online:
Both allow you to enter a URL to an existing web page, or enter HTML directly in a text field. They will then attempt to parse your HTML and report back on any errors or warnings, for example: an element missing a closing tag.