- Home
- /
- Tutorials
- /
- HTML Tutorial
- /
- HTML Elements & Attributes
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, likehref="..."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
- Block-level elements start on a new line and take the full width:
<div>,<h1>,<p>,<table>,<form>. - Inline elements sit inside a line of text:
<span>,<a>,<strong>,<img>.
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>ais the element.hrefis the attribute name.https://html5andcss3.orgis the attribute value.
Common attributes
| Attribute | What it does | Example |
|---|---|---|
| id | A unique name for the element | <h1 id="welcome"> |
| class | One or more names for styling | <p class="note"> |
| href | The address of a link | <a href="https://example.com"> |
| src | The file for an image, video or script | <img src="photo.jpg"> |
| alt | Text that replaces an image | <img alt="Logo"> |
| style | Inline CSS | <p style="color: red;"> |
| title | A 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, notHREF. - 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+ |
|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Yes | Yes |
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
