HTML stands for HyperText Markup Language
HTML is the standard markup language/format for creating web pages, containing the content and structure of a page as a series of tags/elements.
-
HTML | MDN
When in doubt, refer to the MDN documentation! -
Basics of HTML
A very calming introduction by Laurel Schwulst. -
Organizing Files for the Web Sasha Portis on (web) file-naming, for when you get to saving.
HTML is the skeleton of the web. At its most basic it is a text file, in a folder on a computer, with a .html
extension.
This format was codified by our pal Tim Berners-Lee in 1991, evolving from his earlier SGML, a similar/
- HTML 1, 1991
- HTML 2, 1995
- HTML 3, 1997
- HTML 4, 1997 (busy year)
- HTML 5, 2014
The basic document
HTML consists of a range of elements, nested inside one another, like a matryoshka doll of text.
As a visual:
As code:
<!DOCTYPE html>
<html>
<head>
<title>Page title</title>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
</body>
</html>
The <html>
element contains all elements of the page, the <head>
element contains the title, and the body contains <h1>
and <p>
.
We call these semantic elements—which is saying that they give their contents a meaning or a role. (Tim’s diagram.) These roles are then interpreted by your browser (Chrome, Safari, Firefox, etc.) when it loads the file, to ultimately display the page. We call this parsing the document.
The Semantic Web is not a separate Web but an extension of the current one, in which information is given well-defined meaning, better enabling computers and people to work in cooperation.
In our example, here is what we’ve told the computer:
-
<!DOCTYPE html>
What kind of file this is, so it knows how to parse it.-
<html>
The root element of an HTML page, containing all the content. -
<head>
The meta information about the HTML page—like its title, default language, scripts, and stylesheets.
Nothing in this element is visible on the page itself.<title>
Specifies a title for the page—which is shown in the browser’s tab, and when it is shared.
-
<body>
Defines the document’s body—the container for all the visible contents, such as headings, paragraphs, images, hyperlinks, tables, lists, etc.-
<h1>
Defines a primary/first-level heading. -
<p>
Defines a paragraph.
-
-
We use semantic elements to help structure and describe our content—but also for accessibility (screen readers)—where the tag type helps indicate what things are.
What are elements?
Elements are composed of tags (opening, closing) and their content:
Some elements do not have any content or children, like <br>
or <img>
. These are called empty elements and do not have a closing tag.
Common elements
-
<h1>
<h2>
<h3>
<h4>
<h5>
<h6>
for headings<h1>There should only be one first-level heading!</h1>
-
<p>
for paragraphs<p>You should always wrap your text in a paragraph!</p>
-
<a>
are linksThe
href
(Hypertext REFerence) specifies a URL that the link points to, and the tag wraps the visible link text. Thehref
can point to another, local HTML file (living in the same directory structure) or an external page. They can also point to specific parts of a page.<a href="https://www.example.com">Links need attributes!</a>
-
<img>
are imagesThe
src
likewise can point to a local image file or an external URL!alt
provides a description for accessibility/screen readers.<img src="example.jpg" alt="Images should have descriptions!">
-
<header>
<footer>
<nav>
<main>
<article>
<section>
<div>
define areasThese are the structural containers of a website. The names don’t imbue meaning or function directly, but help us organize and think about our content structure. (And again, are helpful for accessibility.)
<body> <header> <!-- A header. --> </header> <main> <!-- Your main content. --> </main> <footer> <!-- The footer. --> </footer> </body>
-
<span>
<strong>
<em>
wrap around bits of text<p>You may have noticed I like using <em>emphasis</em>.</p>
-
ol
ul
, andli
build lists<ul> <li><!-- A list item. --></li> <li><!-- Another. --></li> <li><!-- A third. --></li> </ul>
There are many, many HTML elements, all with particular uses. (We’ll unpack some more, below.)
Attributes
All HTML elements can have attributes, which provide more information about the element:
Common attributes
-
The
lang
attribute of the<html>
tag declares the language of the Web page.<html lang="en">
-
The
href
attribute of<a>
specifies the URL of the page the link goes to.<a href="https://www.example.com">Goes to example.com</a>
-
The
target
attribute_blank
can tell a<a>
to open in a new window/tab.<a href="https://www.example.com" target="_blank">New tab!</a>
This can be annoying, so use it judiciously!
-
The
style
attribute is used to add styles to an element, such as color, font, size, etc.<p style="color: blue;">This is blue text.</p>
We’ll use CSS for this kind of thing, but know this is how it used to be done and it was brittle and terrible.
-
The
src
attribute of<img>
specifies the path to the image to be displayed, as above.<img src="example.jpg">
-
The
width
andheight
attributes of<img>
provide size information for images. Not required, but helps prevent layout “sloshing” as images load.<img src="example.jpg" width="200px" height="200px">
-
The
alt
attribute of<img>
provides an alternate text for an image, used by screen readers.<img src="example.jpg" alt="A description of the image.">
-
The
id
specifies a singular, unique element on a page—for CSS targeting and anchor (scroll, “jump”) links, prepended with#
.<h2 id="a-heading-element">A heading element</h2> <a href="#a-heading-element">Goes to “a heading element”</a>
-
The
class
attribute provides a selector took hook on with CSS.<p class="warning">We’ll get into this soon.</p>
Case, whitespace, tabs, line breaks
HTML doesn’t care about capitalization, extra white space, or line breaks. The browser will just read everything from left to right, as if it is one long, running sentence. So the shouty <HTML>
and quieter <html>
are interpreted the same.
The browser parses both of these in the exact same way:
<body>
<h1>Dog Breeds</h1>
<p>There are many kind of dog breeds</p>
<ul>
<li>German Shepherd</li>
<li>Bulldog</li>
<li>Poodle</li>
</ul>
</body>
<body><h1>Dog Breeds</h1><p>There are many kind of
dog breeds</p><ul><li>German Shepherd</li>
<li>Bulldog</li><li>Poodle</li></ul></body>
But obviously, the first one is much more readable to us humans. We can use whitespace, tabs/indenting, and line breaks to make it easier for us to read the code. There are a lot of common patterns used—like indenting to indicate hierarchy/nesting. But there are also no wrong ways to do it! In HTML, spaces are code ergonomics for you—just like a good chair or desk, that allow you to work more comfortably.
Code is read more often than it is written. Code should always be written in a way that promotes readability.
Block elements
Block-level elements always start on a new line, and take up the full width available—stretching out to the left and right of their parent/container. They stack on top of each other. Importantly, block elements can have a top and bottom margin, unlike inline elements:
<address>
<article>
<aside>
<blockquote>
<canvas>
<dd>
<div>
<dl>
<dt>
<fieldset>
<figcaption>
<figure>
<footer>
<form>
<h1>
–<h6>
<header>
<hr>
<li>
<main>
<nav>
<noscript>
<ol>
<p>
<pre>
<section>
<table>
<tfoot>
<ul>
Inline elements
Inline elements do not start on a new line, and only take up as much width as necessary. I like to think of these as the little metal slugs from printing. Other text and inline elements will continue to flow around them, and they can wrap to new lines:
<em>
<strong>
<span>
<a>
<img>
So many elements!
Comments
You can comment part of the code and the browser won’t show it. Comments are often used to explain your thinking, organize your code, “turn off” a bit of code, or hide whatever you’d like.
I highly recommend getting into a habit of commenting your code, especially when starting out. If you figure something tricky out, write down why and how you solved it to help you understand and remember. And you’ll often come back to things. Commenting your code is a gift to your future self!
Tables
Tables aren’t used as often anymore, in favor of <div>
and other layout elements. You used to have to use them to get any kind of multi-column, grid layouts. But those need even more CSS!
Lists
Any time you have more than two of something, you probably have a list. These are commonly used for semantic navigation elements, as well, think “here’s a list of links in this site.”
Description lists
There are specific lists for defining things.
Details/summary
There is even some basic interactivity (way, way ahead of JavaScript) with details disclosure elements that open and close.
Again, there are many, many, many, many HTML elements. Try and find the one that best fits your usage, wherever possible using a semantic element that fits your content.
User-agent styles
We haven’t applied any styles/CSS here yet, so everything we see in these examples is based on user-agent stylesheets—that is, each browser’s own default display (and behavior) for an element type. This is what the web was, before CSS! But as a designer, rarely what you want. We’ll get into writing our own styles in the coming weeks.
3.2. Priority of Constituencies
In case of conflict, consider users over authors over implementors over specifiers over theoretical purity. In other words costs or difficulties to the user should be given more weight than costs to authors; which in turn should be given more weight than costs to implementors; which should be given more weight than costs to authors of the spec itself, which should be given more weight than those proposing changes for theoretical reasons alone. Of course, it is preferred to make things better for multiple constituencies at once.