Skip to main content

HTML Basics

HTML Elements & Attributes

An HTML element is a piece of the page, written with tags: <p>Hello</p> is a paragraph element. An attribute is extra information inside the opening tag, like href="..." on a link. Those two ideas are most of HTML.

What Is an HTML Element?

An element usually has three parts: an opening tag, the content, and a closing tag.

Example

html

<p>This is a paragraph.</p>
  • <p> is the opening (start) tag.
  • </p> is the closing (end) tag, with a slash.
  • "This is a paragraph." is the content.
  • All three together are the paragraph element.

HTML Element Syntax

Example

html

<h1>Hello World!</h1>

Some elements have no content and no closing tag. They are called void elements:

Void Elements Example

html

<br>
<img src="image.jpg" alt="My Image">
<hr>

<br> is a line break, <img> an image and <hr> a dividing line. You may see them written as <br /> in older code; the slash is optional in HTML.

Types of HTML Elements

The difference is explained in Block vs Inline Elements.

What Are HTML Attributes?

An attribute gives an element extra information. It goes inside the opening tag, as a name, an equals sign, and a value in quotes.

Example

html

<a href="https://html5andcss3.org">Visit Our Site</a>
  • a is the element.
  • href is the attribute name.
  • https://html5andcss3.org is the attribute value.

Common attributes

AttributeWhat it doesExample
idA unique name for the element<h1 id="welcome">
classOne or more names for styling<p class="note">
hrefThe address of a link<a href="https://example.com">
srcThe file for an image, video or script<img src="photo.jpg">
altText that replaces an image<img alt="Logo">
styleInline CSS<p style="color: red;">
titleA tooltip on hover<abbr title="HyperText Markup Language">

Attributes that work on every element are listed in HTML Global Attributes.

Attribute Rules

  • Write attributes inside the opening tag, never in the closing tag.
  • Use lowercase names, like href, not HREF.
  • Put values in double quotes. Without quotes, a value with a space breaks.
  • Separate several attributes with spaces. Their order does not matter.

Example

html

<img src="pic.jpg" alt="My Photo" width="400" height="300">

Nesting HTML Elements

Example

html

<p>This is a <strong>bold</strong> word inside a paragraph.</p>

The <strong> is nested inside the <p>. Close tags in the reverse order you opened them: the last one opened is the first one closed. <p><strong>text</p></strong> is wrong.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

HTML Elements & Attributes exercises

Open a new tab

Add a link with the text Open documentation that goes to https://developer.mozilla.org, opens in a new tab and has the rel value noopener.

Result

Check your answer to see it here.