HTML (HyperText Markup Language) is a markup language that tells web
browsers how to structure the web pages you visit. It can be as
complicated or as simple as the web developer wants it to be. HTML
consists of a series of elements, which you use to enclose, wrap, or
mark up different parts of content to make it appear or act in a
certain way. The enclosing tags can make content into a hyperlink to
connect to another page, italicize words, and so on. For example,
consider the following line of text:
My cat is very grumpy
If we wanted the text to stand by itself, we could specify that it
is a paragraph by enclosing it in a paragraph
<p>
element:
<p>My cat is very grumpy</p>
Tags in HTML are not case-sensitive. This means they can be written
in uppercase or lowercase.
One should have a basic knowledge of:
- Computer syntax
- Software installation
- Working with computer files
One should have access to:
- Computer with Internet access
- Code text editor
- Patience
Let's further explore our paragraph element from the previous
section:
The anatomy of our element is:
-
The opening tag: This consists of the name of the element
(in this example, p for paragraph), wrapped in opening and closing
angle brackets. This opening tag marks where the element begins or
starts to take effect. In this example, it precedes the start of
the paragraph text.
-
The content: This is the content of the element. In this
example, it is the paragraph text.
-
The closing tag: This is the same as the opening tag,
except that it includes a forward slash before the element name.
This marks where the element ends. Failing to include a closing
tag is a common beginner error that can produce peculiar results.
The element is the opening tag, followed by content, followed by the
closing tag.
Nesting Elements
Elements can be placed within other elements. This is called
nesting. If we wanted to state that our cat is very grumpy, we could
wrap the word "very" in a <strong>
element,
which means that the word is to have strong(er) text formatting:
<p>My cat is <strong>very</strong> grumpy.
There is a right and wrong way to do nesting. In the example above,
we opened the p
element first, then opened the
strong
element. For proper nesting, we should close the
strong
element first, before closing the
p
.
Block versus inline elements
There are two important categories of elements to know in HTML:
block-level elements and inline elements.
-
Block-level elements form a visible block on a page. A block-level
element appears on a new line following the content that precedes
it. Any content that follows a block-level element also appears on
a new line. Block-level elements are usually structural elements
on the page. For example, a block-level element might represent
headings, paragraphs, lists, navigation menus, or footers. A
block-level element wouldn't be nested inside an inline element,
but it might be nested inside another block-level element.
-
Inline elements are contained within block-level elements, and
surround only small parts of the document's content (not entire
paragraphs or groupings of content). An inline element will not
cause a new line to appear in the document.
Elements can also have attributes. Attributes look like this:
Attributes contain extra information about the element that won't
appear in the content. In this example, the
class
attribute is an identifying name used to target
the element with style information.
An attribute should have:
-
A space between it and the element name. (For an element with more
than one attribute, the attributes should be separated by spaces
too.)
- The attribute name, followed by an equal sign.
-
An attribute value, wrapped with opening and closing quote marks.
Boolean attributes
Sometimes you will see attributes written without values. This is
entirely acceptable. These are called Boolean attributes. Boolean
attributes can only have one value, which is generally the same as
the attribute name.
Individual HTML elements aren't very useful on their own. Next,
let's examine how individual elements combine to form an entire HTML
page:
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8" />
<title>My test page</title>
</head>
<body>
<p>This is my page</p>
</body>
</html>
Here we have:
-
<!DOCTYPE html>
: The document type. When HTML
was young (1991-1992), doctypes were meant to act as links to a
set of rules that the HTML page had to follow to be considered
good HTML. More recently, the doctype is a historical artifact
that needs to be included for everything else to work right.
-
<html>
: This element wraps all the content on
the page. It is sometimes known as the root element.
-
<head>
: This element acts as a container for
everything you want to include on the HTML page, that isn't the
content the page will show to viewers. This includes keywords and
a page description that would appear in search results, CSS to
style content, character set declarations, and more.
-
<meta charset="utf-8">
: This element represents
metadata that cannot be represented by other HTML meta-related
elements. Also sets the character set for your document to UTF-8,
which includes most characters from the vast majority of human
written languages. With this setting, the page can now handle any
textual content it might contain. There is no reason not to set
this, and it can help avoid some problems later.
-
<title>
: This sets the title of the page, which
is the title that appears in the browser tab the page is loaded
in. The page title is also used to describe the page when it is
bookmarked.
-
<body>
: This contains all the content that
displays on the page, including text, images, videos, games,
playable audio tracks, or whatever else.
Whitespace in HTML
No matter how much whitespace you use inside HTML element content
(which can include one or more space character, but also line
breaks), the HTML parser reduces each sequence of whitespace to a
single space when rendering the code. So why use so much whitespace?
The answer is readability.
It can be easier to understand what is going on in your code if you
have it nicely formatted. In our HTML we've got each nested element
indented by two spaces more than the one it is sitting inside. It is
up to you to choose the style of formatting (how many spaces for
each level of indentation, for example), but you should consider
formatting it.
In HTML, the characters <, >," ,'
and
&
are special characters. They are parts of the
HTML syntax itself. So how do you include one of these special
characters in your text? For example, if you want to use an
ampersand or less-than sign, and not have it interpreted as code.
You do this with character references. These are special codes that
represent characters, to be used in these exact circumstances. Each
character reference starts with an ampersand (&), and ends with a
semicolon (;).
The character reference equivalent could be easily remembered
because the text it uses can be seen as less than for '<',
quotation for '" ' and similarly for others.
At this point, you should understand what HTML looks like, and how it
works at a basic level. You should also be able to write a few
elements and attributes. The subsequent articles of this module go
further on some of the topics introduced here, as well as presenting
other concepts of the language.