An HTML element is the basic building block of a web page. It is made of a start tag, optional attributes, content and an end tag, and it tells the browser what the content is: a heading, a paragraph, a link, an image or a form control. This tutorial explains the syntax, the main kinds of elements, nesting rules and the semantic elements you should use today.
Most elements have an opening tag, content and a closing tag.
| Part | Example | Meaning |
|---|---|---|
| Start tag | <p class="intro"> | Element name and attributes |
| Attribute | class="intro" | Extra information, written as name and value |
| Content | HTML is the structure of the web. | Text or other elements inside |
| End tag | </p> | Slash plus the element name |
Element names are not case sensitive, but lowercase is the convention. Attribute values should be quoted. The full element, from the start tag to the end tag, is the element; the tag is only the markup that opens or closes it.
Void elements have no content and no end tag. The common ones are <br>, <hr>, <img>, <input>, <meta> and <link>. Writing <br /> is allowed, but the slash does nothing in HTML.
A few elements, such as <li> and <p>, can omit the end tag in some cases. Always write the end tag, because it makes the structure clear and avoids surprises when elements are nested.
Browsers display elements in the normal flow in two main ways. Block-level elements start on a new line and fill the available width. Inline elements sit inside a line of text.
| Display type | Examples | Behaviour |
|---|---|---|
| Block | <div>, <p>, <h1> to <h6>, <ul>, <section> | Start on a new line, stretch to full width |
| Inline | <span>, <a>, <strong>, <em>, <code> | Flow with the text, take only the space they need |
| Inline-block | <img>, <button>, <input> | Sit in a line but accept width and height |
The HTML specification no longer defines "block" and "inline" itself. It groups elements into content categories such as flow content and phrasing content, and CSS controls how they are shown. You can change the display type with the CSS display property.
Use <strong> for importance and <em> for stress emphasis. Use <b> and <i> only for text that is styled without extra meaning. Use headings in order, with one <h1> describing the page.
Elements can contain other elements, and they must close in the reverse order they opened. The browser fixes some mistakes, but the result may not match what you meant.
Some nesting is not allowed by the content model. A <p> cannot contain a <div> or another <p>, and an <a> cannot contain another <a> or an interactive element such as <button>. Use the W3C Nu HTML Checker to find these errors.
Semantic elements describe the role of a part of the page, which helps search engines, screen readers and other people reading your code. Prefer them to a pile of <div> elements.
The <main> element appears once per page. Use <article> for self-contained content, <section> for a themed group with a heading, <nav> for navigation, <aside> for related content and <header> and <footer> for introductory and closing content.
Every element accepts global attributes such as id, class, lang, title, hidden, tabindex and data-* for custom data. Use id for a unique anchor and class for styling groups.
Presentational elements from old HTML are obsolete and should not be used: <center>, <font>, <big>, <marquee>, <frame> and <frameset>. Use CSS for layout and appearance.
| Obsolete element | Use instead |
|---|---|
<center> | CSS text-align: center or flexbox |
<font> | CSS font-family, font-size, color |
<big> | CSS font-size |
<frameset> | Layout with CSS, or <iframe> for embedded content |
A tag is the markup in angle brackets, such as <p> or </p>. An element is the start tag, content and end tag together.
You can define custom elements with the Custom Elements API, and their names must contain a hyphen, for example my-card. Do not invent tags without a hyphen, because they could clash with future standard elements.
Check nesting and the parent's content model first, then the CSS display value. The browser developer tools show the parsed element tree.
An HTML element combines a start tag, attributes, content and an end tag. Use void elements without end tags, nest elements correctly, pick semantic elements for structure and leave styling to CSS. Avoid obsolete elements and validate the markup.