HTML Documentation
This technical documentation provides a clear guide to HTML, covering its basic structure, elements, and key components used in web development.
<Introduction>
HTML, or Hypertext Markup Language, is the backbone of web development. It provides the structure for web pages, allowing developers to format text, embed images, create links, and organize content.
HTML consists of elements represented by tags such as <html>
, <head>
, <body>
, and more. These tags structure content and give instructions to the browser on how to display the page.
<Basic Structure>
The basic structure of an HTML document includes a doctype declaration, an HTML tag, and essential child
elements such as <head>
and
<body>
. These elements form the foundation for any HTML page.
Here's a simple example of the basic structure of an HTML document:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Page Title</title>
</head>
<body>
<h1>Welcome to the Basic HTML Structure</h1>
<p>This is a simple HTML page</p>
</body>
</html>
<Elements and Tags>
In HTML, elements are the building blocks of a webpage, typically consisting of an opening tag, content, and
a closing tag. Tags tell the browser how to display content. For example, the
<h1>
tag defines a top-level heading, and the
<p>
tag defines a paragraph.
Some elements are void elements, meaning they don’t have content and don’t require a closing tag. These
self-closing elements perform a specific function. Examples include the
<img>
tag for images and the
<br>
tag for line breaks. Void elements are used when no surrounding
content is needed, so they close immediately after the opening tag.
<Attributes>
HTML attributes provide additional information about HTML elements. They are used to modify the behavior or style of an element or provide additional functionality. Attributes are always written within the opening tag of an element and consist of a name and a value pair.
Some of the most commonly used attributes in HTML are:
-
id: Assigns a unique identifier to an element, which can be used for styling or
referencing it in JavaScript.
Example:<div id="header">...</div>
-
class: Allows you to assign one or more class names to an element. These class
names are used by CSS to apply styles to elements.
Example:<div class="container"<...</div>
-
href (Hyperlink Reference): Is used to specify the URL for links (
<a>
tags).
Example:<a href="https://www.example.com"<Click here</a<
-
style: Allows you to directly apply CSS styles to an element.
Example:<p style="color: blue;"<This text is blue.</p<
<Headings and Paragraphs>
Headings and paragraphs are fundamental elements in HTML, used to structure content and make it more readable. They help organize the text and give it a clear hierarchy, which improves both user experience and accessibility.
Headings in HTML range from <h1>
to
<h6>
, with <h1>
being the highest level, usually reserved for the main
title of the page, and <h6>
for the least important headings. Each
level should be used to represent a different level of content importance.
<h1>This is a level 1 heading</h1>
<h2>This is a level 2 heading</h2>
<h3>This is a level 3 heading</h3>
Here's an example of how headings and paragraphs are used together to create a well-structured document:
<h1>HTML Basics</h1>
<p>HTML is the language used to create web pages.</p>
<h2>Headings</h2>
<p>Headings are used to create titles and organize content.</p>
<h2>Paragraphs</h2>
<p>Paragraphs group sentences into blocks of text for readability.</p>
<Lists: Ordered and Unordered>
In HTML, lists are used to group related items together. There are two main types of lists: ordered lists and unordered lists.
-
Ordered Lists are used when the sequence of items is important. They are created with the
<ol>
tag, and each item is wrapped in the<li>
tag. By default, ordered lists display numbers (1, 2, 3, etc.) to indicate their position in the sequence. This makes them ideal for instructions, rankings, or processes that need to be followed in a specific order.
Example of an ordered list:<ol> <li>Preheat the oven to 350°F (175°C).</li> <li>Mix the flour and sugar in a bowl.</li> <li>Bake for 20 minutes or until golden brown.</li> </ol>
-
Unordered Lists are used when the order of items does not matter. They are created with the
<ul>
tag, and each item is wrapped in the<li>
tag. By default, unordered lists display bullet points instead of numbers. These lists are ideal for listing items where the sequence isn’t important, such as product features or general options.
Example of an unordered list:<ul> <li>Fast performance</li> <li>High-quality display</li> <li>Long battery life</li> </ul>
<Images>
Images play a crucial role in web design by enhancing the visual appeal of a webpage, conveying information, and improving user engagement.
To add an image in HTML, you use the <img>
element. Unlike most other
HTML elements, the <img>
tag is a void element, or self-closing,
meaning it doesn't require a closing tag.
Some of the most commonly used attributes with the <img>
element
include:
-
src (Source): Is the most important attribute and specifies the path to the
image you want to display. It can point to an image on your website or an external image hosted
elsewhere.
Example:<img src="logo.png">
-
alt (Alternative Text): Is used to describe the image for users who cannot see
it, including those using screen readers or in cases where the image doesn't load. This improves
accessibility and ensures all users can understand the content of the image.
Example:<img src="photo.jpg" alt="A beautiful sunset over the mountains">
-
width and height: You can specify the size of the image using the
width
andheight
attributes, either in pixels or percentage. This helps control the image's display size on the page.
Example:<img src="photo.jpg" width="500" height="300">
-
title: Can provide additional information about the image. This information is
typically displayed as a tooltip when the user hovers over the image.
Example:<img src="photo.jpg" title="Click for more details">
<Tables>
Tables are essential for organizing data in rows and columns on web pages. They allow for a clear, structured way to display information such as financial reports, schedules, or any data that fits a tabular format. When used correctly, tables can enhance readability and make complex data easier to interpret.
To create a table in HTML, you use the <table>
element, along with
various other elements to define rows, headers, and cells.
Key HTML Elements for Tables:
-
<table>
: This is the container element that holds the entire table structure. -
<thead>
,<tbody>
, and<tfoot>
: These elements are used to group the header, body, and footer sections of a table, respectively. They help in structuring the table for better accessibility and usability. -
<tr>
(table row): Is used to define a row of cells in the table. -
<th>
(table header): Is used to define a cell as a header. Typically, it is bold and centered by default. -
<td>
(table data): Is used to define a cell that contains regular data in the table. -
<caption>
: This is an optional element that provides a title or description for the table, displayed above or below the table.
Code Example:
The following HTML code creates a basic table with three rows and three columns.
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row 1, Col 1</td>
<td>Row 1, Col 2</td>
<td>Row 1, Col 3</td>
</tr>
<tr>
<td>Row 2, Col 1</td>
<td>Row 2, Col 2</td>
<td>Row 2, Col 3</td>
</tr>
<tr>
<td>Row 3, Col 1</td>
<td>Row 3, Col 2</td>
<td>Row 3, Col 3</td>
</tr>
</tbody>
</table>
Rendered Table:
Here’s how the above code will display as an actual table in a browser.
Header 1 | Header 2 | Header 3 |
---|---|---|
Row 1, Col 1 | Row 1, Col 2 | Row 1, Col 3 |
Row 2, Col 1 | Row 2, Col 2 | Row 2, Col 3 |
Row 3, Col 1 | Row 3, Col 2 | Row 3, Col 3 |
<Forms and Input>
Forms are key for collecting user data on websites. HTML provides input elements that let users enter text, select options, check boxes, or upload files. These simple inputs gather information like names, emails, preferences, and feedback.
Labels make forms more user-friendly and accessible. The <label>
tag,
paired with the for
attribute, connects directly to its input, helping users
understand the required information and making the form easier to navigate, especially for screen reader
users.
A typical form contains the following parts: