Learn HTML: Elements and Structure

In this course, you will learn about HTML elements and structure.

StartHTML Illustration

Key Concepts

Review core concepts you need to learn to master this subject

Document type declaration

The document type declaration <!DOCTYPE html> is required as the first line of an HTML document. The doctype declaration is an instruction to the browser about what type of document to expect and which version of HTML is being used, in this case it’s HTML5.

HTML element

The <html> element, the root of an HTML document, should be added after the doctype declaration. All content/structure for an HTML document should be contained between the opening and closing <html> tags.

The head element

The <head> element contains general information about an HTML page that isn’t displayed on the page itself. This information is called metadata and includes things like the title of the HTML document and links to stylesheets.

Title element

The <title> element contains a text that defines the title of an HTML document. The title is displayed in the browser’s title bar or tab in which the HTML page is displayed. The <title> element can only be contained inside a document’s <head> element.

Anchor element

The anchor element, <a>, is used to create hyperlinks in an HTML document. The hyperlinks can point to other webpages, files on the same server, a location on the same page, or any other URL via the hyperlink reference attribute, href. The href determines the location the anchor element points to.

Target attribute

The target attribute on an anchor (<a>) element specifies where a hyperlink should be opened. For example, a target value of ”_blank” will tell the browser to open the hyperlink in a new tab in modern browsers, or in a new window in older browsers or if the browser has had settings changed to open hyperlinks in a new window.

File paths

URL paths in HTML can be absolute paths, like a full URL, for example: https://developer.mozilla.org/en-US/docs/Learn or a relative file path that links to a local file in the same folder or on the same server, for example: ./style.css. Relative file paths begin with ./ followed by a path to the local file. ./ tells the browser to look for the file path from the current folder.

HTML anchor tags

HTML anchor (<a>) tags can surround and hyperlink content to a specified url. They can be applied to many different kinds of content, including <img> elements.

Linking to location in the same document

The HTML anchor element, <a> can create hyperlinks to different parts of the same HTML document using the href attribute to point to the desired location with # followed by the id of the element to link to.

Whitespace in HTML

Whitespace added to an HTML document between block-level elements will generally be ignored by the browser and are not added to increase spacing on the rendered HTML page. Rather, whitespace is added for organization and easier reading of the HTML document itself.

Indenting nested HTML code

HTML code should be formatted such that the indentation level of text increases once for each level of nesting. The W3C standards recommend two spaces of indentation per level of nesting.

Adding comments in HTML

In HTML, comments can be added between an opening <!-- and closing -->. Content inside of comments will not be rendered by browsers, and are usually used to describe a part of code or provide other details. Comments can span single or multiple lines.

HTML Acronym and Markup Languages

The acronym HTML stands for HyperText Markup Language. A markup language is a computer annotation system for defining the structure and presentation of text.

HTML Usage

HTML is used to give content to a web page and instructs web browsers on how to structure that content.

HTML Element Definition and Syntax

An HTML element is a piece of content in an HTML document and uses the following syntax: opening tag + content + closing tag. In the above code block, <p> is the opening tag, Hello World! is the content, and </p> is the closing tag.

HTML Tag Syntax

The syntax for a single HTML tag is an opening angle bracket (<) followed by the element name and closed with the closing angle bracket (>). The above code block is an example of a single opening <div> tag.

HTML Element Content

The content of an HTML element is the information between the opening and closing tags of an element. In the above example the element content is “Codecademy is the best!”.

HTML Element Closing Tag

An HTML closing tag is used to denote the end of an HTML element. The syntax for a closing tag is a left angle bracket (<) followed by a forward slash (/) then the element name and a right angle bracket to close (>).

The Body Element Syntax and Usage

We create the <body> element by using the opening and closing <body> tags. Web browsers will render any content inside the <body> tags. In this code block “Learn to code with Codecademy :)” will be rendered to the browser.

The Body Element Usage (cont.)

The <body> element should contain all content meant to be rendered to a web page. There can be one <body> element per HTML document.

HTML Structure

HTML is organized into a family tree structure. HTML elements can have parents, grandparents, siblings, children, grandchildren, etc.

