Professional Documents
Culture Documents
UNIT 2
HTML
HTML Elements
An HTML element usually consists of a start tag and end tag, with the content inserted in
between:
HTML Attributes
All HTML elements can have attributes
Attributes provide additional information about an element
Attributes are always specified in the start tag
Attributes usually come in name/value pairs like: name="value"
Basic Structure:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
All HTML documents must start with a document type declaration: <!DOCTYPE html>.
1
E- Comm Subject Unit 2
The HTML document itself begins with <html> and ends with </html>.
The visible part of the HTML document is between <body> and </body>.
HTML Headings
HTML headings are defined with the <h1> to <h6> tags.
<h1> defines the most important heading. <h6> defines the least important heading:
CODE:
<html>
<body>
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<h3>This is heading 3</h3>
<h4>This is heading 4</h4>
<h5>This is heading 5</h5>
<h6>This is heading 6</h6>
</body>
</html>
OUTPUT:
2
E- Comm Subject Unit 2
HTML Paragraphs
HTML paragraphs are defined with the <p> tag:
CODE:
<html>
<body>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
</body>
</html>
OUTPUT:
HTML Links
HTML links are defined with the <a> tag:
CODE:
<html>
<body>
<h2>HTML Links</h2>
<p>HTML links are defined with the a tag:</p>
3
E- Comm Subject Unit 2
OUTPUT:
HTML Images
HTML images are defined with the <img> tag.
The source file (src), alternative text (alt), width, and height are provided as attributes:
CODE:
<html>
<body>
<h2>HTML Images</h2>
<p>HTML images are defined with the img tag:</p>
4
E- Comm Subject Unit 2
HTML Buttons
HTML buttons are defined with the <button> tag:
CODE:
<html>
<body>
<h2>HTML Buttons</h2>
<p>HTML buttons are defined with the button tag:</p>
<button>Click me</button>
</body>
</html>
OUTPUT:
5
E- Comm Subject Unit 2
HTML Lists
HTML lists are defined with the <ul> (unordered/bullet list) or the <ol> (ordered/numbered list)
tag, followed by <li> tags (list items):
CODE:
<html>
<body>
<h2>An Unordered HTML List</h2>
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
<h2>An Ordered HTML List</h2>
<ol>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
</body>
</html>
6
E- Comm Subject Unit 2
OUTPUT:
CODE:
<html>
<body>
<p>Browsers usually insert quotation marks around the q element.</p>
<p>WWF's goal is to: <q>Build a future where people live in harmony with nature.</q></p>
</body>
</html>
OUTPUT:
WWF's goal is to:” Build a future where people live in harmony with nature”.
7
E- Comm Subject Unit 2
CODE:
<html>
<body>
<blockquote cite="http://www.worldwildlife.org/who/index.html">
</blockquote>
</body>
</html>
OUTPUT:
For 50 years, WWF has been protecting the future of nature. The world's leading conservation
organization, WWF works in 100 countries and is supported by 1.2 million members in the
United States and close to 5 million globally.
CODE:
<html>
<body>
<!-- This is a comment -->
8
E- Comm Subject Unit 2
<p>This is a paragraph.</p>
<!-- Comments are not displayed in the browser -->
</body>
</html>
OUTPUT:
This is a paragraph.
HTML Colors
HTML colors are specified using predefined color names, or RGB, HEX, HSL, RGBA, HSLA values.
Tomato
Orange
DodgerBlue
MediumSeaGreen
Gray
SlateBlue
Violet
9
E- Comm Subject Unit 2
LightGray
CODE:
<html>
<body>
<h1 style="border: 2px solid Tomato;">Hello World</h1>
<h1 style="border: 2px solid DodgerBlue;">Hello World</h1>
<h1 style="border: 2px solid Violet;">Hello World</h1>
</body>
</html>
OUTPUT:
Hello World
Hello World
Hello World
When you move the mouse over a link, the mouse arrow will turn into a little hand.
CODE:
<html>
10
E- Comm Subject Unit 2
<body>
<h2>HTML Links</h2>
<p><a href="https://www.w3schools.com/html/">Visit our HTML tutorial</a></p>
</body>
</html>
OUTPUT:
HTML Links
Visit our HTML tutorial
The link text is the visible part (Visit our HTML tutorial).
Clicking on the link text will send you to the specified address.
CODE:
<html>
<body>
<h2>Local Links</h2>
<p><a href="html_images.asp">HTML Images</a> is a link to a page on this website.</p>
OUTPUT:
Local Links
HTML Images is a link to a page on this website.
11
E- Comm Subject Unit 2
OUTPUT:
Link Colors
You can change the default colors of links
HTML Images
The <img> tag is empty, it contains attributes only, and does not have a closing tag.
The src attribute specifies the URL (web address) of the image:
12
E- Comm Subject Unit 2
CODE:
<html>
<body>
<h2>Alternative text</h2>
<p>The alt attribute should reflect the image content, so users who cannot see the image gets an
understanding of what the image contains:</p>
<img src="img_chania.jpg" alt="Flowers in Chania" width="460" height="345">
</body>
</html>
OUTPUT:
Alternative text
The alt attribute should reflect the image content, so users who cannot see the image gets an
understanding of what the image contains:
CODE:
<html>
<body>
<h2>Image Size</h2>
13
E- Comm Subject Unit 2
<p>Use the style attribute to specify the width and height of an image:</p>
<img src="img_girl.jpg" alt="Girl in a jacket" style="width:500px;height:600px;">
</body>
</html>
OUTPUT:
Image Size
Use the style attribute to specify the width and height of an image:
Image as a Link
To use an image as a link, put the <img> tag inside the <a> tag:
CODE:
<html>
<body>
<h2>Image as a Link</h2>
<p>The image is a link. You can click on it.</p>
<a href="default.asp">
<img src="smiley.gif" alt="HTML tutorial" style="width:42px;height:42px;border:0;">
</a>
<p>Add "border:0;" to prevent IE9 (and earlier) from displaying a border around the image.</p>
</body>
</html>
OUTPUT:
Image as a Link
The image is a link. You can click on it.
14
E- Comm Subject Unit 2 CHITRA NASA
Add "border:0;" to prevent IE9 (and earlier) from displaying a border around the image.
Image Floating
Use the CSS float property to let the image float to the right or to the left of a text:
CODE:
<html>
<body>
<h2>Floating Images</h2>
<p><strong>Float the image to the right:</strong></p>
<p>
<img src="smiley.gif" alt="Smiley face" style="float:right;width:42px;height:42px;">
A paragraph with a floating image. A paragraph with a floating image. A paragraph with a
floating image.
</p>
<p><strong>Float the image to the left:</strong></p>
<p>
<img src="smiley.gif" alt="Smiley face" style="float:left;width:42px;height:42px;">
A paragraph with a floating image. A paragraph with a floating image. A paragraph with a
floating image.
</p>
</body>
</html>
OUTPUT:
Floating Images
Float the image to the right:
A paragraph with a floating image. A paragraph with a floating image. A paragraph with a
floating image.
15
E- Comm Subject Unit 2 CHITRA NASA
A paragraph with a floating image. A paragraph with a floating image. A paragraph with a
floating image.
Image Maps
The <map> tag defines an image-map.
map. An image
image-map is an image with clickable areas.
In the image below, click on the computer, the phone, or the cup of coffee:
The name attribute of the <map> tag is associated with the <img>'s usemap attribute and creates a
relationship between the image and the map.
The <map> element contains a number of <area> tags, that define the clickable areas in the
image-map.
CODE:
<html>
<body>
<h2>Image Maps</h2>
<p>Click on the computer, the phone, or the cup of coffee to go to a new page and read more
about the topic:</p>
<img src="workplace.jpg" alt="Workplace" usemap="#workmap" width="400" height="379">
<map name="workmap">
<area shape="rect" coords="34,44,270,350" alt="Computer" href="computer.htm">
<area shape="rect" coords="290,172,333,250" alt="Phone" href="phone.htm">
<area shape="circle" coords="337,300,44" alt="Cup of coffee" href="coffee.htm">
</map>
</body>
</html>
OUTPUT:
Image Maps
Click on the computer, the phone, or the cup of coffee to go to a new page and read more about
the topic:
16
E- Comm Subject Unit 2 CHITRA NASA
HTML Lists
The list items will be marked with bullets (small black circles) by default:
Value Description
Disc Sets the list item marker to a bullet (default)
circle Sets the list item marker to a circle
square Sets the list item marker to a square
none The list items will not be marked
CODE:
<!DOCTYPE html>
<html>
<body>
<h2>Unordered List with Disc Bullets</h2>
<ul style="list-style-type:disc">
<li>Coffee</li>
<li>Tea</li>
17
E- Comm Subject Unit 2 CHITRA NASA
<li>Milk</li>
</ul>
</body>
</html>
OUTPUT:
CODE:
OUTPUT:
Type Description
type="1" The list items will be numbered with numbers (default)
type="A" The list items will be numbered with uppercase letters
18
E- Comm Subject Unit 2 CHITRA NASA
CODE:
<html>
<body>
<h2>Ordered List with Letters</h2>
<ol type="A">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
</body>
</html>
OUTPUT:
Each table row is defined with the <tr> tag. A table header is defined with the <th> tag. By
default, table headings are bold and centered. A table data/cell is defined with the <td> tag.
CODE:
<html>
<body>
<h2>Basic HTML Table</h2>
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
19
E- Comm Subject Unit 2 CHITRA NASA
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
</body>
</html>
OUTPUT:
CODE:
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
20
E- Comm Subject Unit 2 CHITRA NASA
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
</head>
<body>
<h2>Table Caption</h2>
<p>To add a caption to a table, use the caption tag.</p>
<table style="width:100%">
<caption>Monthly savings</caption>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$50</td>
</tr>
</table>
</body>
</html>
OUTPUT:
Table Caption
To add a caption to a table, use the caption tag.
Monthly savings
Month Savings
January $100
February $50
NEW CODE:
<table border="1">
<tr>
<th>Month</th>
21
E- Comm Subject Unit 2 CHITRA NASA
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</table>
OUTPUT:
Month Savings
January $100
February $80
Note: The border attribute is not supported in HTML5. Use CSS instead.
IMPORTANT CONCEPT:
CELLPADDING sets the amount of space between the contents of the cell and
the cell wall. The default is 1. CELLPADDING is usually more effective than
CELLSPACING for spreading out the contents of the table.
22
E- Comm Subject Unit 2 CHITRA NASA
CODE:
<html>
<head>
<title>HTML Table Cellpadding</title>
</head>
<body>
<table border = "1" cellpadding = "5" cellspacing = "5">
<tr>
<th>Name</th>
<th>Salary</th>
</tr>
<tr>
<td>Ramesh Raman</td>
<td>5000</td>
</tr>
<tr>
<td>Shabbir Hussein</td>
<td>7000</td>
</tr>
</table>
</body>
</html>
Name Salary
23
E- Comm Subject Unit 2 CHITRA NASA
<html>
<head>
<title>HTML Table Colspan/Rowspan</title>
</head>
<body>
<table border = "1">
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
<tr>
<td rowspan = "2">Row 1 Cell 1</td>
<td>Row 1 Cell 2</td>
<td>Row 1 Cell 3</td>
</tr>
<tr>
<td>Row 2 Cell 2</td>
<td>Row 2 Cell 3</td>
</tr>
<tr>
<td colspan = "3">Row 3 Cell 1</td>
</tr>
</table>
</body>
</html>
Column 1 Column 2 Column 3
Row 1 Cell 2 Row 1 Cell 3
Row 1 Cell 1
Row 2 Cell 2 Row 2 Cell 3
Row 3 Cell 1
24
E- Comm Subject Unit 2 CHITRA NASA
<form>
.
form elements
.
</form>
An HTML form contains form elements.
The <input> element can be displayed in several ways, depending on the type attribute.
Type Description
<input type="text"> Defines a one-line text input field
<input type="radio"> Defines a radio button (for selecting one of many choices)
<input type="submit"> Defines a submit button (for submitting the form)
Text Input
<input type="text"> defines a one-line input field for text input:
Source Code:
<html>
<body>
<h2>Text Input</h2>
<form>
25
E- Comm Subject Unit 2 CHITRA NASA
First name:<br>
<input type="text" name="firstname">
<br>
Last name:<br>
<input type="text" name="lastname">
</form>
<p>Note that the form itself is not visible.</p>
<p>Also note that the default width of a text input field is 20 characters.</p>
</body>
</html>
Output:
First name:
Last name:
Source Code:
<html>
<body>
<h2>Radio Buttons</h2>
<form>
<input type="radio" name="gender" value="male" checked> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other
</form>
</body>
</html>
Output:
26
E- Comm Subject Unit 2 CHITRA NASA
Radio Buttons
Male
Female
Other
The form-handler is typically a server page with a script for processing input data.
Source Code:
<html>
<body>
<h2>HTML Forms</h2>
<form action="/action_page.php">
/action_page.php">
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit">
</form>
<p>If you click the "Submit" button, the form-data will be sent to a page called
"/action_page.php".</p>
</body>
</html>
Output:
HTML Forms
First name:
Mickey
27
E- Comm Subject Unit 2 CHITRA NASA
Last name:
Mouse
If you click the "Submit" button, the form-data will be sent to a page called "/action_page.php".
Normally, the form data is sent to a web page on the server when the user clicks on the submit
button.
In the example above, the form data is sent to a page on the server called "/action_page.php".
This page contains a server-side script that handles the form data:
<form action="/action_page.php">
If the action attribute is omitted, the action is set to the current page.
The default value is "_self" which means the form will be submitted in the current window.
To make the form result open in a new browser tab, use the value " _blank":
Example
<form action="/action_page.php" target="_blank">
Source Code:
<html>
<body>
<h2>The target Attribute</h2>
<p>When submitting this form, the result will be opened in a new browser tab:</p>
<form action="/action_page.php" target="_blank">
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
28
E- Comm Subject Unit 2 CHITRA NASA
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
1. Get Method
<html>
<body>
<h2>The method Attribute</h2>
<p>This form will be submitted using the GET method:</p>
<form action="/action_page.php" target="_blank" method="GET">
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit">
</form>
<p>After you submit, notice that the form values is visible in the address bar of the new browser
tab.</p>
</body>
</html>
However, when GET is used, the submitted form data will be visible in the page
address field:
2. Post Method
<html>
<body>
<h2>The method Attribute</h2>
<p>This form will be submitted using the POST method:</p>
<form action="/action_page.php" target="_blank" method="POST">
First name:<br>
29
E- Comm Subject Unit 2 CHITRA NASA
Always use POST if the form data contains sensitive or personal information. The
POST method does not display the submitted form data in the page address field.
If the name attribute is omitted, the data of that input field will not be sent at all.
This example will only submit the "Last name" input field:
<html>
<body>
<h2>The name Attribute</h2>
<form action="/action_page.php">
First name:<br>
<input type="text" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit">
</form>
<p>If you click the "Submit" button, the form-data will be sent to a page called
"/action_page.php".</p>
30
E- Comm Subject Unit 2 CHITRA NASA
<p>Notice that the value of the "First name" field will not be submitted, because
the input element does not have a name attribute.</p>
</body>
</html>
<html>
<body>
31
E- Comm Subject Unit 2 CHITRA NASA
Last name:
Mouse
<html>
<body>
<h2>The value Attribute</h2>
<p>The value attribute specifies the initial value for an input field:</p>
<form action="">
First name:<br>
<input type="text" name="firstname" value="John">
<br>
Last name:<br>
<input type="text" name="lastname">
</form>
</body>
</html>
<html>
<body>
32
E- Comm Subject Unit 2 CHITRA NASA
<p>The readonly attribute specifies that the input field is read only (cannot be
changed):</p>
<form action="">
First name:<br>
<br>
Last name:<br>
</form>
</body>
</html>
A disabled input field is unusable and un-clickable, and its value will not be sent when
submitting the form:
<html>
<body>
33
E- Comm Subject Unit 2 CHITRA NASA
Last name:<br>
<input type="text" name="lastname">
</form>
</body>
</html>
<form action="">
First name:<br>
<input type="text" name="firstname" value="John" size="40">
</form>
<form action="">
First name:<br>
<input type="text" name="firstname" maxlength="10">
</form>
The autocomplete attribute specifies whether a form or input field should have
autocomplete on or off.
When autocomplete is on, the browser automatically completes the input values
based on values that the user has entered before.
Tip: It is possible to have autocomplete "on" for the form, and "off" for specific
input fields, or vice versa.
34
E- Comm Subject Unit 2 CHITRA NASA
The autocomplete attribute works with <form> and the following <input> types:
text, search, url, tel, email, password, datepickers, range, and color.
For eg:
<form action="/action_page.php" autocomplete="on">
First name:<input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
E-mail: <input type="email" name="email" autocomplete="off"><br>
<input type="submit">
</form>
Source Code:
<html>
<body>
<h2>The autocomplete Attribute</h2>
<form action="/action_page.php" autocomplete="on">
First name:<input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
E-mail: <input type="email" name="email" autocomplete="off"><br>
<input type="submit">
</form>
<p>Fill in and submit the form, then reload the page to see how autocomplete
works.</p>
<p>Notice that autocomplete is "on" for the form, but "off" for the e-mail
field.</p>
</body>
</html>
<html>
<body>
<h2>The autofocus Attribute</h2>
35
E- Comm Subject Unit 2 CHITRA NASA
<p>The autofocus attribute specifies that the input field should automatically get
focus when the page loads.</p>
<form action="/action_page.php">
First name:<input type="text" name="fname" autofocus><br>
Last name: <input type="text" name="lname"><br>
<input type="submit">
</form>
<p><strong>Note:</strong> The autofocus attribute of the input tag is not
supported in Internet Explorer 9 and earlier versions.</p>
</body>
</html>
36