HTML Elements
An HTML element is defined by a start tag, an end tag, and the content in between. Most HTML elements can be nested.
What is an HTML Element?
An HTML element is the building block of an HTML document. It is represented by tags. For example, the following is a paragraph element:
<p>This is a paragraph.</p>
Key characteristics of an HTML element:
- It starts with a start tag (e.g.,
<p>). - It ends with an end tag (e.g.,
</p>). - The content is placed between the start and end tags.
- Some elements, like the line break element (
<br>) or image element (<img>), are called "empty" or "void" elements because they don't have content and don't need an end tag. They are often written with a closing slash (e.g.,<br />or<img src="..." alt="..." />), though this is not strictly required in HTML5.
Common HTML Elements
Here are some of the most fundamental and frequently used HTML elements:
Block-level vs. Inline Elements
HTML elements are typically classified as either block-level or inline.
- Block-level elements start on a new line and take up the full width available. Examples include
<div>,<p>,<h1>to<h6>,<ul>,<ol>,<li>,<form>. - Inline elements do not start on a new line and only take up as much width as necessary. Examples include
<a>,<span>,<strong>,<em>,<img>,<input>.
Structure of an HTML Element
Most HTML elements have the following structure:
<tagname attribute="value">Content goes here</tagname>
Example: The Anchor (Link) Element
The <a> element is used to create hyperlinks. It requires the href attribute to specify the URL of the page the link points to.
Link Example
This is a link to the HTML Introduction page.
<a href="/msdn/documentation/html/introduction.html">HTML Introduction</a>
Example: The Image Element
The <img> element is used to embed images. It requires the src attribute for the image source and the alt attribute for alternative text.
Image Example (Placeholder)
<img src="image.jpg" alt="Description of image" />
Common HTML Element Categories
| Category | Description | Examples |
|---|---|---|
| Document Structure | Elements that define the overall structure of the HTML document. | <html>, <head>, <body>, <title> |
| Metadata | Elements that provide metadata about the HTML document. | <meta>, <link>, <style> |
| Sectioning Content | Elements that define sections of a document, like headings and navigation. | <article>, <aside>, <nav>, <section>, <header>, <footer> |
| Heading Content | Elements that define headings for sections. | <h1>, <h2>, <h3>, <h4>, <h5>, <h6> |
| Phrasing Content | Elements that define text and inline elements. | <p>, <a>, <span>, <strong>, <em>, <code>, <img> |
| Embedded Content | Elements that embed other content into the document. | <img>, <audio>, <video>, <iframe> |
| Form Elements | Elements used to create interactive forms. | <form>, <input>, <textarea>, <button>, <select>, <label> |
Nesting Elements
Elements can be nested inside other elements. This is crucial for creating complex structures. For example, a <ul> (unordered list) element can contain multiple <li> (list item) elements.
<ul>
<li>First item</li>
<li>Second item</li>
</ul>
Conclusion
Understanding HTML elements is fundamental to web development. Each element has a specific purpose and structure, and they work together to form the content and layout of web pages.