HTML Heading Elements

HTML can use six different levels of heading elements. The heading elements are ordered from highest level (<h1>) to lowest level (<h6>).

The Div Element

The <div> element is used as a container that divides an HTML document into sections and is short for “division”. <div> elements can contain flow content such as headings, paragraphs, links, images, etc.

HTML Attributes

HTML attributes are values we can use on the opening tag of an element to configure the element or change the element’s default behavior. In the above code block we are giving the <p> (paragraph) element a unique identifier using the id attribute and changing the color of the default text using the style attribute.

HTML attribute name and values

HTML attributes consist of a name and a value using the following syntax: name=”value” and can be added to the opening tag of an HTML element to configure or change the behavior of the element.

Unique ID Attributes

In HTML, specific and unique id attributes can be assigned to different elements in order to differentiate between them. When needed, the id value can be called upon by CSS and Javascript to manipulate, format, and perform specific instructions on that element and that element only. Valid id attributes can only contain capital letters (A-Z), lowercase letters (a-z), hyphens (-), underscores (_), colons (:), and periods (.).

Paragraph elements

Paragraph elements, <p>, contain and display blocks of text.

Span element

The <span> element is an inline container for text and can be used to group text for styling purposes. However, as <span> is a generic container to separate pieces of text from a larger body of text, its use should be avoided if a more semantic element is available.

Emphasis element

The emphasis element, <em>, emphasizes text and browsers will usually italicize the emphasized text by default.

Strong element

The <strong> element highlights important, serious, or urgent text and browsers will normally render this highlighted text in bold by default.

Line break element

The line break element, <br>, will create a line break in text and is especially useful where division of text is required, like in an address. The line break element requires only an opening tag and must not have a closing tag.

Unordered list elements

The unordered list element, <ul>, is used to create a list of items in no particular order. Each individual list item will have a bullet point by default.

List item element

List item elements (<li>) create list items in ordered lists (<ol>) and unordered lists (<ul>). List items must be added to ordered and unordered lists using the <li> element.

HTML Ordered lists

HTML ordered list (<ol>) elements are used to create lists of items with a sequential order. Each list item in an ordered list appears numbered by default.

HTML image elements

HTML image (<img>) elements embed images in documents. The src attribute contains the image URL and is mandatory. <img> is an empty element and should not have a closing tag.

HTML alt attribute

An <img> element can have alternative text via the alt attribute. The alternative text will be displayed if an image fails to render due to an incorrect URL, if the image format is not supported by the browser, if the image is blocked from being displayed, or if the image has not been received from the URL.

HTML alt attribute for screen-reading

The alternative text on an image element, the alt attribute value, will be read aloud if screen reading software is used and helps support visually impaired users by providing a text descriptor for the image content on a webpage.

Video element

The <video> element embeds a media player into an HTML document for video playback. The src attribute will contain the URL to the video. Adding the controls attribute will display video controls in the media player.

Video element with text content

The <video> element can contain text content between its opening and closing tags which will be displayed if the browser does not support the video format or if the video fails to load.

