Last modified: October 14, 2023
This article is written in: 🇺🇸
<p>
for paragraphs, <a>
for links, and <img>
for images.<img>
tag uses the src
attribute to specify the image source, and the alt
attribute for a textual description.<header>
, <footer>
, <article>
, and <nav>
are used to define the purpose and structure of different content areas, enhancing both accessibility and SEO.<div>
and <span>
, to group and organize content, making it easier to apply styles and manage layout.<h1>
for headers, <p>
for paragraphs, <ul>
for lists, and others that define page content.<head>
for metadata, a <body>
for content, and commonly includes <header>
and <footer>
sections.<form>
, <input>
, <textarea>
, and <button>
enable data collection on websites.<img>
, <video>
, and <audio>
facilitate this process.<a>
tag is used to create hyperlinks to other pages or external resources, providing essential connectivity.The HTML document structure provides a standardized way to structure content on the web. Adhering to this structure ensures browser compatibility and proper rendering of web pages.
Below is a foundational structure of an HTML document:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<title>Sample Document</title>
</head>
<body>
<!-- Content goes here -->
</body>
</html>
<!DOCTYPE html>
declaration is an essential instruction for web browsers, identifying the document as HTML5. This declaration ensures that browsers interpret the page using the standards of HTML5.<html lang="en">
element, the root of any HTML page is defined. The lang
attribute, set to "en" in this example, indicates the primary language of the content, which aids in both accessibility and search engine optimization.<head>
section of an HTML document is a container for metadata. Metadata in this section influences how the page is processed by browsers and understood by search engines.<meta charset="UTF-8">
, the document’s character encoding is set to UTF-8, which supports nearly all writing systems worldwide. This encoding is vital for ensuring proper display of text across different languages and characters.<meta name="viewport" content="width=device-width, initial-scale=1.0">
tag is a key component for responsive design. By setting the viewport width to the device width, it ensures the page adjusts properly on various devices, particularly mobile phones and tablets.<title>
tag specifies the name of the document, which appears in the browser tab. An informative and relevant title is crucial for both user experience and search engine optimization, as it describes the page’s content.<body>
tag is the main container for all visible content on the page. Any text, images, videos, and other elements intended for display to the user are included within this tag, making it the core of the HTML document for content rendering.The <head>
section of an HTML document contains metadata (information about the document) and links to resources like stylesheets, scripts, and favicons. While not directly displayed to users, its contents influence how browsers present the page and how search engines interpret it.
The <title>
tag defines the title of the document, which appears in the browser's title bar or tab.
Here is an example:
<title>My Awesome Website</title>
<meta>
tags provide additional details about the document, influencing its behavior on devices and its appearance in search results.
html
<meta charset="utf-8"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<meta content="Discover the latest updates and features of My Awesome Website." name="description"/>
<meta content="innovation, tech, design" name="keywords"/>
<link href="favicon.ico" rel="icon" type="image/x-icon"/>
If the website does not provide an explicit favicon link in the <head>
, browsers default to searching for a file named favicon.ico in the website's root directory.
At the core of HTML lies the concept of tags and attributes. These elements enable the creation of structured documents and provide a way to describe the content's presentation and semantic meaning.
While a vast number of tags are defined in the HTML specification, only a subset is frequently used in day-to-day web development. For a comprehensive list of all available tags, refer to the MDN's HTML element reference.
HTML tags represent elements and come in pairs: an opening tag and a closing tag. The content of the element resides between these tags. Some tags, called void elements (like <img>
and <br>
), don't have a closing tag.
<elementname> Content of the element </elementname>
It's common in HTML to nest one tag within another, establishing a parent-child relationship between the elements.
<parentelement>
<childelement> Content inside child element </childelement>
</parentelement>
This hierarchical structure forms the basis for the Document Object Model (DOM), a tree-like representation of the content.
Attributes provide additional information about an element and are always specified in the opening tag. They come in name/value
pairs like this: name="value"
.
For instance, the tag often uses the href attribute to specify the link destination:
<a href="https://www.example.com">Visit Example</a>
In this example, href is the attribute name, and https://www.example.com
is its value.
Void elements, also known as self-closing elements, are unique HTML tags that don't require a closing tag. While they don't wrap around content, they can have attributes which give them functionality or provide additional context.
<img>
: This is used to embed images in a document. It often comes with attributes such as src
(which specifies the URL of the image) and alt
(which provides an alternate description for the image, aiding accessibility).
<img alt="a cute puppy" src="example.jpg"/>
<br>
: Represents a line break. Useful for creating breaks inside paragraphs or other inline-level content.
This is some text.<br/>And this is a new line.
<input>
: Used within forms to let users input data. It can have a variety of types like text, radio, checkbox, etc.
<input name="username" placeholder="Enter your username" type="text"/>
<hr>
: Produces a thematic break or a horizontal rule. It's often used to separate content sections.
<hr/>
<meta>
, <link>
, and <area>
are other examples of void elements.In HTML, div
and span
are two of the most versatile elements used to control and group content for various purposes, especially when combined with CSS.
div
div
element, short for "division," is a block-level container that serves to group content and organize sections within a webpage. It helps structure different parts of the page visually and functionally.div
occupies the entire width of its parent container, forming a block on the page. This block-level nature distinguishes it from inline elements.div
element itself lacks semantic meaning, it is widely used as a generic container. It becomes more meaningful when combined with CSS classes or IDs for styling, or with JavaScript for added interactivity.span
span
element functions as an inline-level container, making it ideal for isolating specific pieces of content within a larger block. For example, it can be used to highlight text or wrap icons within a paragraph without disrupting the line flow.div
, a span
does not create a new line but stays inline with other content, making it suitable for small adjustments within text or images.div
, the span
element is non-semantic, meaning it has no inherent meaning on its own. It is primarily utilized to apply specific styles or scripts to targeted portions of content within a document.Examples:
div
:
<div class="section">
This is a paragraph within a div.
</div>
span
:
html
This is a <span class="highlight">highlighted</span> text.
<div>
<span>This is a span inside a div.</span>
</div>
However, since div is a block-level element and span is an inline-level element, placing a div inside a span is considered incorrect and goes against HTML specifications:
<!-- Incorrect Usage -->
<span> This is a <div>This is a div</div> span.</span>
Some elements are only used for displaying information and users can't interact with them. We can call them readonly elements.
Basic elements that are used to display text in a document are headers and paragraphs. Headers are used to display titles and paragraphs are used to display large amounts of text. There are many levels of headers, from H1 to H6.
Example:
<h1>This is a header</h1>
This is a paragraph
Links are used to link to other pages. When clicked, the user is taken to the linked page.
Example:
The link below will take the user to the Wikipedia website.
<a href="https://en.wikipedia.org/wiki/HTML">Wikipedia</a>
This link will trigger email client to send an email to the address foo@bar.com
:
<a href="mailto:foo@bar.com">send e-mail</a>
You may also connect links to files stored on the server. Users may download the file by clicking on the link:
<a download="" href="report.txt">report</a>
To display a list of items, we use lists. There are two types of lists: ordered and unordered.
An example of unordered list:
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
Short summary:
The following tags are used to create lists:
Element | Description | Usage | Example |
<ul> |
Unordered List | Creates a bulleted list | <ul><li>Item</li></ul> |
<ol> |
Ordered List | Creates a numbered list | <ol><li>Item</li></ol> |
<li> |
List Item | Represents an item within a list | <ul><li>First item</li></ul> |
Tables are used to display data using rows and columns of cells.
Example:
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
</tr>
<tr>
<td>Cell 4</td>
<td>Cell 5</td>
<td>Cell 6</td>
</tr>
</table>
Short summary:
The following tags are used to create tables:
Element | Description | Usage | Example |
<table> |
Table | Defines a table | <table><tr><td>Data</td></tr></table> |
<tr> |
Table Row | Defines a row within a table | <tr><td>Data</td></tr> |
<th> |
Table Header | Defines a header cell in a table row | <tr><th>Header</th></tr> |
<td> |
Table Cell | Defines a standard cell within a row | <tr><td>Data</td></tr> |
As opposed to readonly elements, there are some elements that users can interact with and trigger actions based on their state.
Buttons are used to trigger actions. They are used to submit forms, to open links, and to trigger media playback.
A simple button:
<button type="submit">press me!</button>
Whenever the button is clicked, the event handler will be triggered.
Short summary:
There are following types of buttons:
Element | Description | Usage | Example |
<button> |
Default Button | Creates a clickable button | <button>Click Me</button> |
<input type="submit"> |
Submit Button | Submits form data to a server | <input type="submit" value="Submit"> |
<input type="reset"> |
Reset Button | Resets all fields in a form to default values | <input type="reset" value="Reset"> |
Input is used to collect data from the user.
A simple example with placeholder text:
<input placeholder="enter your name" type="text"/>
A more complex example with a label and validation:
<label for="name">Name</label>
<input id="name" maxlength="10" minlength="3" name="name" required="" type="text"/>
Short summary:
Input can be one of the many types. The default type is text
. Other types include:
Element | Description | Usage | Example |
<input type="text"> |
Text Input | Creates a single-line text input field | <input type="text" placeholder="Name"> |
<input type="password"> |
Password Input | Creates a field for entering passwords | <input type="password" placeholder="Password"> |
<input type="email"> |
Email Input | Creates a field for email addresses | <input type="email" placeholder="Email"> |
<input type="number"> |
Number Input | Creates a field for numeric input | <input type="number" min="0" max="10"> |
<input type="radio"> |
Radio Button | Creates a radio button for selection | <input type="radio" name="gender" value="male"> |
<input type="checkbox"> |
Checkbox | Creates a checkbox for selection | <input type="checkbox" name="agree"> |
<input type="search"> |
Search Input | Creates a search field | <input type="search" placeholder="Search..."> |
Input attributes include:
Attribute | Description | Usage | Example |
placeholder |
Text that appears when input is empty | Provides a hint inside the input field | <input type="text" placeholder="Enter name"> |
autofocus |
Input is focused when the page loads | Sets the initial focus to this input | <input type="text" autofocus> |
disabled |
Input is disabled | Prevents input and makes it non-editable | <input type="text" disabled> |
readonly |
Input is readonly | Makes the input non-editable but selectable | <input type="text" readonly> |
size |
Size of the input field | Defines the width of the input (in characters) | <input type="text" size="20"> |
list |
List of options for the input | Links input to a <datalist> for suggested options |
<input type="text" list="options"><datalist id="options"><option value="Option1"><option value="Option2"></datalist> |
Possible values for the validation
attribute:
Attribute | Description | Usage | Example |
minlength |
Minimum length of the input | Specifies the minimum number of characters | <input type="text" minlength="5"> |
maxlength |
Maximum length of the input | Specifies the maximum number of characters | <input type="text" maxlength="10"> |
required |
Input is required | Makes the input mandatory before submission | <input type="text" required> |
Forms are way of grouping many inputs in a signle structure.
Let's create a simple form:
<form>
<label for="name">Name</label>
<input id="name" name="name" type="text"/>
<button type="submit">Submit</button>
</form>
In this example, there is a single input field with a label. The label is used to describe the input. The input is used to collect data from the user. The button is used to submit the form.
Short summary:
Element | Description | Usage | Example |
<form> |
Form container | Wraps form elements and allows data submission | <form action="/submit" method="post">...</form> |
<label> |
Label | Provides a label for an input, aiding accessibility | <label for="username">Username:</label> |
<input> |
Input field | Allows various types of user input, such as text, email, and password | <input type="text" id="username"> |
<button> |
Button | Creates a clickable button for form actions, such as submit or reset | <button type="submit">Submit</button> |
Inputs are great, but they are displayed as a single line. Sometimes we need to collect more than one line of text and then we use text areas.
Let's take a look at the following example:
<textarea cols="50" name="description" rows="5">
Enter your description here
</textarea>
In the example above, we have a text area with a name description
which consists of 5 rows and 50 columns.
For styling elements, we generally use CSS. There are however some ways to style text using only HTML.
Text formatting includes making text bold, italic, underlined, and strikethrough.
The following example shows how to make text bold:
This is <b>bold</b> text.
Short summary:
Element | Description | Usage | Example |
b or strong |
Bold Text | Emphasizes text by making it bold | b Bold Text b , strong Bold Text strong |
i or em |
Italic Text | Emphasizes text by making it italicized | i Italic Text i , em Italic Text em |
u |
Underline | Underlines the text | u Underlined Text u |
s |
Strikethrough | Adds a strikethrough effect to the text | s Strikethrough Text s |
There are some special characters that should be escaped in HTML in order to be correctly displayed in the browser.
Example:
This is <b>bold</b> text.
Short summary:
The following characters should always be escaped:
Character Entity | Description | Usage | Example |
& |
Ampersand | Escaped by using & |
& |
< |
Less Than | Escaped by using &lt; |
&lt; |
> |
Greater Than | Escaped by using &gt; |
&gt; |
" |
Double Quote | Escaped by using &quot; |
&quot; |
Any sequence of whitespace characters (space, line break, tab) is treated as a single space in HTML.
We may use three types of media:
Images are specified using the img
tag.
Let's take a look at the following example:
<img alt="A cute cat" src="https://placekitten.com/g/200/300" srcset="https://placekitten.com/g/400/600 400w, https://placekitten.com/g/600/900 600w, https://placekitten.com/g/800/1200 800w"/>
We use the srcset
attribute to specify multiple image paths for different resolutions. The first image path is the one that is displayed by default.
Short summary:
The following attributes are used to specify images:
Attribute | Description | Usage | Example |
src |
Path to the image | Specifies the image file location | img src="image.jpg" alt="Description" |
srcset |
Multiple paths to the image | Provides multiple image sources for different resolutions | img src="image.jpg" srcset="image-2x.jpg 2x" alt="Description" |
alt |
Alternative text | Displays text if the image cannot be loaded | img src="image.jpg" alt="Description" |
Some of the commenly used image formats are:
Format | Description | Usage | Example File Extension |
jpg |
JPEG image | Used for photographs and complex images with gradients | image.jpg |
jpeg |
JPEG image | Same as jpg , often interchangeable |
image.jpeg |
png |
PNG image | Supports transparency, good for graphics and icons | image.png |
gif |
GIF image | Supports animation, limited color palette | image.gif |
svg |
SVG image | Vector format, scalable without quality loss | image.svg |
Videos are specified using the video
tag.
Let's take a look at the following example:
<video controls="">
<source src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm" type="video/webm"/>
<source src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4" type="video/mp4"/>
Your browser does not support HTML5 video.
</video>
A video can have multiple sources. The first source is the one that is displayed by default. The second source is used if the first source is not supported. Finally, the text Your browser does not support HTML5 video.
is displayed if the browser does not support any of the formats.
Short summary:
The following attributes are used to specify videos:
Attribute | Description | Usage | Example |
src |
Path to the video | Specifies the video file location | video src="video.mp4" controls |
type |
Type of the video | Specifies the video format (e.g., video/mp4 ) |
source src="video.mp4" type="video/mp4" |
controls |
Video with playback controls | Displays play, pause, and volume controls | video src="video.mp4" controls |
autoplay |
Automatically plays the video | Begins playing the video upon page load | video src="video.mp4" autoplay |
loop |
Video plays in a loop | Repeats the video automatically when it ends | video src="video.mp4" loop |
Some of the commenly used video formats are:
Format | Description | Usage | Example File Extension |
webm |
WebM video | Open, compressed format for web videos | video.webm |
mp4 |
MP4 video | Widely used video format compatible with most devices | video.mp4 |
Audio is specified using the audio
tag.
Let us take a look at the following example:
<audio controls="">
<source src="https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3" type="audio/mpeg"/>
<source src="https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.ogg" type="audio/ogg"/>
Your browser does not support the audio element.
</audio>
The first source is the one that is played by default. The second source is used if the first source is not supported. Finally, the text Your browser does not support the audio element.
is displayed if the browser does not support any of the formats.
Short summary:
The following attributes are used to specify audio:
Attribute | Description | Usage | Example |
src |
Path to the audio | Specifies the audio file location | audio src="audio.mp3" controls |
type |
Type of the audio | Specifies the audio format (e.g., audio/mpeg ) |
source src="audio.mp3" type="audio/mpeg" |
controls |
Audio with playback controls | Displays play, pause, and volume controls | audio src="audio.mp3" controls |
autoplay |
Automatically plays the audio | Begins playing the audio upon page load | audio src="audio.mp3" autoplay |
loop |
Audio plays in a loop | Repeats the audio automatically when it ends | audio src="audio.mp3" loop |
The following are some of the commonly used audio formats:
Format | Description | Usage | Example File Extension |
mp3 |
MP3 audio | Widely used for compressed audio files | audio.mp3 |
wav |
WAV audio | Uncompressed, high-quality audio format | audio.wav |
<img src="..." alt="..." />
to improve readability and ensure the code conforms to modern standards.article
, time
, header
, nav
, and aside
to structure content effectively, improving both SEO and accessibility.<p>
tags instead of using <br>
tags for spacing, maintaining a clean document structure.<h1>
to <h6>
to provide clear content structure for users and search engines.<button>
and <input>
instead of relying on non-semantic elements styled to look interactive.<label>
tag to create accessible forms that are easier to use for screen reader users.<meta name="author" content="...">
to provide essential information about the page to search engines and browsers.<html>
tag, specifying the primary language of the content, like <html lang="en">
for English.<title>
tag for each page to improve SEO and provide clear navigation information for users.<body>
tag, making it easier for browsers to locate and render the core elements of your page.async
and defer
attributes to prevent JavaScript from blocking the page's initial render.