HTML Notes
HTML Notes
Topics
1. Basic Structure
2. Semantic HTML
Header <header>
Navigation <nav>
Section <section>
Article <article>
Aside <aside>
Footer <footer>
3. Forms
Form <form>
Input <input>
Textarea <textarea>
Select <select> and Option <option>
Button <button>
4. Links and Images
Anchor <a>
Image <img>
Image Map <map> and <area>
5. Tables
Table <table>
Table Header <thead> , Body <tbody> , and Footer <tfoot>
Table Cell <td> and Header Cell <th>
6. Lists
Ordered List <ol>
Unordered List <ul>
Definition List <dl> , <dt> , <dd>
7. Media
Image <img>
Audio <audio>
Video <video>
Embedding Content
8. Metadata
Topics
1. Basic Structure: Understand the basic structure of an HTML document,
including the <!DOCTYPE> declaration, <html> , <head> , and <body> tags.
4. Links and Images: Learn how to create hyperlinks using <a> and include
images using <img> .
5. Tables: Understand how to create tables using <table> , <tr> , <td> , <th> , etc.,
and how to style them using CSS.
6. Lists: Learn about ordered <ol> and unordered <ul> lists, as well as list items
<li> .
7. Media: Understand how to embed media such as audio and video using
<audio> and <video> tags.
8. Metadata: Learn about the <meta> tag and its importance for providing
metadata about the HTML document.
11. SEO (Search Engine Optimization): Understand basic SEO principles and how
to optimize HTML content for search engines.
12. HTML5 APIs: Learn about HTML5 APIs such as Geolocation, Web Storage,
Web Workers, and Canvas for advanced web development features.
13. Best Practices: Understand best practices for writing clean, maintainable, and
accessible HTML code.
14. Integration with CSS and JavaScript: Understand how HTML integrates with
CSS for styling and JavaScript for interactivity.
15. Version Control: Learn how to use version control systems like Git to manage
your HTML files and collaborate with others.
1. Basic Structure
HTML, which stands for Hypertext Markup Language, is the standard markup
language used to create web pages. Understanding the basic structure of an
HTML document is fundamental to building web pages.
An HTML document starts with a <!DOCTYPE> declaration, which specifies the HTML
version being used. This is followed by the <html> element, which contains the
entire HTML document. Inside the <html> element, there are two main sections:
the <head> and the <body> .
The <head> section contains meta-information about the document, such as the
page title, character set, and links to external resources like stylesheets and
scripts. The <body> section contains the content of the web page, including text,
images, links, and other elements.
Here is a basic example of an HTML document structure:
htmlCopy code
<!DOCTYPE html>
<html>
<head>
In this example, the <!DOCTYPE html> declaration specifies that the document is an
HTML5 document. The <html> element contains the entire document, and the
<head> element contains meta-information and links to external resources. The
2. Semantic HTML
Semantic HTML elements are tags that clearly describe their meaning in the web
page content, rather than just how they should look. Using semantic elements
properly can improve the accessibility, SEO, and maintainability of your web
pages.
Header <header>
The <header> element represents a group of introductory or navigational aids. It
typically contains a logo or site name, along with navigation links or other
elements related to the header of a page.
Example:
htmlCopy code
<header>
<h1>My Website</h1>
Navigation <nav>
The <nav> element is used to define a section of navigation links that allow users
to navigate the website. It typically contains a list of links to different sections or
pages of the website.
Example:
htmlCopy code
<nav>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#services">Services</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
Section <section>
The <section> element defines a section of a document or application. It is often
used to group related content together, such as a blog post, a chapter in a book,
or a set of FAQs.
Example:
Article <article>
The <article> element represents a self-contained piece of content that could be
distributed and independently understood. It can be used for blog posts, news
articles, forum posts, etc.
Example:
htmlCopy code
<article>
<h2>Blog Post Title</h2>
<p>Content of the blog post...</p>
</article>
Aside <aside>
The <aside> element represents content that is tangentially related to the content
around it. It is often used for sidebars or content that is not central to the main
content of the page.
Example:
htmlCopy code
<aside>
Footer <footer>
The <footer> element represents a footer for its nearest sectioning content or
sectioning root element. It typically contains information about the author,
copyright information, and links to related documents.
Example:
htmlCopy code
<footer>
<p>© 2024 My Website. All rights reserved.</p>
</footer>
Using semantic HTML elements not only improves the structure and readability of
your code but also enhances the accessibility and SEO of your web pages.
3. Forms
HTML forms are used to collect user input, such as text, selections, and buttons.
They are essential for interactive websites and can be created using various form
elements and attributes.
Form <form>
The <form> element is used to create an HTML form for user input. It can contain
various form elements like text fields, checkboxes, radio buttons, etc., and has
attributes for defining the form's action (where the form data is sent) and method
(how the form data is sent).
htmlCopy code
<form action="/submit-form" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br>
<br>
Input <input>
The <input> element is used to create various types of input fields, such as text
fields, checkboxes, radio buttons, etc. It has attributes like type (specifying the
type of input field) and name (used to identify the input field when the form is
submitted).
htmlCopy code
<label for="email">Email:</label>
<input type="email" id="email" name="email">
Example (checkbox):
htmlCopy code
<input type="checkbox" id="subscribe" name="subscribe" value
="yes">
<label for="subscribe">Subscribe to our newsletter</label>
htmlCopy code
<label for="message">Message:</label>
<textarea id="message" name="message" rows="4" cols="50"></te
xtarea>
htmlCopy code
<label for="country">Country:</label>
<select id="country" name="country">
<option value="us">United States</option>
<option value="ca">Canada</option>
<option value="uk">United Kingdom</option>
</select>
Button <button>
The <button> element is used to create a clickable button within a form. It can be
used to submit the form, reset the form, or trigger a JavaScript function.
Example:
htmlCopy code
<button type="submit">Submit</button>
HTML forms are a crucial part of web development, allowing you to collect user
input and interact with users. Understanding how to create and use HTML forms is
essential for building dynamic and interactive web pages.
Anchor <a>
The <a> element, also known as the anchor element, is used to create hyperlinks
to other web pages, files, locations within the same page, or email addresses.
Example (external link):
htmlCopy code
<a href="https://www.example.com">Visit Example</a>
htmlCopy code
<a href="#section2">Jump to Section 2</a>
Image <img>
The <img> element is used to embed images in a web page. It has attributes for
specifying the image source (URL), alternative text (for accessibility), width,
height, and more.
Example:
Example:
htmlCopy code
<img src="planets.jpg" alt="Planets" usemap="#planetmap">
<map name="planetmap">
<area shape="rect" coords="0,0,82,126" href="sun.html" al
t="Sun">
<area shape="circle" coords="90,58,3" href="mercury.html"
alt="Mercury">
<area shape="circle" coords="124,58,8" href="venus.html"
alt="Venus">
</map>
Using links and images effectively in your web pages can improve navigation and
user experience. Understanding how to create links to different destinations and
embed images can enhance the visual appeal and functionality of your web
pages.
5. Tables
Tables in HTML are used to display data in a tabular format. They consist of rows
( <tr> ) and cells ( <td> for regular cells, <th> for header cells), organized into
columns.
Example:
htmlCopy code
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>John</td>
<td>25</td>
</tr>
<tr>
<td>Jane</td>
<td>30</td>
</tr>
</table>
Example:
htmlCopy code
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
element is used to create header cells (which are typically bold and centered).
Example:
htmlCopy code
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>John</td>
<td>25</td>
</tr>
Using tables appropriately can help you present data in a structured and
organized manner on your web pages. However, it's important to use tables for
tabular data and not for layout purposes, as this can affect accessibility and
responsiveness.
6. Lists
Lists in HTML are used to group related items together. There are three main types
of lists in HTML: ordered lists, unordered lists, and definition lists.
htmlCopy code
<ol>
<li>First item</li>
<li>Second item</li>
<li>Third item</li>
</ol>
Example:
Example:
htmlCopy code
<dl>
<dt>HTML</dt>
<dd>HyperText Markup Language</dd>
<dt>CSS</dt>
<dd>Cascading Style Sheets</dd>
</dl>
Lists are useful for organizing and presenting information in a structured format.
They can be used for navigation menus, product listings, or any other content
where items need to be grouped together.
7. Media
HTML provides elements to embed various types of media, such as images, audio,
and video, into web pages. These elements allow you to enhance your content
with multimedia elements.
Image <img>
Example:
htmlCopy code
<img src="image.jpg" alt="Description of the image">
Audio <audio>
The <audio> element is used to embed audio content in a web page. It requires the
srcattribute, which specifies the URL of the audio file. You can also specify
additional attributes like controls to display audio controls (play, pause, volume) to
the user.
Example:
htmlCopy code
<audio controls>
<source src="audio.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
Video <video>
The <video> element is used to embed video content in a web page. It requires the
src attribute, which specifies the URL of the video file. Like the <audio> element,
you can use the controls attribute to display video controls to the user.
Example:
htmlCopy code
<video controls>
<source src="video.mp4" type="video/mp4">
Your browser does not support the video element.
Embedding Content
You can also embed content from other sources, such as YouTube videos or
Google Maps, using the <iframe> element.
Example (YouTube video):
htmlCopy code
<iframe width="560" height="315" src="https://www.youtube.co
m/embed/VIDEO_ID" frameborder="0" allowfullscreen></iframe>
HTML provides various elements for embedding different types of media, allowing
you to enhance your web pages with images, audio, video, and other multimedia
elements.
8. Metadata
Metadata in HTML provides information about the document, such as its title,
character encoding, authorship, and viewport settings. This information is not
displayed on the page but is used by browsers and search engines.
htmlCopy code
<!DOCTYPE html>
htmlCopy code
<meta charset="UTF-8">
htmlCopy code
<meta name="viewport" content="width=device-width, initial-sc
ale=1.0">
htmlCopy code
<title>My Web Page</title>
htmlCopy code
<meta name="author" content="John Doe">
<meta name="description" content="This is a description of my
web page.">
9. Accessibility
Accessibility in web development refers to designing and developing websites
that can be used by people of all abilities, including those with disabilities. HTML
provides several features that help improve the accessibility of web pages.
htmlCopy code
<img src="image.jpg" alt="Description of the image">
htmlCopy code
<label for="username">Username:</label>
<input type="text" id="username" name="username">
htmlCopy code
<nav role="navigation">
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#services">Services</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
By following these best practices for accessibility in HTML, you can ensure that
your web pages are usable and accessible to a wider range of users, including
those with disabilities.
htmlCopy code
<meta name="viewport" content="width=device-width, initial-sc
ale=1.0">
Media Queries
Media queries allow you to apply CSS styles based on the characteristics of the
device, such as screen width, height, and orientation. This allows you to create
responsive designs that adapt to different screen sizes.
Example (CSS media query):
Flexible Layouts
Using relative units like percentages or vw (viewport width) in your CSS for widths
and margins allows your layout to adapt to different screen sizes.
Example:
cssCopy code
.container {
width: 80%;
margin: 0 auto;
}
Fluid Images
Using the max-width: 100%; CSS rule for images ensures that they scale down
proportionally on smaller screens, preventing them from overflowing their
containers.
Example:
cssCopy code
img {
max-width: 100%;
height: auto;
}
Example (Flexbox):
cssCopy code
.container {
display: flex;
justify-content: space-between;
align-items: center;
}
By implementing these responsive design techniques in your HTML and CSS, you
can create web pages that provide a consistent and user-friendly experience
across a wide range of devices.
htmlCopy code
<title>My Web Page</title>
htmlCopy code
<meta name="description" content="This is a description of my
web page.">
htmlCopy code
<h1>Main Heading</h1>
<h2>Subheading</h2>
htmlCopy code
<img src="image.jpg" alt="Description of the image">
Semantic HTML
htmlCopy code
<link rel="canonical" href="https://www.example.com/page">
By incorporating these SEO best practices into your HTML, you can improve the
visibility of your website in search engine results and attract more organic traffic.
Geolocation API
The Geolocation API allows web applications to access the user's geographical
location. This can be used for location-aware services or to provide customized
content based on the user's location.
Example (getting the user's current location):
javascriptCopy code
navigator.geolocation.getCurrentPosition(function(position) {
console.log("Latitude: " + position.coords.latitude);
console.log("Longitude: " + position.coords.longitude);
javascriptCopy code
// Storing data
localStorage.setItem("username", "John");
// Retrieving data
var username = localStorage.getItem("username");
console.log("Username: " + username);
javascriptCopy code
var worker = new Worker("worker.js");
Canvas API
The Canvas API provides a way to draw graphics, animations, and other visual
elements on the web page using JavaScript. It is particularly useful for creating
games and interactive visualizations.
Example (drawing a rectangle on a canvas):
javascriptCopy code
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.fillStyle = "blue";
ctx.fillRect(10, 10, 50, 50);
WebSockets API
The WebSockets API allows for full-duplex communication between a web
browser and a server over a single, long-lived connection. This enables real-time
communication between the client and server.
Example (connecting to a WebSocket server):
javascriptCopy code
var socket = new WebSocket("ws://example.com/socket");
socket.onopen = function() {
console.log("WebSocket connection opened");
};
socket.onmessage = function(event) {
console.log("Message received: " + event.data);
};
These are just a few examples of the many APIs available in HTML5. By leveraging
these APIs, web developers can create more powerful and interactive web
applications.
Form <form>
The <form> element is used to create a form in HTML. It contains form elements
such as input fields, checkboxes, radio buttons, etc., and has attributes for
defining the form's action (where the form data is sent) and method (how the form
data is sent).
Example:
htmlCopy code
<form action="/submit-form" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required
>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
Input Validation
HTML5 introduced several new input types and attributes that help in input
validation. For example, the required attribute makes a field mandatory, and the
pattern attribute specifies a regular expression for validating the input.
htmlCopy code
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
javascriptCopy code
var emailInput = document.getElementById("email");
if (emailInput.validity.valid) {
// Input is valid
} else {
// Input is invalid, handle error
}
Custom Validation
You can also implement custom validation logic using the setCustomValidity()
method. This allows you to define custom error messages and validation rules for
javascriptCopy code
var passwordInput = document.getElementById("password");
passwordInput.addEventListener("input", function() {
if (passwordInput.value.length < 8) {
passwordInput.setCustomValidity("Password must be at
least 8 characters long");
} else {
passwordInput.setCustomValidity("");
}
});
By using these features, you can create forms that not only look good but also
provide a smooth user experience by guiding users to input data correctly.
Audio <audio>
The <audio> element allows you to embed audio content in a web page. You can
specify the audio file's source using the src attribute and provide controls for
playback using the controls attribute.
Example:
htmlCopy code
<audio controls>
<source src="audio.mp3" type="audio/mp3">
Your browser does not support the audio element.
Video <video>
The <video> element is used to embed video content in a web page. Similar to the
element, you can specify the video file's source using the
<audio> src attribute
and provide controls for playback using the controls attribute.
Example:
htmlCopy code
<video controls>
<source src="video.mp4" type="video/mp4">
Your browser does not support the video element.
</video>
Supported Formats
Different browsers support different audio and video formats. To ensure
compatibility across browsers, you can provide multiple sources using the
<source> element with different formats.
Example (providing multiple video sources):
htmlCopy code
<video controls>
<source src="video.mp4" type="video/mp4">
<source src="video.webm" type="video/webm">
Your browser does not support the video element.
</video>
htmlCopy code
<video autoplay loop>
<source src="video.mp4" type="video/mp4">
Your browser does not support the video element.
</video>
Accessibility Considerations
When using audio and video elements, it's important to provide alternative content
or captions for users who cannot access the audio or video content.
By using the <audio> and <video> elements, you can easily add audio and video
content to your web pages, enhancing the user experience with multimedia
content.
Draggable Attribute
To make an element draggable, you can use the draggable attribute. Set it to "true"
to enable dragging for an element.
Example:
htmlCopy code
<div draggable="true">Drag me!</div>
Drag Events
dragover : Fired when a dragged element is being dragged over a drop target.
htmlCopy code
<div id="dragTarget" ondragover="allowDrop(event)" ondrop="dr
op(event)">Drop here</div>
<script>
function allowDrop(event) {
event.preventDefault();
}
function drop(event) {
event.preventDefault();
var data = event.dataTransfer.getData("text");
event.target.appendChild(document.getElementById(data));
}
</script>
In this example, the allowDrop function is used to allow dropping elements onto the
target, and the drop function handles the dropping of the element by appending it
to the target.
htmlCopy code
<div id="dragSource" draggable="true" ondragstart="drag(even
t)">Drag me!</div>
<script>
function drag(event) {
event.dataTransfer.setData("text", event.target.id);
}
</script>
In this example, the drag function sets the data to be transferred as the ID of the
dragged element.
By leveraging HTML5's drag and drop functionality, you can create more
interactive and intuitive user interfaces in your web applications.