Notes - HTML
Notes - HTML
Web development is the process of building and maintaining websites and web
applications.
HTML is like frame of the house, providing the door, windows, rooms etc.
CSS is like the interior & exterior design to make house visually appealing.
Few terms:
Responsive design: Responsive web design is about creating web pages that
looks good on all devices. A responsive design will automatically adjust
their layout and content to the screen size and resolution of the device they
are being viewed on. This ensures a good user experience regardless of whether
someone is viewing the site on a desktop computer, a tablet, or a smartphone.
gs/html/pg-1
HTML - The Structure
Hypertext: Hypertext means, text, images, etc. with a link embedded in it.
If you click on that link, it will open a new webpage.
HTML Elements
An HTML element is defined by a start tag, some content, and an end tag. That
means, the HTML element is everything from the start tag to the end tag.
Tags
Tags are used to structure the content on web pages. Tags are pre-defined
elements in HTML, enclosed within angle < > brackets. All HTML tags have a
particular function associated with them.
Paired Tags Paired tags have a separate opening and a separate closing
tag. The closing tag has a forward slash (/) before the tag name.
Unpaired Tags Unpaired tags are single tags with no closing tag. These
tags are also called singular tags or empty tags.
Example: <br>
gs/html/pg-2
Attributes
Here, <img> is the tag and 'src' is the attribute name. The 'src' attribute
specifies the path of the image to be displayed.
Comments in HTML
Any text written inside <!-- and --> is considered as a comment and it does not
display on the webpage.
<!DOCTYPE html>
<html>
<head>
<title> ------ </title>
</head>
<body>
----------Any tags can be given
</body>
</html>
<html lang="en">: It is the parent tag for all HTML elements. Everything
related to an HTML document must be written inside the <html> tag. lang="en"
specifies the language as English.
<head>: This tag contains meta-information about the HTML document, such as
the title, character set, and links to external resources.
NOTE: It is a container tag for all those elements that are not directly
displayed on the webpage but required for the page functionalities.
gs/html/pg-3
<title>: It specifies the title of the webpage, which is displayed in the
browser's title bar or tab.
<body>: Encloses all the content that will be displayed on the webpage.
NOTE: All above tags are paired tags. Only <!DOCTYPE html> is a singular tag.
Paired Tags
<h1> to <h6>: These are heading tags. They represent different levels of
headings, with <h1> being the largest and <h6> being the smallest.
Example: <h1> Hello! </h1>
<p>: It defines a paragraph, anything written inside <p> and </p> displays
as a paragraph on the webpage.
Example: <p>Hello! Hello! Hello! Hello! Hello!
Hello! Hello! Hello! Hello! Hello! Hello! Hello! </p>
<strong>: It is used to define text with strong importance. The content inside
is displayed in bold.
Example: <strong> Hello! </strong>
<b>: It is used to make the text bold, without any extra importance.
Example: <b> Hello! </b>
<a>: <a> tag, known as Anchor tag creates a hyperlink, which is used to link
from one web page to another.
You can create hyperlinks using text, images, etc. available on a webpage.
Anything (text, image, etc.) between opening <a> and closing </a> tags become
clickable and when someone clicks on it, the linked page will be opened.
gs/html/pg-4
Syntax: <a href="URL" target="_target_type"> Text to be linked </a>
Explanation:
href: It specifies the URL of the page that the link points to.
target: It specifies where to open the linked document. Few values are:
Text to be linked is the part that will be visible to the reader. Clicking
on the text, will send the reader to the specified URL address.
gs/html/pg-6
<main>: It defines the main content of an HTML document, which should be
unique. Only one <main> element should exist per page.
It should not contain any content that is repeated across documents such
navigation links, copyright information, etc.
Example: <body>
<main>
<h1>Programming Languages</h1>
<p>C Programming, C++ Programming</p>
<article>
<h1>C Programming</h1>
<p>C is a Procedural language</p>
</article>
<article>
<h1>C++ Programming</h1>
<p>C++ programming is an Object oriented
programming.</p>
</article>
</main>
</body>
Example:
<body>
<ul>
<li>Mouse</li>
<li>Keyboard</li>
</ul>
<ul>
<li>Mouse</li>
<li>Keyboard</li>
</ul>
</body>
gs/html/pg-7
<ol>: It defines an ordered list. An ordered list can be numerical or
alphabetical. It provides sequential organization of items.
Example: <body>
<ol>
<li>Mouse</li>
<li>Keyboard</li>
<li>Speaker</li>
</ol>
<ol type='A' start='3'>
<li>Mouse</li>
<li>Keyboard</li>
<li>Speaker</li>
</ol>
</body>
Example: <head>
<style>
#p {
color:red
</style>
</head>
<body>
<h1 id="p">HELLO !</h1>
</body>
The <audio> tag can contain one or more <source> tags with different audio
sources. The browser will choose the first source it supports.
The text between the <audio> and </audio> tags will only be displayed in
browsers that do not support the <audio> element.
gs/html/pg-8
Few Attributes of <audio> tag:
<audio controls>
<source src="Sare%20Jahan%20Se%20Acha.mp3" type="audio/mpeg">
<source src="Vande%20Mataram.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
Example:
<video src="TomJerry.mp4" width="350" height="250" controls>
Video tag not supported by your browser
</video>
The HTML <iframe> tag specifies an inline frame. An inline frame is used to
embed another document within the current HTML document.
The embedded document can be a webpage, video, audio, google map, etc.
The content inside the iframe remains independent of the parent document,
meaning styles and scripts of the parent document do not interfere.
Attributes of <iframe>:
Example:
<body>
<iframe
src="[Link]
width="600px" height="450px" allowfullscreen>
</iframe>
</body>
gs/html/pg-10
<link>: It defines the relationship between the current document and an
external resource. It is an empty element, it contains attributes only.
The <link> tag is most often used to link external style sheets or to add a
favicon to your website.
rel: Specifies the relationship between the current and the linked
document.
Example: <head>
<link rel="stylesheet" type="text/css" href="[Link]">
</head>
Adding Favicon:
A favicon is a small image displayed next to the page title in the browser
tab.
Favicons are added to HTML documents using the <link> tag within the <head>
section. The rel attribute of the <link> tag is set to icon to specify
that the linked resource is a favicon. The href attribute is used to
provide the path to the favicon image file.
You can use any image you like as your favicon. You can also create your
own favicon on sites like [Link]
Example:
<head>
<title>My Page</title>
<link rel="icon" href="[Link]" />
</head>
<body>
<h1> You can download favicon from [Link] </h1>
<p></p>
</body>
gs/html/pg-11
<img>: This tag is used to embed an image in an HTML page.
Example:
<html>
<body>
<img src="[Link]" alt="Flower image" width="300px height="300px">
</body>
</html>
<meta>: Meta tags are used to provide metadata about an HTML document, such
as character set, page description, viewport settings, etc.
Metadata is data about data. It means information about the webpage content.
initial-scale=1.0: Sets the initial zoom level when the page is first
loaded. 1.0 means the page will be displayed at 100% zoom, without any
initial scaling.
gs/html/pg-12
Block and Inline Elements
Block-level Elements
Examples: <div>, <p>, <h1> to <h6>, <ul>, <ol>, <li>, <form>, <header>, <footer>,
<section>, <nav>.
Inline Elements
They flow with the text and are part of the line.
Examples: <span>, <a>, <img>, <strong>, <em>, <br>, <label>, <input>, <select>.
In HTML, id and class are attributes used to identify and style elements.
id Attribute
No two elements on the same page should have the same id.
It is used by JavaScript to access & manipulate the element with specific id.
class Attribute
The HTML class attribute is used to specify a class for an HTML element.
In CSS, a class is selected using the . symbol followed by the class value
(e.g., .myClass).
JavaScript can access elements with a specific class name with the
getElementsByClassName() method.
Tables
In HTML, Tables are used to organize and display data in a structured format of
rows and columns.
A table cell can contain text, images, lists, links, other tables, etc.
<table>: It creates the table. It acts as a container for all table elements.
<tr>: Stands for table row and creates a row in the table.
gs/html/pg-14
<td>: Defines a data cell within a row, containing the actual table data.
<th>: Defines a header cell within a row, typically used for column labels.
It is often bold and centred.
NOTE: An HTML table may also include <colgroup>, <thead>, <tfoot> and <tbody>
rowspan: Specifies the number of rows a cell should span vertically. Used to
merge cells vertically, making a single cell occupy the space of multiple
rows.
cellpadding: Sets the space between the cell content and its border. (CSS
property is CSS padding).
Example 1:
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
text-align: center;
padding: 2px;
border-spacing: 5px;
border-collapse: collapse;
}
caption {
color: red;
font-weight: 600;
}
</style>
</head>
gs/html/pg-15
<body>
<table style="width: 400px">
<caption>
STUDENT MARKS
</caption>
<tr>
<th>ROLL</th>
<th>NAME</th>
<th>TOTAL MARKS</th>
</tr>
<tr>
<td>1</td>
<td>Rahul</td>
<td>485</td>
</tr>
<tr>
<td>2</td>
<td>Soham</td>
<td>445</td>
</tr>
</table>
</body>
</html>
<body>
<table border="1" bgcolor="lightyellow" width="400px" cellspacing="3"
cellpadding="5">
<tr>
<th rowspan="6">Months</th>
<th clospan="2">28 days</th>
</tr>
<tr><td>February</td></tr>
gs/html/pg-16
<tr><th>30 days</th></tr>
Forms
An HTML form is used to collect user input. The user input is most often sent
to a server for processing.
The <form> element is a container for different types of input elements, such
as: text fields, checkboxes, radio buttons, submit buttons, etc.
Value: URL
NOTE: If action attribute is omitted, the action is set to the current page.
method: It specifies the HTTP method that will be used to send data to the
server when the form is submitted.
GET: Appends the form data to the URL, in name/value pairs. It is not used
to send sensitive data as the submitted form data is visible in the URL.
The length of a URL is limited (2048 characters). It is useful for form
submissions where a user wants to bookmark the result.
gs/html/pg-17
POST: Appends the form data inside the body of the HTTP request (the
submitted form data is not shown in the URL). Always use POST to send
sensitive or personal information. It has no size limitations. Form
submissions with POST cannot be bookmarked.
Below are few elements. There are elements like <legend>, <output>, etc.
Example: <form>
<label for="username">Username:</label>
<input type="text" id="username" name="username">
</form>
NOTE: The for attribute of the <label> tag should be equal to id attribute
of the <input> element to bind them together.
Example: <form>
<label for="password">Password:</label>
<input type="text" id="password" name="username">
</form>
gs/html/pg-18
<select>: It is used to create a drop-down list for user input, containing
<option> tags to display the available choices. It is a paired element.
Its multiple attribute allows selecting one or multiple options from a list.
By default, the first item in the drop-down list is selected. You can
preselect an option with the selected attribute.
Example 1: <form>
<label for="colors">Choose a color:</label>
<select id="colors" name="colors">
<option value="red"> Red </option>
<option value="blue"> Blue </option>
<option value="yellow"> Yellow </option>
</select>
</form>
Example:
<form action="/action_page.php">
<textarea name="message" rows="10" cols="30"> Comment area </textarea>
</form>
Example:
<button type="button" onclick="alert('Hello World!')"> Click Me! </button>
gs/html/pg-19
Few Input Types in HTML Forms:
Below are some input types. There are more input types like "url", "range", etc.
NOTE: The radio group must share the same name (the value of the name
attribute) to be treated as a group. You can have as many radio groups on a
page as you want, but each group must share a common name attribute.
NOTE: The value attribute defines the unique value associated with each radio
button. The value is not shown to the user, but is the value that is sent to
the server on "submit" to identify which radio button that was selected.
Example: <form>
<p>Gender :</p>
<input type="radio" id="male" name="gender" value="Male">
<label for="html"> Male </label>
checked attribute makes the checkbox checked by default when the page loads.
Example: <body>
<h3>Favourite Color:</h3>
<form action="/example_page.php">
<input type="checkbox" id="red" name="color" value="Red">
<label for="red"> RED </label><br>
Example:<body>
<form>
<input type="button" value="Click me" onclick="msg()">
</form>
<script>
function msg() {
alert("Hello world!");
}
</script>
</body>
<input type="reset">: It creates a reset button that will reset all the form
values to their default values.
Example:
<form>
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname" value="Rahul"><br><br>
<input type="reset" value="Reset" />
</form>
gs/html/pg-21
<input type="submit">: It creates a button for submitting form data to a
server page.
Example:
<form action="/example_page.php">
<label for="fname">First name: </label>
<input type="text" id="fname" name="fname"><br><br>
<input type="submit" value="Submit">
</form>
Here, if you click the "Submit" button, the form-data will be sent to a page
called "/example_page.php".
NOTE: The date format for the <input type="date"> element is standardized as
YYYY-MM-DD. Internally, the input value is always in this format, but the
visual representation of the date picker can vary depending on the browser
and operating system's local settings. For example, a user in the United
States might see a date picker displaying MM/DD/YYYY, even though the
underlying value is still stored as YYYY-MM-DD.
It is important to note that HTML does not provide a direct way to change
the display format of the date picker. The browser determines how the date
is presented to the user.
Example:
<form>
<label for="start-time">Select a time:</label>
<input type="time" id="start-time" name="start-time">
</form>
NOTE: The format for this input is always a 24-hour format, represented as
HH:mm or HH:mm:ss. While the input may display in 12-hour format, depending
on the browser and operating system settings, but internally the value will
always be submitted in 24-hour format.
gs/html/pg-22
<input type="tel">: It creates a field for entering phone number.
Example: <form>
<label for="phone">Enter your phone number:</label>
<input type="tel" id="phone" name="phone"
pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}">
</form>
Example:<form>
<label for="email">Enter your email:</label>
<input type="email" id="email" name="email" multiple>
</form>
You can also use the "multiple" attribute within the <input type="file"> tag
to allow users to select multiple files at once.
Example: <form>
<label for="myfile">Select a file:</label>
<input type="file" id="myfile" name="myfile" multiple>
</form>
There are more attributes of <input> element. Below are only few.
id: It serves as a unique identifier for that specific element within the
HTML document. No two elements should have the same id.
gs/html/pg-23
NOTE: id must be unique within the HTML document, whereas name can be non-
unique, and is sometimes re-used within a form (like, same name should be
used for all options of radio input type, to ensure that only one radio button
is selected from a group).
Example: <form>
<p>Please select your age:</p>
<input type="radio" id="age1" name="age" value="30">
<label for="age1">0 - 30</label><br>
For button, reset, and submit - it defines the text on the button.
For text, password, and hidden - it defines the initial (default) value
of the input field. If omitted, the field is empty.
For checkbox, radio, image - It defines the value associated with the
input, which is sent when the form is submitted.
required: It specifies that the input field must be filled out before
submitting the form.
Example:
<label for="phone">Phone Number:</label>
<input type="tel" id="phone" name="phone" placeholder="000-000- 0000">
gs/html/pg-24
pattern: It specifies a regular expression that the input field's value is
checked against, when the form is submitted. It is used with input types -
text, date, search, url, tel, email, and password.
Here, a pattern is defined to enter phone number. To add a hyphen after first
3 digits, then again a hyphen after next 3 digits.
min and max: The min and max attributes specify the minimum and maximum values
for an input field. It is used with input types - number, range, date,
datetime-local, month, time and week.
-----------------------------------x-----------------------------------------
gs/html/pg-25