Chevron Left Icon
Introduction to HTML
Lesson 1 of 2
Chevron Right Icon
  1. 1

    Welcome to the world of code! Last year, millions of learners from our community started with HTML. Why? HTML is the skeleton of all web pages. It's often the first language learned by marketers an...

  2. 2

    HTML is composed of elements. These elements structure the webpage and define its content. Let's take a look at how they're written. The diagram to the right displays an HTML paragraph element. ...

  3. 3

    One of the key HTML elements we use to build a webpage is the body element. Only content inside the opening and closing body tags can be displayed to the screen. Here's what opening and closing b...

  4. 4

    HTML is organized as a collection of family tree relationships. As you saw in the last exercise, we placed [...] tags within [...] tags. When an element is contained inside another element, it...

  5. 5

    Headings in HTML are similar to headings in other types of media. For example, in newspapers, large headings are typically used to capture a reader's attention. Other times, headings are used to de...

  6. 6

    One of the most popular elements in HTML is the [...] element. [...] is short for "division" or a container that divides the page into sections. These sections are very useful for grouping elem...

  7. 7

    If we want to expand an element's tag, we can do so using an attribute. Attributes are content added to the opening tag of an element and can be used in several different ways, from providing inf...

  8. 8

    If you want to display text in HTML, you can use a paragraph or span: - Paragraphs ( [...] ) contain a block of plain text. - [...] contains short pieces of text or other HTML. They are use...

  9. 9

    You can also style text using HTML tags. The [...] tag emphasizes text, while the [...] tag highlights important text. Later, when you begin to style websites, you will decide how you want br...

  10. 10

    The spacing between code in an HTML file doesn't affect the positioning of elements in the browser. If you are interested in modifying the spacing in the browser, you can use HTML's line break el...

  11. 11

    In addition to organizing text in paragraph form, you can also display content in an easy-to-read list. In HTML, you can use an unordered list tag ( [...] ) to create a list of items in no parti...

  12. 12

    Ordered lists ( [...] ) are like unordered lists, except that each list item is numbered. They are useful when you need to list different steps in a process or rank items for first to last. Yo...

  13. 13

    All of the elements you've learned about so far (headings, paragraphs, lists, and spans) share one thing in common: they're composed entirely of text! What if you want to add content to your web pa...

  14. 14

    Part of being an exceptional web developer is making your site accessible to users of all backgrounds. In order to make the Web more inclusive, we need to consider what happens when assistive techn...

  15. 15

    In addition to images, HTML also supports displaying videos. Like the [...] tag, the [...] tag requires a [...] attribute with a link to the video source. Unlike the [...] tag however, the ...

  16. 16

    Congratulations on completing the first lesson of HTML! You are well on your way to becoming a skilled web developer. Let's review what you've learned so far: 1. HTML stands for Hyper*T...

  1. 1

    Now that you've learned about some of the most common HTML elements, it's time to learn how to set up an HTML file. HTML files require certain elements to set up the document properly. You can le...

  2. 2

    The [...] declaration provides the browser with two pieces of information (the type of document and the HTML version to expect), but it doesn't actually add any HTML structure or content. To c...

  3. 3

    So far you've done two things to set up the file properly: Declared to the browser that your code is HTML with [...] Added the HTML element ( [...] ) that will contain the rest of your code. ...

  4. 4

    What kind of metadata about the web page can the [...] element contain? If you navigate to the Codecademy catalog and look at the top of your browser, you'll notice the words [...] , which ...

  5. 5

    Good work! Unfortunately, the browser panel used by the Codecademy environment does not have a title bar, so the "Brown Bear" title you wrote in the previous exercise will not be displayed. Outside...

  6. 6

    One of the powerful aspects of HTML (and the Internet), is the ability to link to other web pages. You can add links to a web page by adding an anchor element [...] and including the text o...

  7. 7

    Have you ever clicked on a link and observed the resulting web page open in a new browser window? If so, you can thank the [...] element's [...] attribute. The [...] attribute specifies how ...

  8. 8

    Thus far you have learned how to link to external web pages. Many sites also link to internal web pages like Home, About, and Contact. Before we learn how to link between internal pages, let's est...

  9. 9

    You've probably visited websites where not all links were made up of text. Maybe the links you clicked on were images or some other form of content. So far, we've added links that were made up of ...

  10. 10

    At this point, we have all the content we want on our page. Since we have so much content, it doesn't all fit on the screen. How do we make it easier for a user to jump to different portions of our...

  11. 11

    The rest of this lesson will focus on some tools developers use to make code easier to interpret. As the code in an HTML file grows, it becomes increasingly difficult to keep track of how elements...

  12. 12

    The second tool web developers use to make the structure of code easier to read is indentation. The spaces are inserted using the space and tab bars on your keyboard. The World Wide Web C...

  13. 13

    HTML files also allow you to add comments to your code. Comments begin with [...] and end with [...] . Any characters in between will be ignored by your browser. [...] Including comments in...

  14. 14

    You now know all of the basic elements and set-up you need to structure an HTML page and add different types of content. With the help of CSS, very soon you’ll be creating beautiful websites! Whil...

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo

Learn HTML: Elements and Structure

StartHTML Illustration