HTML (HyperText Markup Language) is the code that is used to structure a web page and its content. For example, content could be structured within a set of paragraphs, a list of bulleted points, or using images and data tables. As the title suggests, this article will give you a basic understanding of HTML and its functions.
HTML is a markup language that defines the structure of your content. HTML consists of a series of elements, which you use to enclose, or wrap, different parts of the content to make it appear a certain way, or act a certain way. The enclosing tags can make a word or image hyperlink to somewhere else, can italicize words, can make the font bigger or smaller, and so on. For example, take the following line of content:
My cat is very grumpy
If we wanted the line to stand by itself, we could specify that it is a paragraph by enclosing it in paragraph tags:
p>My cat is very grumpyp>
Let's explore this paragraph element a bit further.
The main parts of our element are as follows:
Elements can also have attributes that look like the following:
Attributes contain extra information about the element that you don't want to appear in the actual content. Here, class is the attribute name and editor-note is the attribute value. The class attribute allows you to give the element a non-unique identifier that can be used to target it (and any other elements with the same class value) with style information and other things. Some attributes have no value, such as required .
Attributes that set a value always have:
Note: Simple attribute values that don't contain ASCII whitespace (or any of the characters " ' ` = < >) can remain unquoted, but it is recommended that you quote all attribute values, as it makes the code more consistent and understandable.
You can put elements inside other elements too — this is called nesting. If we wanted to state that our cat is very grumpy, we could wrap the word "very" in a element, which means that the word is to be strongly emphasized:
p>My cat is strong>verystrong> grumpy.p>
You do however need to make sure that your elements are properly nested. In the example above, we opened the element first, then the element; therefore, we have to close the element first, then the element. The following is incorrect:
p>My cat is strong>very grumpy.p>strong>
The elements have to open and close correctly so that they are clearly inside or outside one another. If they overlap as shown above, then your web browser will try to make the best guess at what you were trying to say, which can lead to unexpected results. So don't do it!
Some elements have no content and are called void elements. Take the element that we already have in our HTML page:
img src="images/firefox-icon.png" alt="My test image" />
This contains two attributes, but there is no closing tag and no inner content. This is because an image element doesn't wrap content to affect it. Its purpose is to embed an image in the HTML page in the place it appears.
That wraps up the basics of individual HTML elements, but they aren't handy on their own. Now we'll look at how individual elements are combined to form an entire HTML page. Let's revisit the code we put into our index.html example (which we first met in the Dealing with files article):
doctype html> html lang="en-US"> head> meta charset="utf-8" /> meta name="viewport" content="width=device-width" /> title>My test pagetitle> head> body> img src="images/firefox-icon.png" alt="My test image" /> body> html>
Here, we have the following:
Let's turn our attention to the element again:
img src="images/firefox-icon.png" alt="My test image" />
As we said before, it embeds an image into our page in the position it appears. It does this via the src (source) attribute, which contains the path to our image file.
We have also included an alt (alternative) attribute. In the alt attribute, you specify descriptive text for users who cannot see the image, possibly because of the following reasons:
The keywords for alt text are "descriptive text". The alt text you write should provide the reader with enough information to have a good idea of what the image conveys. In this example, our current text of "My test image" is no good at all. A much better alternative for our Firefox logo would be "The Firefox logo: a flaming fox surrounding the Earth."
Try coming up with some better alt text for your image now.
Note: Find out more about accessibility in our accessibility learning module.
This section will cover some essential HTML elements you'll use for marking up the text.
Heading elements allow you to specify that certain parts of your content are headings — or subheadings. In the same way that a book has the main title, chapter titles, and subtitles, an HTML document can too. HTML contains 6 heading levels, - , although you'll commonly only use 3 to 4 at most:
h1>My main titleh1> h2>My top level headingh2> h3>My subheadingh3> h4>My sub-subheadingh4>
Note: Anything in HTML between is an HTML comment. The browser ignores comments as it renders the code. In other words, they are not visible on the page - just in the code. HTML comments are a way for you to write helpful notes about your code or logic.
Now try adding a suitable title to your HTML page just above your element.
Note: You'll see that your heading level 1 has an implicit style. Don't use heading elements to make text bigger or bold, because they are used for accessibility and other reasons such as SEO. Try to create a meaningful sequence of headings on your pages, without skipping levels.
p>This is a single paragraphp>
Add your sample text (you should have it from What will your website look like?) into one or a few paragraphs, placed directly below your element.
A lot of the web's content is lists and HTML has special elements for these. Marking up lists always consists of at least 2 elements. The most common list types are ordered and unordered lists:
Each item inside the lists is put inside an (list item) element.
For example, if we wanted to turn the part of the following paragraph fragment into a list
p> At Mozilla, we're a global community of technologists, thinkers, and builders working together… p>
We could modify the markup to this
p>At Mozilla, we're a global community ofp> ul> li>technologistsli> li>thinkersli> li>buildersli> ul> p>working together…p>
Try adding an ordered or unordered list to your example page.
Links are very important — they are what makes the web a web! To add a link, we need to use a simple element — — "a" being the short form for "anchor". To make text within your paragraph into a link, follow these steps:
a>Mozilla Manifestoa>
a href="">Mozilla Manifestoa>
a href="https://www.mozilla.org/en-US/about/manifesto/"> Mozilla Manifesto a>
You might get unexpected results if you omit the https:// or http:// part, called the protocol, at the beginning of the web address. After making a link, click it to make sure it is sending you where you wanted it to.
Note: href might appear like a rather obscure choice for an attribute name at first. If you are having trouble remembering it, remember that it stands for hypertext reference.
Add a link to your page now, if you haven't already done so.
If you have followed all the instructions in this article, you should end up with a page that looks like the one below (you can also view it here):
If you get stuck, you can always compare your work with our finished example code on GitHub.
Here, we have only really scratched the surface of HTML. To find out more, go to our Learning HTML topic.