HTML Notes
HTML Notes
HTML stands for Hyper Text Markup Language, which is the most widely used language on Web
to develop web pages. HTML was created by Berners-Lee in late 1991 but "HTML 2.0" was the first
standard HTML specification which was published in 1995
It is used to design web pages using the markup language. HTML is the combination
of Hypertext and Markup language. Hypertext defines the link between the web pages and markup
language defines the text document within t
he tag that define the structure of web pages.
HTML Tags: Tags are the starting and ending parts of an HTML element. They
begin with < symbol and end with > symbol. Whatever written inside < and > are
called tags. Example : <b> </b>
1. Document structure
2. Block
3. inline
The basic structure of an HTML page is laid out below. It contains the essential building-block
elements (i.e. doctype declaration, HTML, head, title, and body elements) upon which all web
pages are created.
<DOCTYPE! html> – A doctype or document type declaration is an instruction
that tells the web browser about the markup language in which the current page is
written. It is not an element or tag. The doctype declaration is not case-sensitive.
<html> – This tag is used to define the root element of HTML document. This tag
tells the browser that it is an HTML document. It is the second outer container
element that contains all other elements within it.
<head> – This tag is used to define the head portion of the HTML document that
contains information related to the document. Elements within the head tag are not
visible on the front-end of a webpage.
<body> – The body tag is used to enclose all the visible content of a webpage. In
other words, the body content is what the browser will show on the front end.
Example 1: This is the basic example of HTML that display the heading and paragraph
content.
HTML
<!DOCTYPE html>
<html>
</html>
Characteristics of HTML:
Easy to understand: It is the most straightforward language you can say,
very easy to grasp this language and easy to develop.
Flexibility: This language is so much flexible that you can create
whatever you want, a flexible way to design web pages along with the text.
Linkable: You can make linkable text like users can connect from one
page to another page or website through these characteristics.
Limitless features: You can add videos, GIFs, pictures, or sound
anything you want that will make the website more attractive and
understandable.
Support: You can use this language to display the documents on any
platform like Windows, Linux, or Mac.
Not a Programming Language: HTML is not a programming language
as it is only concerned with presenting the information on the web. It is not
used to program any logic but to give structure and semantically meaning
to our website. Though we can link JavaScript code to it which is a
programming language.
Language Support: HTML can support various other languages
like JavaScript, Ruby, PHP, Perl, and many more. You can also able to
run embed python during the runtime.
Advantages of HTML:
HTML is easy to learn, easy to apply and it’s totally free you will just need
a text editor and a browser.
HTML is supported by all the browsers and it is the most friendly search
engine.
HTML can easily integrate with other languages and is easy to develop.
It is the basic of all programming languages and the lightest language ever.
In HTML, the display changes frequently depending on the window size or
the device size making it comfortable to read by the user.
Disadvantages of HTML:
HTML can be used to create only static Web-page, it can not create
dynamic web-page.
There is a lack of security in HTML.
Creating a simple Web-page required so many tags.
HTML language is not centralised i.e. all the web pages that are connected,
you have to design them separately else need to use CSS.
HTML becomes complex when you try to create a huge website.
HTML Editors
HTML text editors are used to create and modify web pages. HTML codes can be
written in any text editors including the notepad. One just needs to write HTML in
any text editor and save the file with an extension “.html” or “.htm”. Some of the
popular HTML text editors are given below:
Notepad
Notepad++
Sublime Text 3
Atom
Notepad
Notepad is a simple text editor. It is an inbuilt desktop application available in
Windows OS.
Brackets
Brackets is an open-source software primarily used for Web development. It
provides live HTML, CSS, JavaScript editing functionality.
Sublime Text 3
Sublime is a cross platform code editor tool. It supports all markup languages.
Atom
Atom is an open source code editor tool for MAC, Linux and Windows.
Steps to write HTML code in Editor:
1. Open any of the text editors of your choice. Here we are using
the notepad text editor.
2. Create new file: File->New File or Ctrl+N.
4. Save the file with a suitable name of your choice and .html extension.
5. Open the saved HTML file in your favourite browser (double click on the
file, or right-click – and choose “Open with”).
Id
Title
Class
Style
The Id Attribute
The id attribute of an HTML tag can be used to uniquely identify any element within an HTML
page. There are two primary reasons that you might want to use an id attribute on an element −
If an element carries an id attribute as a unique identifier, it is possible to identify
just that element and its content.
If you have two elements of the same name within a Web page (or style sheet),
you can use the id attribute to distinguish between elements that have the same
name.
We will discuss style sheet in separate tutorial. For now, let's use the id attribute to distinguish
between two paragraph elements as shown below.
Example
<p id = "html">This para explains what is HTML</p>
<p id = "css">This para explains what is Cascading Style Sheet</p>
The title Attribute
The title attribute gives a suggested title for the element. They syntax for the title attribute is
similar as explained for id attribute −
The behavior of this attribute will depend upon the element that carries it, although it is often
displayed as a tooltip when cursor comes over the element or while the element is loading.
Example
<!DOCTYPE html>
<html>
<head>
<title>The title Attribute Example</title>
</head>
<body>
<h3 title = "Hello HTML!">Titled Heading Tag Example</h3>
</body>
</html>
This will produce the following result −
Now try to bring your cursor over "Titled Heading Tag Example" and you will see that whatever
title you used in your code is coming out as a tooltip of the cursor.
The class Attribute
The class attribute is used to associate an element with a style sheet, and specifies the class of
element. You will learn more about the use of the class attribute when you will learn Cascading
Style Sheet (CSS). So for now you can avoid it.
The value of the attribute may also be a space-separated list of class names. For example −
class = "className1 className2 className3"
The style Attribute
The style attribute allows you to specify Cascading Style Sheet (CSS) rules within the element.
<!DOCTYPE html>
<html>
<head>
<title>The style Attribute</title>
</head>
<body>
<p style = "font-family:arial; color:#FF0000;">Some text...</p>
</body>
</html>
This will produce the following result −
At this point of time, we are not learning CSS, so just let's proceed without bothering much
about CSS. Here, you need to understand what are HTML attributes and how they can be used
while formatting content.
Internationalization Attributes
There are three internationalization attributes, which are available for most (although not all)
XHTML elements.
dir
lang
xml:lang
The dir Attribute
The dir attribute allows you to indicate to the browser about the direction in which the text
should flow. The dir attribute can take one of two values, as you can see in the table that follows
−
Value Meaning
rtl Right to left (for languages such as Hebrew or Arabic that are read
right to left)
Example
<!DOCTYPE html>
<html dir = "rtl">
<head>
<title>Display Directions</title>
</head>
<body>
This is how IE 5 renders right-to-left directed text.
</body>
</html>
This will produce the following result −
When dir attribute is used within the <html> tag, it determines how text will be presented
within the entire document. When used within another tag, it controls the text's direction for
just the content of that tag.
The lang Attribute
The lang attribute allows you to indicate the main language used in a document, but this
attribute was kept in HTML only for backwards compatibility with earlier versions of HTML.
This attribute has been replaced by the xml:lang attribute in new XHTML documents.
The values of the lang attribute are ISO-639 standard two-character language codes.
Check HTML Language Codes: ISO 639 for a complete list of language codes.
Example
<!DOCTYPE html>
<html lang = "en">
<head>
<title>English Language Page</title>
</head>
<body>
This page is using English Language
</body>
</html>
This will produce the following result −
We will see related examples as we will proceed to study other HTML tags. For a complete list
of HTML Tags and related attributes please check reference to HTML Tags List.
HTML - Header
We have learnt that a typical HTML document will have following structure −
<head>
Document header related tags
</head>
<body>
Document body related tags
</body>
</html>
This chapter will give a little more detail about header part which is represented by HTML
<head> tag. The <head> tag is a container of various important tags like <title>, <meta>,
<link>, <base>, <style>, <script>, and <noscript> tags.
<!DOCTYPE html>
<html>
<head>
<title>HTML Title Tag Example</title>
</head>
<body>
<p>Hello, World!</p>
</body>
</html>
This will produce the following result −
<!DOCTYPE html>
<html>
<head>
<title>HTML Meta Tag Example</title>
<!-- Tag to tell robots not to index the content of a page -->
<meta name = "robots" content = "noindex, nofollow">
</head>
<body>
<p>Hello, World!</p>
</body>
</html>
This will produce the following result −
<!DOCTYPE html>
<html>
<head>
<title>HTML Base Tag Example</title>
<base href = "https://www.tutorialspoint.com/" />
</head>
<body>
<img src = "/images/logo.png" alt = "Logo Image"/>
<a href = "/html/index.htm" title = "HTML Tutorial"/>HTML Tutorial</a>
</body>
</html>
This will produce the following result −
But if you change base URL to something else, for example, if base URL is
http://www.tutorialspoint.com/home then image and other given links will become like
http://www.tutorialspoint.com/home/images/logo.png and
http://www.tutorialspoint.com/html/index.htm
<!DOCTYPE html>
<html>
<head>
<title>HTML link Tag Example</title>
<base href = "https://www.tutorialspoint.com/" />
<link rel = "stylesheet" type = "text/css" href = "/css/style.css">
</head>
<body>
<p>Hello, World!</p>
</body>
</html>
This will produce the following result −
<!DOCTYPE html>
<html>
<head>
<title>HTML style Tag Example</title> #00aa77
<base href = "https://www.tutorialspoint.com/" />
<body>
<p class = "myclass">Hello, World!</p>
</body>
</html>
This will produce the following result −
Note − To learn about how Cascading Style Sheet works, kindly check a separate tutorial
available at css
<!DOCTYPE html>
<html>
<head>
<title>HTML script Tag Example</title>
<base href = "http://www.tutorialspoint.com/" />
<body>
<input type = "button" onclick = "Hello();" name = "ok" value = "OK" />
</body>
</html>
This will produce the following result, where you can try to click on the given button –
OK
HTML - Formatting
If you use a word processor, you must be familiar with the ability to make text bold, italicized,
or underlined; these are just three of the ten options available to indicate how text can appear
in HTML and XHTML.
Bold Text
Anything that appears within <b>...</b> element, is displayed in bold as shown below −
Example
<!DOCTYPE html>
<html>
<head>
<title>Bold Text Example</title>
</head>
<body>
<p>The following word uses a <b>bold</b> typeface.</p>
</body>
</html>
This will produce the following result −
Italic Text
Anything that appears within <i>...</i> element is displayed in italicized as shown below −
Example
<!DOCTYPE html>
<html>
<head>
<title>Italic Text Example</title>
</head>
<body>
<p>The following word uses an <i>italicized</i> typeface.</p>
</body>
</html>
This will produce the following result −
Underlined Text
Anything that appears within <u>...</u> element, is displayed with underline as shown below
−
Example
<!DOCTYPE html>
<html>
<head>
<title>Underlined Text Example</title>
</head>
<body>
<p>The following word uses an <u>underlined</u> typeface.</p>
</body>
</html>
This will produce the following result −
Strike Text
Anything that appears within <strike>...</strike> element is displayed with strikethrough,
which is a thin line through the text as shown below −
Example
<!DOCTYPE html>
<html>
<head>
<title>Strike Text Example</title>
</head>
<body>
<p>The following word uses a <strike>strikethrough</strike> typeface.</p>
</body>
</html>
This will produce the following result −
Monospaced Font
The content of a <tt>...</tt> element is written in monospaced font. Most of the fonts are
known as variable-width fonts because different letters are of different widths (for example, the
letter 'm' is wider than the letter 'i'). In a monospaced font, however, each letter has the same
width.
Example
<!DOCTYPE html>
<html>
<head>
<title>Monospaced Font Example</title>
</head>
<body>
<p>The following word uses a <tt>monospaced</tt> typeface.</p>
</body>
</html>
This will produce the following result −
Superscript Text
The content of a <sup>...</sup> element is written in superscript; the font size used is the same
size as the characters surrounding it but is displayed half a character's height above the other
characters.
Example
<!DOCTYPE html>
<html>
<head>
<title>Superscript Text Example</title>
</head>
<body>
<p>The following word uses a <sup>superscript</sup> typeface.</p>
</body>
</html>
This will produce the following result −
Subscript Text
The content of a <sub>...</sub> element is written in subscript; the font size used is the same
as the characters surrounding it, but is displayed half a character's height beneath the other
characters.
Example
<!DOCTYPE html>
<html>
<head>
<title>Subscript Text Example</title>
</head>
<body>
<p>The following word uses a <sub>subscript</sub> typeface.</p>
</body>
</html>
This will produce the following result −
Inserted Text
Anything that appears within <ins>...</ins> element is displayed as inserted text.
Example
<!DOCTYPE html>
<html>
<head>
<title>Inserted Text Example</title>
</head>
<body>
<p>I want to drink <del>cola</del> <ins>wine</ins></p>
</body>
</html>
This will produce the following result −
Deleted Text
Anything that appears within <del>...</del> element, is displayed as deleted text.
Example
<!DOCTYPE html>
<html>
<head>
<title>Deleted Text Example</title>
</head>
<body>
<p>I want to drink <del>cola</del> <ins>wine</ins></p>
</body>
</html>
This will produce the following result −
Larger Text
The content of the <big>...</big> element is displayed one font size larger than the rest of the
text surrounding it as shown below −
Example
<!DOCTYPE html>
<html>
<head>
<title>Larger Text Example</title>
</head>
<body>
<p>The following word uses a <big>big</big> typeface.</p>
</body>
</html>
This will produce the following result −
Smaller Text
The content of the <small>...</small> element is displayed one font size smaller than the rest
of the text surrounding it as shown below −
Example
<!DOCTYPE html>
<html>
<head>
<title>Smaller Text Example</title>
</head>
<body>
<p>The following word uses a <small>small</small> typeface.</p>
</body>
</html>
This will produce the following result −
Grouping Content
The <div> and <span> elements allow you to group together several elements to create
sections or subsections of a page.
For example, you might want to put all of the footnotes on a page within a <div> element to
indicate that all of the elements within that <div> element relate to the footnotes. You might
then attach a style to this <div> element so that they appear using a special set of style rules.
Example
<!DOCTYPE html>
<html>
<head>
<title>Div Tag Example</title>
</head>
<body>
<div id = "menu" align = "middle" >
<a href = "/index.htm">HOME</a> |
<a href = "/about/contact_us.htm">CONTACT</a> |
<a href = "/about/index.htm">ABOUT</a>
</div>
</html>
This will produce the following result −
The <span> element, on the other hand, can be used to group inline elements only. So, if you
have a part of a sentence or paragraph which you want to group together, you could use the
<span> element as follows.
Example
<!DOCTYPE html>
<html>
<head>
<title>Span Tag Example</title>
</head>
<body>
<p>This is the example of <span style = "color:green">span tag</span>
and the <span style = "color:red">div tag</span> alongwith CSS</p>
</body>
</html>
This will produce the following result −
These tags are commonly used with CSS to allow you to attach a style to a section of a page.
HTML - Comments
Comment is a piece of code which is ignored by any web browser. It is a good practice to add
comments into your HTML code, especially in complex documents, to indicate sections of a
document, and any other notes to anyone looking at the code. Comments help you and others
understand your code and increases code readability.
HTML comments are placed in between <!-- ... --> tags. So, any content placed with-in <!-- ...
--> tags will be treated as comment and will be completely ignored by the browser.
Example
<!DOCTYPE html>
<html>
<body>
<p>Document content goes here.....</p>
</body>
</html>
This will produce the following result without displaying the content given as a part of
comments –
<head>
<title>Valid Comment Example</title>
</head>
<body>
<!-- This is valid comment -->
<p>Document content goes here.....</p>
</body>
</html>
This will produce the following result −
But, following line is not a valid comment and will be displayed by the browser. This is because
there is a space between the left angle bracket and the exclamation mark.
<!DOCTYPE html>
<html>
<head>
<title>Invalid Comment Example</title>
</head>
<body>
< !-- This is not a valid comment -->
<p>Document content goes here.....</p>
</body>
</html>
This will produce the following result −
Multiline Comments
So far we have seen single line comments, but HTML supports multi-line comments as well.
You can comment multiple lines by the special beginning tag <!-- and ending tag --> placed
before the first line and end of the last line as shown in the given example below.
Example
<!DOCTYPE html>
<html>
<head>
<title>Multiline Comments</title>
</head>
<body>
<!--
This is a multiline comment and it can
span through as many as lines you like.
-->
</html>
This will produce the following result −
Conditional Comments
Conditional comments only work in Internet Explorer (IE) on Windows but they are ignored
by other browsers. They are supported from Explorer 5 onwards, and you can use them to give
conditional instructions to different versions of IE.
Example
<!DOCTYPE html>
<html>
<head>
<title>Conditional Comments</title>
<!--[if IE 6]>
Special instructions for IE 6 here
<![endif]-->
</head>
<body>
<p>Document content goes here.....</p>
</body>
</html>
You will come across a situation where you will need to apply a different style sheet based on
different versions of Internet Explorer, in such situation conditional comments will be helpful.
<head>
<title>Using Comment Tag</title>
</head>
<body>
<p>This is <comment>not</comment> Internet Explorer.</p>
</body>
</html>
If you are using IE, then it will produce following result −
But if you are not using IE, then it will produce following result −
<head>
<title>Commenting Script Code</title>
<script>
<!--
document.write("Hello World!")
//-->
</script>
</head>
<body>
<p>Hello , World!</p>
</body>
</html>
This will produce the following result −
Commenting Style Sheets
if you are using Cascading Style Sheet (CSS) in your HTML code then it is recommended to
put that style sheet code inside proper HTML comments so that old browsers can work
properly.
Example
<!DOCTYPE html>
<html>
<head>
<title>Commenting Style Sheets</title>
<style>
<!--
.example {
border:1px solid #4a7d49;
}
//-->
</style>
</head>
<body>
<div class = "example">Hello , World!</div>
</body>
</html>
This will produce the following result −
Following is the list of phrase tags, some of which we have already discussed in HTML
formatting.
o Abbreviation tag : <abbr>
o Acronym tag: <acronym> (not supported in HTML5)
o Marked tag: <mark>
o Strong tag: <strong>
o Emphasized tag : <em>
o Definition tag: <dfn>
o Quoting tag: <blockquote>
o Short quote tag : <q>
o Code tag: <code>
o Keyboard tag: <kbd>
o Address tag: <address>
Example
1. <p>An <abbr title = "Hypertext Markup language">HTML </abbr>language is used
to create web pages. </p>
Output:
2. Marked tag:
The content written between <mark> and </mark> tag will show as yellow mark on browser.
This tag is used to highlight a particular text.
Play Video
Example
1. <p>This tag will <mark>highlight</mark> the text.</p>
Output:
3. Strong text:
This tag is used to display the important text of the content. The text written between <strong>
and </strong> will be displayed as important text.
Example
1. <p>In HTML it is recommended to use <strong>lower-
case</strong>, while writing a code. </p>
Output:
4. Emphasized text
This tag is used to emphasize the text, and displayed the text in italic form. The text written
between <em> and </em> tag will italicized the text.
Example
1. <p>HTML is an <em>easy </em>to learn language.</p>
Output:
5. Definition tag:
When you use the <dfn> and </dfn> tags, it allow to specify the keyword of the content.
Following is the example to show how to definition element.
Example
1. <p><dfn>HTML </dfn> is a markup language. </p>
Output:
6. Quoting text:
The HTML <blockquote> element shows that the enclosed content is quoted from another
source. The Source URL can be given using the cite attribute, and text representation of source
can display using <cite> ..... </cite>element.
Example
1. <blockquote cite="https://www.keepinspiring.me/famous-
quotes/"><p>?The first step toward success is taken when you refuse to be a captive o
f the environment in which you first find yourself.?</p></blockquote>
2. <cite>-Mark Caine</cite>
Output:
7. Short Quotations:
An HTML <q> ....... </q> element defines a short quotation. If you will put any content between
<q> ....... </q>, then it will enclose the text in double quotes.
Example:
1. <p>Steve Jobs said: <q>If You Are Working On Something That You Really Care A
bout, You Don?t Have To Be Pushed. The Vision Pulls You.</q>?</p>
Output:
8. Code tags
The HTML <code> </code> element is used to display the part of computer code. It will display
the content in monospaced font.
1. <p>First Java program</p>
2. <p><code>class Simple{ public static void main(String args[]){
3. System.out.println("Hello Java"); }} </code>
4. </p>
Output:
9. Keyboard Tag
In HTML the keyboard tag, <kbd>, indicates that a section of content is a user input from
keyboard.
Output:
Output:
Tag Description
Defines the deleted text and is used to mark a portion of text which
<del> has been deleted from the document.
Tag Description
<strong> Defines strong text i.e. show the importance of the text.
HTML
<!DOCTYPE html>
<html>
<body>
<bdo dir="ltr">Hello</bdo>
</body>
</html>
Output:
This text will go right-to-left
Hello
This text will go left-to-right
Hello
2. <del> Tag: The <del> tag draws a line cutting the text which shows the text is
wrong or deleted.
Syntax:
<del>Statements... </del>
HTML
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Output:
3. <ins> Tag: The <ins> tag is used to show that some text is inserted into the
document. Browsers will usually show inserted tags underlined.
Syntax:
<ins>Statements... </ins>
HTML
<!DOCTYPE html>
<html>
<body>
<del>1000</del><ins>850</ins>
</p>
</body>
</html>
Output:
4. <strong> Tag: The <strong> tag is used to tell the browser that the text inside this
tag is of great importance and significance. Browser displays this text in bold.
Syntax:
<strong>Statements... </strong>
HTML
<!DOCTYPE html>
<html>
<body>
<p>This is
</p>
</body>
</html>
Output:
5. <code> Tag: Code tag is used to tell the user that the text written inside this tag is
a code of some programming language. The content inside this tag is displayed in
monospace font.
Syntax:
<code>Statements... </code>
HTML
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Output:
6. <kbd> Tag: The <kbd> tag is used to denote that the text shown is input from the
keyboard. The content inside is displayed in a monospace font.
Syntax:
<KBD>Statements... </KBD>
HTML
<!DOCTYPE html>
<html>
<body>
<p>
Press <kbd>Ctrl</kbd> +
</p>
</body>
</html>
Output:
7. <pre> Tag: Text in a pre tag is displayed in fixed width-font, and preserves all
the spaces and break lines i.e. the text is shown exactly as it was written in HTML
source code.
Syntax:
<pre>Statements... </pre>
HTML
<!DOCTYPE html>
<html>
<body>
geeks
this is
pre tag
</pre>
</body>
</html>
Output:
Sample Code using Logical Tags: This example contains all logical tags.
HTML
<!DOCTYPE html>
<html>
<body>
<h1>Logical Tags</h1>
GFG
</abbr>
<br />
GFG
</acronym>
<br />
</address>
<br />
my favourite website.
<br />
<code>
</code>
<blockquote cite=
"https://www.Hello.org/">
and quizzes.
</blockquote>
<p>
and quizzes.
</p>
<kbd>
Hello - This is
a Keyboard input
</kbd>
<pre>
Dear User,
Congratulations !!
Thanks,
GfG Team
</body>
</html>
Output:
HTML Paragraphs Tags:
In this article, we will know the HTML Paragraph, & its basic implementation through
the examples. The <p> tag in HTML defines a paragraph. These have both opening and
closing tags. So anything mentioned within <p> and </p> is treated as a paragraph.
Most browsers read a line as a paragraph even if we don’t use the closing tag i.e, </p>,
but this may raise unexpected results. So, it is a good convention, and we must use the
closing tag.
Syntax:
<p> Content </p>
Example 1: In this example, we are using the <p> tag that is used for paragraphs in
HTML.
HTML
<!DOCTYPE html>
<html>
<body>
<h2>Welcome To Hello</h2>
</body>
</html>
Output:
HTML <p> tag
<html>
<body>
</body>
</html>
Output:
<html>
<body>
<p>
</p>
<p>
</p>
</body>
</html>
Output:
<br> tag: There is a way to let the HTML know where the browser needs to change the
lines by using the <br> tag. These tags do not have any closing tag. So, just a single
opening tag will change the line.
Syntax:
<br>
Example: This example explains the <br> tag inside the <p> tag to add the line-break.
HTML
<!DOCTYPE html>
<html>
<body>
<p>
</p>
</body>
</html>
Output:
Align attribute: The <p> tag specifically supports the alignment attribute and allows
us to align our paragraphs in left, right, or center alignment.
Syntax:
<p align="value">
Example: This example explains the align attribute to align the content in the <p> tag.
HTML
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Output:
<pre> tag: We have seen how the paragraph tag ignores all the changes of lines and
extra spaces within a paragraph, but there is a way to preserve this by the use of
the <pre> tag. It also contains an opening and a closing tag. It displays a text within a
fixed height and width and preserves the extra lines and spaces we use.
Syntax:
<pre> Content </pre>
Example: This example explains the use of the <pre> tag in the <p> tag.
HTML
<!DOCTYPE html>
<html>
<body>
<pre>
tag.
</pre>
<pre>
tag.
</pre>
</body>
</html>
Output:
HTML Heading
A HTML heading or HTML h tag can be defined as a title or a subtitle which you want to
display on the webpage. When you place the text within the heading tags <h1>.........</h1>, it
is displayed on the browser in the bold format and size of the text depends on the number of
heading.
There are six different HTML headings which are defined with the <h1> to <h6> tags, from
highest level h1 (main heading) to the least level h6 (least important heading).
h1 is the largest heading tag and h6 is the smallest one. So h1 is used for most important heading
and h6 is used for least important.
Headings in HTML helps the search engine to understand and index the structure of web
page.
Play Video
Note: The main keyword of the whole content of a webpage should be display by h1 heading tag.
Heading no. 1
Heading no. 2
Heading no. 3
Heading no. 4
Heading no. 5
Heading no. 6
Heading elements (h1....h6) should be used for headings only. They should not be used just to
make text bold or big.
o HTML headings can also be used with nested elements. Following are different codes to
display the way to use heading elements.
Example:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>Heading elements</title>
5. </head>
6. <body>
7. <h1>This is main heading of page. </h1>
8. <p>h1 is the most important heading, which is used to display the keyword of page </p>
9. <h2>This is first sub-heading</h2>
10. <p>h2 describes the first sub heading of page. </p>
11. <h3>This is Second sub-heading</h3>
12. <p>h3 describes the second sub heading of page.</p>
13. <p>We can use h1 to h6 tag to use the different sub-
heading with their paragraphs if
14. required.
15. </p>
16. </body>
17. </html>
Output:
HTML - Formatting
If you use a word processor, you must be familiar with the ability to make text bold, italicized,
or underlined; these are just three of the ten options available to indicate how text can appear
in HTML and XHTML.
Bold Text
Anything that appears within <b>...</b> element, is displayed in bold as shown below −
Example
<!DOCTYPE html>
<html>
<head>
<title>Bold Text Example</title>
</head>
<body>
<p>The following word uses a <b>bold</b> typeface.</p>
</body>
</html>
This will produce the following result −
Italic Text
Anything that appears within <i>...</i> element is displayed in italicized as shown below −
Example
<!DOCTYPE html>
<html>
<head>
<title>Italic Text Example</title>
</head>
<body>
<p>The following word uses an <i>italicized</i> typeface.</p>
</body>
</html>
This will produce the following result −
Underlined Text
Anything that appears within <u>...</u> element, is displayed with underline as shown below
−
Example
<!DOCTYPE html>
<html>
<head>
<title>Underlined Text Example</title>
</head>
<body>
<p>The following word uses an <u>underlined</u> typeface.</p>
</body>
</html>
This will produce the following result −
Strike Text
Anything that appears within <strike>...</strike> element is displayed with strikethrough,
which is a thin line through the text as shown below −
Example
<!DOCTYPE html>
<html>
<head>
<title>Strike Text Example</title>
</head>
<body>
<p>The following word uses a <strike>strikethrough</strike> typeface.</p>
</body>
</html>
This will produce the following result −
Monospaced Font
The content of a <tt>...</tt> element is written in monospaced font. Most of the fonts are
known as variable-width fonts because different letters are of different widths (for example, the
letter 'm' is wider than the letter 'i'). In a monospaced font, however, each letter has the same
width.
Example
<!DOCTYPE html>
<html>
<head>
<title>Monospaced Font Example</title>
</head>
<body>
<p>The following word uses a <tt>monospaced</tt> typeface.</p>
</body>
</html>
This will produce the following result −
Superscript Text
The content of a <sup>...</sup> element is written in superscript; the font size used is the same
size as the characters surrounding it but is displayed half a character's height above the other
characters.
Example
<!DOCTYPE html>
<html>
<head>
<title>Superscript Text Example</title>
</head>
<body>
<p>The following word uses a <sup>superscript</sup> typeface.</p>
</body>
</html>
This will produce the following result −
Subscript Text
The content of a <sub>...</sub> element is written in subscript; the font size used is the same
as the characters surrounding it, but is displayed half a character's height beneath the other
characters.
Example
<!DOCTYPE html>
<html>
<head>
<title>Subscript Text Example</title>
</head>
<body>
<p>The following word uses a <sub>subscript</sub> typeface.</p>
</body>
</html>
This will produce the following result −
Inserted Text
Anything that appears within <ins>...</ins> element is displayed as inserted text.
Example
<!DOCTYPE html>
<html>
<head>
<title>Inserted Text Example</title>
</head>
<body>
<p>I want to drink <del>cola</del> <ins>wine</ins></p>
</body>
</html>
This will produce the following result −
Deleted Text
Anything that appears within <del>...</del> element, is displayed as deleted text.
Example
<!DOCTYPE html>
<html>
<head>
<title>Deleted Text Example</title>
</head>
<body>
<p>I want to drink <del>cola</del> <ins>wine</ins></p>
</body>
</html>
This will produce the following result −
Larger Text
The content of the <big>...</big> element is displayed one font size larger than the rest of the
text surrounding it as shown below −
Example
<!DOCTYPE html>
<html>
<head>
<title>Larger Text Example</title>
</head>
<body>
<p>The following word uses a <big>big</big> typeface.</p>
</body>
</html>
This will produce the following result −
Smaller Text
The content of the <small>...</small> element is displayed one font size smaller than the rest
of the text surrounding it as shown below −
Example
<!DOCTYPE html>
<html>
<head>
<title>Smaller Text Example</title>
</head>
<body>
<p>The following word uses a <small>small</small> typeface.</p>
</body>
</html>
This will produce the following result −
Grouping Content
The <div> and <span> elements allow you to group together several elements to create
sections or subsections of a page.
For example, you might want to put all of the footnotes on a page within a <div> element to
indicate that all of the elements within that <div> element relate to the footnotes. You might
then attach a style to this <div> element so that they appear using a special set of style rules.
Example
<!DOCTYPE html>
<html>
<head>
<title>Div Tag Example</title>
</head>
<body>
<div id = "menu" align = "middle" >
<a href = "/index.htm">HOME</a> |
<a href = "/about/contact_us.htm">CONTACT</a> |
<a href = "/about/index.htm">ABOUT</a>
</div>
</html>
This will produce the following result −
The <span> element, on the other hand, can be used to group inline elements only. So, if you
have a part of a sentence or paragraph which you want to group together, you could use the
<span> element as follows.
Example
<!DOCTYPE html>
<html>
<head>
<title>Span Tag Example</title>
</head>
<body>
<p>This is the example of <span style = "color:green">span tag</span>
and the <span style = "color:red">div tag</span> alongwith CSS</p>
</body>
</html>
This will produce the following result −
These tags are commonly used with CSS to allow you to attach a style to a section of a page.
CHARACTER ENTITY
REFERENCE
A character entity is a code used to represent a character that doesn't belong to
the document's character set. With this mechanism, authors of HTML documents can insert
symbols by writing strings composed by well known and widely supported characters.
There are three ways to write a character entity, and all of them start with an
ampersand ("&") and end with a semicolon (";"):
By its entity name: a standarized and unique name for each character (for
example, á for "á").
By its decimal code: a number sign ("#") followed by a decimal code, unique for
each character (for example, á for "á").
By its hexadecimal code: a number sign ("#") followed by a lowercase letter X
followeb by a hexadecimal code, unique for each character (for
example, á for "á").
Essential Entities
The following table lists the the essential entities in HTML.
Currency Symbols
The following table lists the entities for currency symbols.
General Punctuation
The following table lists the entities for general punctuation.
Arrows
The following table lists the entities for arrows.
Mathematical Symbols
The following table lists the entities for mathematical symbols.
Greek Letters
The following table lists the entities for greek letters.
Miscellaneous
The following table lists the other useful entities supported by HTML.
Character Entity Name Entity Number Description
Hypertext:
Hypertext can be defined as the text shown on your web browser's screen,
which contains hyperlinked data (data here means from which document it
will go to which document) and hence leads readers to different web pages
by clicking on them. In this chapter, you will learn how to create your
hyperlinks and use them to build websites and web pages.
Anchor Tag
The Anchor tag in HTML can be defined as a means to create a hyperlink
that can link your current page on which the text is being converted to
hypertext via <a> (anchor tag) to another page. This anchoring from one
page to another is made possible by the attribute "href", which can be
abbreviated (hypertext reference).
href attribute
The attribute 'href' of the Anchor tag is implemented for defining the
address or path to which this hypertext will get linked. In other words, it
can be said that it directs you out of your page to that destination page,
whose link you have mentioned within the double quotes of the href
attribute as value. The syntax for an anchor tag with href attribute looks
something like this:
Still, many web developers will deposit their link colors in their web pages
to match their site's color scheme. Here's the format:
Run Code
<!DOCTYPE html>
<html>
<body>
<a href="https://www.w3schools.in/">Welcome to w3schools</a>
</body>
</html>
Anchor in Images
Anchor tags can also be used to create hyperlinks through images also. Here's a code snippet
to show how to perform that.
Example:
<!DOCTYPE html>
<html>
<body>
<a href="https://www.w3schools.in/">
<img alt="w3schools logo" src="abc.png" width="170" height="57">
</a>
</body>
</html>
Link Attributes
href attribute
Href stands for hypertext reference. This attribute defines the target address of the
document, which this time is linked to (https://www.google.com). The “=” sign is
the connection of the attribute with the attribute value, whereby “href” is the
attribute and “ https://www.google.com ” is the attribute value. There is an
apostrophe before and after the attribute value. The defined phrase “Google Home”
is known as the anchor text or link text - this is what is visible and clickable for the
user. An anchor text is ideally concise, informative, and relevant to the landing
page.
If you want to link internally to another part of your website, you don’t have to
specify the full address, in this case, the file name is sufficient:
Name attribute
The name attribute of the anchor tag can be used to enable users to “jump” to a
specific point on a page (jump marker, anchor). This is especially useful with large
pages or subdivisions.
<a name="Content"></a>Content
In the first code sample, you link from the bottom of a page back to the beginning,
so users can quickly get to the top of the page without having to scroll for a long
time. In the second example, users can directly access a part of the page, for
example a subdivision point. By clicking, users are guided via name attribute
directly to the subject.
<a href="#Content">Content</a>
By simply setting a hash tag (#) at the anchor name, the browser can identify a jump
within the page.
You can also link to a specific location on another page internally. This would be
defined as follows:
<a href="anotherpage.html#name">Linktext</a>
Target attribute
The target attribute specifies how the destination page or the target document should
be opened. “target=” _ blank “ is used for the opening of the target page in a new
tab. This is the usual option when using target attributes for linking to other pages.
Title attribute
The title attribute gives users an important indication as to where they will be
directed to when they click on a link. If the user hovers with the mouse over a link
reference, the title text, which was defined in the attribute, will be displayed. This
can be a tool tip, or description of the resource to which the link directs. The data
from the title attribute makes the site more user-friendly.
The title attribute can also be used for images and graphic files to give a brief
description of what is shown in the picture.
html
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Output:
html
<!DOCTYPE html>
<html>
<body>
<h2>Topic 1</h2>
<p>paragraph 1
.....</p>
<h2>Topic 2</h2>
<p>paragraph 1
.....</p>
<h2>Topic 3</h2>
<p>paragraph 1
.....</p>
<h2>Topic 4</h2>
<p>paragraph 1
.....</p>
<h2>Topic 5</h2>
<p>paragraph 1
.....</p>
<h2>Topic 6</h2>
<p>paragraph 1
.....</p>
<h2>Topic 7</h2>
<p>paragraph 1
.....</p>
<h2>Topic 8</h2>
<p>paragraph 1
.....</p>
<h2>Topic 9</h2>
<p>paragraph 1
.....</p>
<h2>Topic 10</h2>
<p>paragraph 1
.....</p>
<h2>Topic 12</h2>
<p>paragraph 1
.....</p>
<h2>Topic 13</h2>
<p>paragraph 1
.....</p>
<h2>Topic 14</h2>
<p>paragraph 1
.....</p>
<h2>Topic 15</h2>
<p>paragraph 1
.....</p>
<h2>Topic 16</h2>
<p>paragraph 1
.....</p>
<p>paragraph 1
.....</p>
<h2>Topic 18</h2>
<p>paragraph 1
.....</p>
<h2>Topic 19</h2>
<p>paragraph 1
.....</p>
<p>paragraph 1
.....</p>
</body>
</html
Default Settings
You can specify a default email subject and email body along with your email
address. Following is the example to use default subject and body.
<a href = "mailto:[email protected]?subject = Feedback&body = Message">
Send Feedback
</a>
This code will generate the following link which you can use to send email.
Send Feedback
HTML Image
HTML img tag is used to display image on the web page. HTML img tag is an empty tag that
contains attributes only, closing tags are not used in HTML image element.
Output:
1) src
It is a necessary attribute that describes the source or path of the image. It instructs the browser
where to look for the image on the server.
3) width
It is an optional attribute which is used to specify the width to display the image. It is not
recommended now. You should apply CSS in place of width attribute.
4) height
It h3 the height of the image. The HTML height attribute also supports iframe, image and object
elements. It is not recommended now. You should apply CSS in place of height attribute.
Example:
1. <img src="animal.jpg" height="180" width="300" alt="animal image">
Output:
Note: Always try to insert the image with height and width, else it may flicker while displaying
on webpage.
Output:
Note: If src URL will be incorrect or misspell then it will not display your image on web page, so
try to put correct URL.
Example:
1. <a href="https://www.javatpoint.com/what-is-robotics">
<img src="robot.jpg" height="100" width="100">
</a>
Output:
<head>
<title>Set Image Border</title>
</head>
<body>
<p>Setting image Border</p>
<img src = "/html/images/test.png" alt = "Test Image" border = "3"/>
</body>
</html>
This will produce the following result –
<head>
<title>Set Image Alignment</title>
</head>
<body>
<p>Setting image Alignment</p>
<img src = "/html/images/test.png" alt = "Test Image"
border = "3" align = "right"/>
</body>
</html>
This will produce the following result −
Image Maps
What is image mapping :
The HTML <map> tag defines an image map. An image map is an image with
clickable areas.
Example
Here is the HTML source code for the image map above:
To create an image map you need an image, and some HTML code that
describes the clickable areas
The Image
The image is inserted using the <img> tag. The only difference from other
images is that you must add a usemap attribute:
The usemap value starts with a hash tag # followed by the name of the image
map, and is used to create a relationship between the image and the image
map.
The <map> element is used to create an image map, and is linked to the image
by using the required name attribute:
<map name="workmap">
The name attribute must have the same value as the <img>'s usemap attribute .
The Areas
Then, add the clickable areas.
Shape
You must define the shape of the clickable area, and you can choose one of
these values:
You must also define some coordinates to be able to place the clickable area
onto the image.
Shape="rect"
The coordinates for shape="rect" come in pairs, one for the x-axis and one for
the y-axis.
So, the coordinates 34,44 is located 34 pixels from the left margin and 44
pixels from the top:
The coordinates 270,350 is located 270 pixels from the left margin and 350
pixels from the top:
Example
<area shape="rect" coords="34, 44, 270, 350" href="computer.htm">
This is the area that becomes clickable and will send the user to the page
"computer.htm":
Shape="circle"
To add a circle area, first locate the coordinates of the centre of the circle:
337,300
Then specify the radius of the circle:
44 pixels
Example
<area shape="circle" coords="337, 300, 44" href="coffee.htm">
This is the area that becomes clickable and will send the user to the page "coffee.htm":
Shape="poly"
The shape="poly" contains several coordinate points, which creates a shape
formed with straight lines (a polygon).
How can we make the croissant in the image below become a clickable link?
We have to find the x and y coordinates for all edges of the croissant:
The coordinates come in pairs, one for the x-axis and one for the y-axis:
Example
<area shape="poly" coords="140,121,181,116,204,160,204,222,191,270,140,
329,85,355,58,352,37,322,40,259,103,161,128,147" href="croissant.htm">
This is the area that becomes clickable and will send the user to the page
"croissant.htm":
Image Map and JavaScript
A clickable area can also trigger a JavaScript function.
Example
Here, we use the onclick attribute to execute a JavaScript function when the
area is clicked:
<map name="workmap">
<area shape="circle" coords="337,300,44" href="coffee.htm" onclick="m
yFunction()">
</map>
<script>
function myFunction() {
alert("You clicked the coffee cup!");
}
</script>
When the user clicks inside the image the browser will append the X and Y
coordinates (relative to the upper-left corner of the image) to the
anchor URL as a query string and will access the resulting URL (for
example, /imagemapper?3,9 ).
If the browser does not support ismap then the query string must not be added
to the anchor URL and the server should respond appropriately (for example,
by returning a text-only navigation page).
A major disadvantage of server-side image maps is that their implementation can differ
between different server systems. This means that the author of the website using image
maps needs to communicate closely with the Web server's administrator to ensure that the
maps work correctly.
Since there are so many advantages to client-side imagemaps, and almost all current
browser versions support them, we shall not explore server-side image maps further in this
unit (although most image map programmes still provide features to output/edit server-side
map files).
Client-side
Client-side image maps were introduced in HTML 3.2, and do not require
any special logic to be executed on the server (they are fully client-side). They
also do not require any JavaScript.
Pure HTML
A client-side imagemap in HTML consists of two parts:
1. the actual image, which is embedded with the <img> tag. The
image tag must have an attribute usemap, which names the
imagemap to use for this image (multiple imagemaps may exist
on a single page).
2. A <map> element, and inside that, <area> elements, each of which
defines a single clickable area within the imagemap. These are
similar to the <a> tag defining which URL should be opened for
an ordinary web link. A title attribute may be provided, which
may be rendered as a tooltip if a desktop user hovers their mouse
pointer over the area. For web accessibility reasons, it is often
important – and in some cases it may even be a legal or
contractual requirement – to provide an alt attribute describing
the link that screen reader software can read to, for
example, blind users.[3]
The <area> elements can be rectangles ( shape="rect" ), polygons
( shape="poly" ) or circles ( shape="circle" ). Shape-Values are coordinate-pairs.
Every pair has an X and a Y value (from left/top of an image) and is separated
with a comma.
Rectangle: Set four coordinates: "x1,y1,x2,y2"
Polygon: Set as many coordinates as desired (a multiple of two):
"x1,y1,x2,y2, [...] xn,yn"
Circle: One coordinate-pair and another value with a radius:
"x1,y1,radius"
The following example defines a rectangular area ("9,372,66,397"). When a
user clicks anywhere inside this area, they are taken to the English
Wikipedia's home page.
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML nobr Tag</title>
</head>
<body>
<nobr>This is a very long sequence of text that is forced to be
on a single line, even if doing so causes <wbr />
the browser to extend the document window beyond the size of the
viewing pane and the poor user must scroll right <wbr />
</html>
<body>
<h2>GeeksforGeeks</h2>
<p>Hello Geeks!.</p>
<!--font tag-->
<font size="5"> Welcome to GeeksforGeeks </font>
</body>
</html>
The font tag has basically three attributes which are given below:
Font Size attribute
Face/Type attribute
Color attribute
Note: Font tag is not supported in HTML5.
We will discuss all these attributes & understand them through the examples.
font Size: This attribute is used to adjust the size of the text in the HTML document
using a font tag with the size attribute. The range of size of the font in HTML is from
1 to 7 and the default size is 3.
Syntax:
<font size="number">
Example: This example uses the <font> tag where different font sizes are
specified.
HTML
<!DOCTYPE html>
<html>
<body>
<!--HTML font size tag starts here-->
<font size="1">GeeksforGeeks!</font><br />
<font size="2">GeeksforGeeks!</font><br />
<font size="3">GeeksforGeeks!</font><br />
<font size="4">GeeksforGeeks!</font><br />
<font size="5">GeeksforGeeks!</font><br />
<font size="6">GeeksforGeeks!</font><br />
<font size="7">GeeksforGeeks!</font>
<!--HTML font size tag ends here-->
</body>
</html>
Font Type: Font type can be set by using face attribute with font tag in HTML
document. But the fonts used by the user need to be installed in the system first.
Syntax:
<font face="font_family">
Example: This example describes the <font> tag with different font type & font
size.
HTML
<!DOCTYPE html>
<html>
<body>
<!--HTML font face tag starts here-->
<font face="Times New Roman" size="6">
GeeksforGeeks!!
</font> <br />
<font face="Verdana" size="6">
GeeksforGeeks!!
</font><br />
<font face="Comic sans MS" size=" 6">
GeeksforGeeks!!
</font><br />
<font face="WildWest" size="6">
GeeksforGeeks!!
</font><br />
<font face="Bedrock" size="6">
GeeksforGeeks!!
</font><br />
<!--HTML font face tag ends here-->
</body>
</html>
Output:
Font Color: Font color is used to set the text color using a font tag with the color
attribute in an HTML document. Color can be specified either with its name or with
its hex code.
Syntax:
<font color="color_name|hex_number|rgb_number">
Example: This example describes the <font> tag with different font colors.
HTML
<!DOCTYPE html>
<html>
<body>
</html>
HTML - Tables
The HTML tables allow web authors to arrange data like text, images, links, other
tables, etc. into rows and columns of cells.
The HTML tables are created using the <table> tag in which the <tr> tag is used to
create table rows and <td> tag is used to create data cells. The elements under <td>
are regular and left aligned by default
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Tables</title>
</head>
<body>
<table border = "1">
<tr>
<td>Row 1, Column 1</td>
<td>Row 1, Column 2</td>
</tr>
<tr>
<td>Row 2, Column 1</td>
<td>Row 2, Column 2</td>
</tr>
</table>
</body>
</html>
This will produce the following result –
Here, the border is an attribute of <table> tag and it is used to put a border across all
the cells. If you do not need a border, then you can use border = "0".
Table Heading
Table heading can be defined using <th> tag. This tag will be put to replace <td> tag,
which is used to represent actual data cell. Normally you will put your top row as
table heading as shown below, otherwise you can use <th> element in any row.
Headings, which are defined in <th> tag are centered and bold by default.
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Table Header</title>
</head>
<body>
<table border = "1">
<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>
This will produce the following result –
Name Salary
Ramesh Raman 5000
Shabbir Hussein 7000
Example
<!DOCTYPE html>
<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>
This will produce the following result –
Name Salary
Ramesh Raman 5000
Shabbir Hussein 7000
It specifies the space between the border It specifies the space between adjacent
of a table cell and its contents. cells.
It is created by using HTML <table> tag It is also created by using HTML <table>
but type attribute is set to cellpadding. tag but type attribute is set to cellspacing.
Example
<!DOCTYPE html>
<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>
This will produce the following result –
Tables Backgrounds
You can set table background using one of the following two ways −
bgcolor attribute − You can set background color for whole table or just
for one cell.
background attribute − You can set background image for whole table
or just for one cell.
You can also set border color also using bordercolor attribute.
Note − The bgcolor, background, and bordercolor attributes deprecated in HTML5.
Do not use these attributes.
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Table Background</title>
</head>
<body>
<table border = "1" bordercolor = "green" bgcolor = "yellow">
<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>
This will produce the following result –
<!DOCTYPE html>
<html>
<head>
<title>HTML Table Background</title>
</head>
<body>
<table border = "1" bordercolor = "green" background =
"/images/test.png">
<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>
This will produce the following result. Here background image did not apply to table's
header.
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Table Width/Height</title>
</head>
<body>
<table border = "1" width = "400" height = "150">
<tr>
<td>Row 1, Column 1</td>
<td>Row 1, Column 2</td>
</tr>
<tr>
<td>Row 2, Column 1</td>
<td>Row 2, Column 2</td>
</tr>
</table>
</body>
</html>
This will produce the following result –
Table Caption
The caption tag will serve as a title or explanation for the table and it shows up at
the top of the table. This tag is deprecated in newer version of HTML/XHTML.
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Table Caption</title>
</head>
<body>
<table border = "1" width = "100%">
<caption>This is the caption</caption>
<tr>
<td>row 1, column 1</td><td>row 1, columnn 2</td>
</tr>
<tr>
<td>row 2, column 1</td><td>row 2, columnn 2</td>
</tr>
</table>
</body>
</html>
This will produce the following result –
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Table</title>
</head>
<body>
<table border = "1" width = "100%">
<thead>
<tr>
<td colspan = "4">This is the head of the table</td>
</tr>
</thead>
<tfoot>
<tr>
<td colspan = "4">This is the foot of the table</td>
</tr>
</tfoot>
<tbody>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
<td>Cell 4</td>
</tr>
</tbody>
</table>
</body>
</html>
This will produce the following result –
Nested Tables
You can use one table inside another table. Not only tables you can use almost all the
tags inside table data tag <td>.
Example
Following is the example of using another table and other tags inside a table cell.
<!DOCTYPE html>
<html>
<head>
<title>HTML Table</title>
</head>
<body>
<table border = "1" width = "100%">
<tr>
<td>
<table border = "1" width = "100%">
<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>
</td>
</tr>
</table>
</body>
</html>
This will produce the following result −
Name Salary
Ramesh Raman 5000
Shabbir Hussein 7000
HTML - Frames
HTML frames are used to divide your browser window into multiple sections where
each section can load a separate HTML document. A collection of frames in the
browser window is known as a frameset. The window is divided into frames in a
similar way the tables are organized: into rows and columns.
Disadvantages of Frames
There are few drawbacks with using frames, so it's never recommended to use frames
in your webpages −
Some smaller devices cannot cope with frames often because their
screen is not big enough to be divided up.
Sometimes your page will be displayed differently on different
computers due to different screen resolution.
The browser's back button might not work as the user hopes.
There are still few browsers that do not support frame technology.
Creating Frames
To use frames on a page we use <frameset> tag instead of <body> tag. The
<frameset> tag defines, how to divide the window into frames. The rows attribute of
<frameset> tag defines horizontal frames and cols attribute defines vertical frames.
Each frame is indicated by <frame> tag and it defines which HTML document shall
open into the frame.
Note − The <frame> tag deprecated in HTML5. Do not use this element.
Example
Following is the example to create three horizontal frames −
<!DOCTYPE html>
<html>
<head>
<title>HTML Frames</title>
</head>
</frameset>
</html>
This will produce the following result –
Example
Let's put the above example as follows, here we replaced rows attribute by cols and
changed their width. This will create all the three frames vertically −
<!DOCTYPE html>
<html>
<head>
<title>HTML Frames</title>
</head>
<noframes>
<body>Your browser does not support frames.</body>
</noframes>
</frameset>
</html>
This will produce the following result –
1 A percentage of the browser window. For example, to create three vertical frames, use cols
= "10%, 80%, 10%".
Using a wildcard symbol. For example, to create three vertical frames, use cols = "10%, *,
10%". In this case wildcard takes remainder of the window.
As relative widths of the browser window. For example, to create three vertical frames,
use cols = "3*, 2*, 1*". This is an alternative to percentages. You can use relative widths of
the browser window. Here the window is divided into sixths: the first column takes up half
of the window, the second takes one third, and the third takes one sixth.
rows
This attribute works just like the cols attribute and takes the same values, but it is used to
2
specify the rows in the frameset. For example, to create two horizontal frames, use rows =
"10%, 90%". You can specify the height of each row in the same way as explained above
for columns.
border
3 This attribute specifies the width of the border of each frame in pixels. For example,
border = "5". A value of zero means no border.
frameborder
This attribute specifies whether a three-dimensional border should be displayed
4
between frames. This attribute takes value either 1 (yes) or 0 (no).
For example frameborder = "0" specifies no border.
framespacing
This attribute specifies the amount of space between frames in a frameset.
5
This can take any integer value. For example framespacing = "10" means there
should be 10 pixels spacing between each frames.
The <frame> Tag Attributes
Following are the important attributes of <frame> tag −
src
1 This attribute is used to give the file name that should be loaded in the frame. Its value can
be any URL. For example, src = "/html/top_frame.htm" will load an HTML file available in
html directory.
name
This attribute allows you to give a name to a frame. It is used to indicate which frame a
2
document should be loaded into. This is especially important when you want to create links
in one frame that load pages into an another frame, in which case the second frame needs
a name to identify itself as the target of the link.
frameborder
This attribute specifies whether or not the borders of that frame are shown; it
3
overrides the value given in the frameborder attribute on the <frameset> tag if
one is given, and this can take values either 1 (yes) or 0 (no).
marginwidth
This attribute allows you to specify the width of the space between the left and
4
right of the frame's borders and the frame's content. The value is given in pixels.
For example marginwidth = "10".
marginheight
This attribute allows you to specify the height of the space between the top and
5
bottom of the frame's borders and its contents. The value is given in pixels.
For example marginheight = "10".
noresize
6
By default, you can resize any frame by clicking and dragging on the borders
of a frame. The noresize attribute prevents a user from being able to resize the
frame. For example noresize = "noresize".
scrolling
This attribute controls the appearance of the scrollbars that appear on the frame.
7
This takes values either "yes", "no" or "auto". For example scrolling = "no"
means it should not have scroll bars.
longdesc
This attribute allows you to provide a link to another page containing a long
8
description of the contents of the frame. For example
longdesc = "framedescription.htm"
1<!DOCTYPE html>
<html>
<head>
<title>HTML Target Frames</title>
</head>
<frameset cols = "200, *">
<frame src = "/html/menu.htm" name = "menu_page" />
<frame src = "/html/main.htm" name = "main_page" />
<noframes>
</html>
Here, we have created two columns to fill with two frames. The first frame is 200
pixels wide and will contain the navigation menu bar implemented by menu.htm file.
The second column fills in remaining space and will contain the main part of the page
and it is implemented by main.htm file. For all the three links available in menu bar,
we have mentioned target frame as main_page, so whenever you click any of the
links in menu bar, available link will open in main page.
Following is the content of menu.htm file
Live Demo
<!DOCTYPE html>
<html>
</html>
Following is the content of main.htm file −
Live Demo
<!DOCTYPE html>
<html>
</html>
When we load test.htm file, it produces following result −
Now you can try to click links available in the left panel and see the result.
The targetattribute can also take one of the following values −
_self
1
Loads the page into the current frame.
_blank
2
Loads a page into a new browser window. Opening a new window.
_parent
3
Loads the page into the parent window, which in the case of a single frameset is the main
browser window.
_top
4
Loads the page into the browser window, replacing any current frames.
targetframe
5
Loads the page into a named targetframe.
HTML Lists
In this article, we will know the HTML List, along with understanding its types, and
various ways to implement them, through the example.
A list is a record of short pieces of related information or used to display the data or any
information on web pages in the ordered or unordered form. For instance, to purchase
the items, we need to prepare a list that can either be ordered or unordered list which
helps us to organize the data & easy to find the item. Please refer to the HTML <li> type
Attribute article for the various types of attributes that can be used with the ordered &
unordered list.
Example: The below example illustrates the use of the unordered & ordered list in
HTML.
HTML
<!DOCTYPE html>
<html>
<head>
<title>GeeksforGeeks</title>
</head>
<body>
<ul>
<li>Web Technology</li>
<li>Programming Languages</li>
</ul>
<ol>
<li>Array</li>
<li>Linked List</li>
<li>Stacks</li>
<li>Queues</li>
<li>Trees</li>
<li>Graphs</li>
</ol>
</body>
</html>
Output:
HTML List
<html>
<body>
<h2>Grocery list</h2>
<ul>
<li>Bread</li>
<li>Eggs</li>
<li>Milk</li>
<li>Coffee</li>
</ul>
</body>
</html>
Output:
Unordered List
<html>
<head>
<title>HTML ul tag</title>
</head>
<body>
<h1>GeeksforGeeks</h1>
<ul style="list-style-type:disc">
<li>Geeks</li>
<li>Sudo</li>
<li>Gfg</li>
<li>Gate</li>
<li>Placement</li>
</ul>
</body>
</html>
Output:
Example 2: The Circle can be used to set the list item marker to a circle.
HTML
<!DOCTYPE html>
<html>
<body>
<h1>GeeksforGeeks</h1>
<h2>Unordered List with Circle Bullets</h2>
<li>Geeks</li>
<li>Sudo</li>
<li>Gfg</li>
<li>Gate</li>
<li>Placement</li>
</ul>
</body>
</html>
Output:
Example 3: The Square can be used to set the list item marker to a square.
HTML
<!DOCTYPE html>
<html>
<body>
<h1>GeeksforGeeks</h1>
<li>Geeks</li>
<li>Sudo</li>
<li>Gfg</li>
<li>Gate</li>
<li>Placement</li>
</ul>
</body>
</html>
Output:
Example 4: It’s none that can be used to set the list item marker with no mark.
HTML
<!DOCTYPE html>
<html>
<body>
<h1>GeeksforGeeks</h1>
<li>Geeks</li>
<li>Sudo</li>
<li>Gfg</li>
<li>Gate</li>
<li>Placement</li>
</ul>
</body>
</html>
Output:
Example: Nested Unordered List, It is used to nest the list items ie., a list inside another
list.
HTML
<!DOCTYPE html>
<html>
<body>
<h1>GeeksforGeeks</h1>
<ul>
<li>DSA</li>
<ul>
<li>Array</li>
<li>Linked List</li>
<li>stack</li>
<li>Queue</li>
</ul>
<li>Web Technologies</li>
<ul>
<li>HTML</li>
<li>CSS</li>
<li>JavaScript</li>
</ul>
<li>Aptitude</li>
<li>Gate</li>
<li>Placement</li>
</ul>
</body>
</html>
Output:
HTML Ordered List: An ordered list starts with the “ol” tag. Each list item starts with
the “li” tag. The list items are marked with numbers by default.
Syntax:
<ol>
<li>Item1</li>
<li>Item2</li>
<li>Item3</li>
</ol>
Attributes:
compact: It defines the list should be compacted (compact attribute is not
supported in HTML5. Use CSS instead.).
reversed: It defines that the order will be descending.
start: It defines from which number or alphabet the order will start.
type: It defines which type(1, A, a, I, and i) of the order you want in your
list of numeric, alphabetic, or roman numbers.
Example: This example illustrates the use of the reverse attribute, control list counting
& type attribute.
HTML
<!DOCTYPE html>
<html>
<head>
<title>HTML ol tag</title>
</head>
<body>
<h3>HTML ol tag</h3>
<p>reversed attribute</p>
<ol reversed>
<li>HTML</li>
<li>CSS</li>
<li>JS</li>
</ol>
<p>start attribute</p>
<ol start="5">
<li>HTML</li>
<li>CSS</li>
<li>JS</li>
</ol>
<p>type attribute</p>
<ol type="i">
<li>HTML</li>
<li>CSS</li>
<li>JS</li>
</ol>
</body>
</html>
Output:
HTML ordered list has various list item markers: The type attribute of the <ol> tag
defines the type of the list item marker.
Example 1: The list items will be numbered with numbers i.e default.
HTML
<!DOCTYPE html>
<html>
<body>
<ol type="1">
<li>Bread</li>
<li>Eggs</li>
<li>Milk</li>
<li>Coffee</li>
</ol>
</body>
</html>
Output:
Example 2: Type=”A”, this list of items will be numbered with uppercase letters.
HTML
<!DOCTYPE html>
<html>
<body>
<ol type="A">
<li>Bread</li>
<li>Eggs</li>
<li>Milk</li>
<li>Coffee</li>
</ol>
</body>
</html>
Output:
Example 3: Type=”a”, this list of items will be numbered with lowercase letters.
HTML
<!DOCTYPE html>
<html>
<body>
<ol type="a">
<li>Bread</li>
<li>Eggs</li>
<li>Milk</li>
<li>Coffee</li>
</ol>
</body>
</html>
Output:
Ordered List with small alphabetic item maker
Example 4: Type=”I”, this list of items will be numbered with uppercase roman
numbers.
HTML
<!DOCTYPE html>
<html>
<body>
<ol type="I">
<li>Bread</li>
<li>Eggs</li>
<li>Milk</li>
<li>Coffee</li>
</ol>
</body>
</html>
Output:
Example 5: Type=”i”, this list of items will be numbered with lowercase roman
numbers.
HTML
<!DOCTYPE html>
<html>
<body>
<ol type="i">
<li>Bread</li>
<li>Eggs</li>
<li>Milk</li>
<li>Coffee</li>
</ol>
</body>
</html>
Output:
Example 6: Nested ordered list, a nested ordered list is a list that has a list inside another
list.
HTML
<!DOCTYPE html>
<html>
<body>
<h1>GeeksforGeeks</h1>
<ol>
<li>Coffee</li>
<li> Tea
<ol>
<li>Black tea</li>
<li>Green tea</li>
</ol>
</li>
<li>Milk</li>
</ol>
</body>
</html>
Output:
There is another attribute that is specifically defined for a list item, which is used in
with the “li” tag and that is the value attribute. Below is a little description of the
value attribute specifically used with the “li” tag. Though it is used with various other
HTML elements.
Value attribute:
The value attribute may be used on an individual <li> element within an ordered list to
change its value within the list. You define the value of a list item and the number of
any list item appearing after it will be recalculated accordingly.
Example: This example illustrates the use of the “value attribute” used on the <li>
element.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h2>Welcome To GFG</h2>
<ol>
<li>Item One</li>
<li>Item Three</li>
<li>Item Four</li>
</ol>
</body>
</html>
Output:
<img src="https://media.geeksforgeeks.org/wp-
content/uploads/20220522105327/GFG20220522105224.jpg" alt="Use of "value
attribute" on
” srcset=”https://media.geeksforgeeks.org/wp-
content/uploads/20220522105327/GFG20220522105224.jpg, ” sizes=”100vw”
width=”237″>Value attribute
HTML Description List: A description list is a list of terms, with a description of each
term. The <dl> tag defines the description list, the <dt> tag defines the term name, and
the <dd> tag describes each term. Please refer to the How to add description list of an
element using HTML? article for further details.
Syntax:
<dl> Contents... </dl>
Example: This example describes the HTML Description List.
HTML
<!DOCTYPE html>
<html>
<body>
<h2>A Description List</h2>
<dl>
<dt>Coffee</dt>
<dt>Milk</dt>
</dl>
</body>
</html>
Output:
Description List
HTML Form
An HTML form is a section of a document which contains controls such as text fields,
password fields, checkboxes, radio buttons, submit button, menus etc.
An HTML form facilitates the user to enter data that is to be sent to the server for
processing such as name, email address, password, phone number, etc. .
For example: If a user want to purchase some items on internet, he/she must fill the
form such as shipping address and credit/debit card details so that item can be sent
to the given address.\
Tag Description
Tag Description
Note: The <form> element does not itself create a form but it is container to contain
all required form elements, such as <input>, <label>, etc.
Syntax:
1. <form>
2. //Form elements
3. </form>
HTML <input> element
The HTML <input> element is fundamental form element. It is used to create form
fields, to take input from user. We can apply different input filed to gather different
information form user. Following is the example to show the simple text input.
Example:
<body>
<form>
Enter your name <br>
<input type="text" name="username">
</form>
</body>
Output:
1. <form>
2. First Name: <input type="text" name="firstname"/> <br/>
3. Last Name: <input type="text" name="lastname"/> <br/>
4. </form>
Output:
Note: If you will omit 'name' attribute then the text filed input will not be submitted
to server.
Example:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>Form in HTML</title>
5. </head>
6. <body>
7. <form>
8. Enter your address:<br>
9. <textarea rows="2" cols="20"></textarea>
10. </form>
11. </body>
12. </html>
Output:
Label Tag in Form
It is considered better to have label in form. As it makes the code parser/browser/user
friendly.
If you click on the label tag, it will focus on the text control. To do so, you need to have
for attribute in label tag that must be same as id attribute of input tag.
NOTE: It is good to use <label> tag with form, although it is optional but if you will
use it, then it will provide a focus when you tap or click on label tag. It is more worthy
with touchscreens.
1. <form>
2. <label for="firstname">First Name: </label> <br/>
3. <input type="text" id="firstname" name="firstname"/> <br/>
4. <label for="lastname">Last Name: </label>
5. <input type="text" id="lastname" name="lastname"/> <br/>
6. </form>
Output:
HTML Password Field Control
The password is not visible to the user in password field control.
1. <form>
2. <label for="password">Password: </label>
3. <input type="password" id="password" name="password"/> <br/>
4. </form>
Output:
1. <form>
2. <label for="email">Email: </label>
3. <input type="email" id="email" name="email"/> <br/>
4. </form>
Note: If we will not enter the correct email, it will display error like:
If you use one name for all the radio buttons, only one radio button can be selected at
a time.
Using radio buttons for multiple options, you can only choose a single option at a time.
1. <form>
2. <label for="gender">Gender: </label>
3. <input type="radio" id="gender" name="gender" value="male"/>M
ale
4. <input type="radio" id="gender" name="gender" value="female"/>Female
<br/>
5. </form>
Checkbox Control
The checkbox control is used to check multiple options from given checkboxes.
1. <form>
2. Hobby:<br>
3. <input type="checkbox" id="cricket" name="cricket" value="cricket"
/>
4. <label for="cricket">Cricket</label> <br>
5. <input type="checkbox" id="football" name="football" value="footb
all"/>
6. <label for="football">Football</label> <br>
7. <input type="checkbox" id="hockey" name="hockey" value="hockey
"/>
8. <label for="hockey">Hockey</label>
9. </form>
Note: These are similar to radio button except it can choose multiple options at a
time and radio button can select one button at a time, and its display.
Output:
Submit button control
HTML <input type="submit"> are used to add a submit button on web page. When
user clicks on submit button, then form get submit to the server.
Syntax:
The value attribute can be anything which we write on button on web page.
Example:
<form>
<label for="name">Enter name</label><br>
<input type="text" id="name" name="name"><br>
<label for="pass">Enter Password</label><br>
<input type="Password" id="pass" name="pass"><br>
<input type="submit" value="submit">
</form>
Output:
HTML <fieldset> element:
The <fieldset> element in HTML is used to group the related information of a form.
This element is used with <legend> element which provide caption for the grouped
elements.
Example:
<form>
<fieldset>
<legend>User Information:</legend>
<label for="name">Enter name</label><br>
<input type="text" id="name" name="name"><br>
<label for="pass">Enter Password</label><br>
<input type="Password" id="pass" name="pass"><br>
<input type="submit" value="submit">
</fieldset>
</form>
Output:
HTML Form Example
Following is the example for a simple form of registration.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>Form in HTML</title>
5. </head>
6. <body>
7. <h2>Registration form</h2>
8. <form>
9. <fieldset>
10. <legend>User personal information</legend>
11. <label>Enter your full name</label><br>
12. <input type="text" name="name"><br>
13. <label>Enter your email</label><br>
14. <input type="email" name="email"><br>
15. <label>Enter your password</label><br>
16. <input type="password" name="pass"><br>
17. <label>confirm your password</label><br>
18. <input type="password" name="pass"><br>
19. <br><label>Enter your gender</label><br>
20. <input type="radio" id="gender" name="gender" value="male"/>Male <br>
21. <input type="radio" id="gender" name="gender" value="female"/>Fe
male <br/>
22. <input type="radio" id="gender" name="gender" value="others"/>others <br
/>
23. <br>Enter your Address:<br>
24. <textarea></textarea><br>
25. <input type="submit" value="sign-up">
26. </fieldset>
27. </form>
28. </body>
29. </html>
Output:
1. <form action="#">
2. <table>
3. <tr>
4. <td class="tdLabel"><label for="register_name" class="label">Enter name:</labe
l></td>
5. <td><input type="text" name="name" value="" id="register_name" style=
"width:160px"/></td>
6. </tr>
7. <tr>
8. <td class="tdLabel"><label for="register_password" class="label">Enter password
:</label></td>
9. <td><input type="password" name="password" id="register_password" st
yle="width:160px"/></td>
10. </tr>
11. <tr>
12. <td class="tdLabel"><label for="register_email" class="label">Enter Email:</label
></td>
13. <td
14. ><input type="email" name="email" value="" id="register_email" style="width:160p
x"/></td>
15. </tr>
16. <tr>
17. <td class="tdLabel"><label for="register_gender" class="label">Enter Gen
der:</label></td>
18. <td>
19. <input type="radio" name="gender" id="register_gendermale" value="male"
/>
20. <label for="register_gendermale">male</label>
21. <input type="radio" name="gender" id="register_genderfemale" value="fem
ale"/>
22. <label for="register_genderfemale">female</label>
23. </td>
24. </tr>
25. <tr>
26. <td class="tdLabel"><label for="register_country" class="label">Select Country:<
/label></td>
27. <td><select name="country" id="register_country" style="width:160px">
28. <option value="india">india</option>
29. <option value="pakistan">pakistan</option>
30. <option value="africa">africa</option>
31. <option value="china">china</option>
32. <option value="other">other</option>
33. </select>
34. </td>
35. </tr>
36. <tr>
37. <td colspan="2"><div align="right"><input type="submit" id="register_0
" value="register"/>
38. </div></td>
39. </tr>
40. </table>
41. </form>
HTML Form Elements
<input>
<label>
<select>
<textarea>
<button>
<fieldset>
<legend>
<datalist>
<output>
<option>
<optgroup>
Example
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname">
All the different values of the type attribute are covered in the next
chapter: HTML Input Types.
The <label> element is useful for screen-reader users, because the screen-
reader will read out loud the label when the user focus on the input element.
The <label> element also help users who have difficulty clicking on very small
regions (such as radio buttons or checkboxes) - because when the user clicks
the text within the <label> element, it toggles the radio button/checkbox.
The for attribute of the <label> tag should be equal to the id attribute of
the <input> element to bind them together.
Example
<label for="cars">Choose a car:</label>
<select id="cars" name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
Example
<option value="fiat" selected>Fiat</option>
Visible Values:
Use the size attribute to specify the number of visible values:
Example
<label for="cars">Choose a car:</label>
<select id="cars" name="cars" size="3">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
Example
<label for="cars">Choose a car:</label>
<select id="cars" name="cars" size="4" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
Example
<textarea name="message" rows="10" cols="30">
The cat was playing in the garden.
</textarea>
The rows attribute specifies the visible number of lines in a text area.
You can also define the size of the text area by using CSS:
Example
<textarea name="message" style="width:200px; height:600px;">
The cat was playing in the garden.
</textarea>
Example
<button type="button" onclick="alert('Hello World!')">Click
Me!</button>
Click Me!
Note: Always specify the type attribute for the button element. Different
browsers may use different default types for the button element.
Example
<form action="/action_page.php">
<fieldset>
<legend>Personalia:</legend>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
</fieldset>
</form>
Last name:
Doe
Submit
Users will see a drop-down list of the pre-defined options as they input data.
The list attribute of the <input> element, must refer to the id attribute of
the <datalist> element.
Example
<form action="/action_page.php">
<input list="browsers">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
</form>
Example
Perform a calculation and show the result in an <output> element:
<form action="/action_page.php"
oninput="x.value=parseInt(a.value)+parseInt(b.value)">
0
<input type="range" id="a" name="a" value="50">
100 +
<input type="number" id="b" name="b" value="50">
=
<output name="x" for="a b"></output>
<br><br>
<input type="submit">
</form>
HTML Exercises
Test Yourself With Exercises
Exercise:
In the form below, add an empty drop down list with the name "cars".
<form action="/action_page.php">
< >
</ >
</form>
Submit Answer »
This chapter describes the different types for the HTML <input> element.
<input type="button">
<input type="checkbox">
<input type="color">
<input type="date">
<input type="datetime-local">
<input type="email">
<input type="file">
<input type="hidden">
<input type="image">
<input type="month">
<input type="number">
<input type="password">
<input type="radio">
<input type="range">
<input type="reset">
<input type="search">
<input type="submit">
<input type="tel">
<input type="text">
<input type="time">
<input type="url">
<input type="week">
First name:
Last name:
Example
<form>
<label for="username">Username:</label><br>
<input type="text" id="username" name="username"><br>
<label for="pwd">Password:</label><br>
<input type="password" id="pwd" name="pwd">
</form>
Username:
Password:
The form-handler is typically a server page with a script for processing input
data.
Example
<form action="/action_page.php">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
</form>
First name:
John
Last name:
Doe
Submit
If you omit the submit button's value attribute, the button will get a default
text:
Example
<form action="/action_page.php">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit">
</form>
Input Type Reset
<input type="reset"> defines a reset button that will reset all form values to
their default values:
Example
<form action="/action_page.php">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
<input type="reset">
</form>
First name:
John
Last name:
Doe
Submit Reset
If you change the input values and then click the "Reset" button, the form-
data will be reset to the default values.
Radio buttons let a user select ONLY ONE of a limited number of choices:
Example
<p>Choose your favorite Web language:</p>
<form>
<input type="radio" id="html" name="fav_language" value="HTML">
<label for="html">HTML</label><br>
<input type="radio" id="css" name="fav_language" value="CSS">
<label for="css">CSS</label><br>
<input type="radio" id="javascript" name="fav_language" value="JavaSc
ript">
<label for="javascript">JavaScript</label>
</form>
HTML
CSS
JavaScript
Example
<form>
<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">
<label for="vehicle1"> I have a bike</label><br>
<input type="checkbox" id="vehicle2" name="vehicle2" value="Car">
<label for="vehicle2"> I have a car</label><br>
<input type="checkbox" id="vehicle3" name="vehicle3" value="Boat">
<label for="vehicle3"> I have a boat</label>
</form>
I have a bike
I have a car
I have a boat
Example
<input type="button" onclick="alert('Hello World!')" value="Click Me!">
This is how the HTML code above will be displayed in a browser:
Depending on browser support, a color picker can show up in the input field.
Example
<form>
<label for="favcolor">Select your favorite color:</label>
<input type="color" id="favcolor" name="favcolor">
</form>
Depending on browser support, a date picker can show up in the input field.
Example
<form>
<label for="birthday">Birthday:</label>
<input type="date" id="birthday" name="birthday">
</form>
You can also use the min and max attributes to add restrictions to dates:
Example
<form>
<label for="datemax">Enter a date before 1980-01-01:</label>
<input type="date" id="datemax" name="datemax" max="1979-12-
31"><br><br>
<label for="datemin">Enter a date after 2000-01-01:</label>
<input type="date" id="datemin" name="datemin" min="2000-01-02">
</form>
Input Type Datetime-local
The <input type="datetime-local"> specifies a date and time input field, with no
time zone.
Depending on browser support, a date picker can show up in the input field.
Example
<form>
<label for="birthdaytime">Birthday (date and time):</label>
<input type="datetime-local" id="birthdaytime" name="birthdaytime">
</form>
Some smartphones recognize the email type, and add ".com" to the keyboard
to match email input.
Example
<form>
<label for="email">Enter your email:</label>
<input type="email" id="email" name="email">
</form>
Example
<form>
<input type="image" src="img_submit.gif" alt="Submit" width="48" height
="48">
</form>
Example
<form>
<label for="myfile">Select a file:</label>
<input type="file" id="myfile" name="myfile">
</form>
A hidden field lets web developers include data that cannot be seen or
modified by users when a form is submitted.
A hidden field often stores what database record that needs to be updated
when the form is submitted.
Note: While the value is not displayed to the user in the page's content, it is
visible (and can be edited) using any browser's developer tools or "View
Source" functionality. Do not use hidden inputs as a form of security!
Example
<form>
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<input type="hidden" id="custId" name="custId" value="3487">
<input type="submit" value="Submit">
</form>
Depending on browser support, a date picker can show up in the input field.
Example
<form>
<label for="bdaymonth">Birthday (month and year):</label>
<input type="month" id="bdaymonth" name="bdaymonth">
</form>
The following example displays a numeric input field, where you can enter a
value from 1 to 5:
Example
<form>
<label for="quantity">Quantity (between 1 and 5):</label>
<input type="number" id="quantity" name="quantity" min="1" max="5">
</form>
Input Restrictions
Here is a list of some common input restrictions:
Attribute Description
checked Specifies that an input field should be pre-selected when the page lo
type="radio")
disabled Specifies that an input field should be disabled
You will learn more about input restrictions in the next chapter.
The following example displays a numeric input field, where you can enter a
value from 0 to 100, in steps of 10. The default value is 30:
Example
<form>
<label for="quantity">Quantity:</label>
<input type="number" id="quantity" name="quantity" min="0" max="100"
step="10" value="30">
</form>
Example
<form>
<label for="vol">Volume (between 0 and 50):</label>
<input type="range" id="vol" name="vol" min="0" max="50">
</form>
Example
<form>
<label for="gsearch">Search Google:</label>
<input type="search" id="gsearch" name="gsearch">
</form>
Example
<form>
<label for="phone">Enter your phone number:</label>
<input type="tel" id="phone" name="phone" pattern="[0-9]{3}-[0-9]{2}-
[0-9]{3}">
</form>
Try it Yourself »
Depending on browser support, a time picker can show up in the input field.
Example
<form>
<label for="appt">Select a time:</label>
<input type="time" id="appt" name="appt">
</form>
Try it Yourself »
Some smartphones recognize the url type, and adds ".com" to the keyboard
to match url input.
Example
<form>
<label for="homepage">Add your homepage:</label>
<input type="url" id="homepage" name="homepage">
</form>
Try it Yourself »
Input Type Week
The <input type="week"> allows the user to select a week and year.
Depending on browser support, a date picker can show up in the input field.
Example
<form>
<label for="week">Select a week:</label>
<input type="week" id="week" name="week">
</form>
Example
Input fields with initial (default) values:
<form>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe">
</form>
Try it Yourself »
The readonly Attribute
The input readonly attribute specifies that an input field is read-only.
A read-only input field cannot be modified (however, a user can tab to it,
highlight it, and copy the text from it).
The value of a read-only input field will be sent when submitting the form!
Example
A read-only input field:
<form>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John" readonly><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe">
</form>
Try it Yourself »
The value of a disabled input field will not be sent when submitting the form!
Example
A disabled input field:
<form>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John" disabled><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe">
</form>
Try it Yourself »
ADVERTISEMENT
Note: The size attribute works with the following input types: text, search,
tel, url, email, and password.
Example
Set a width for an input field:
<form>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" size="50"><br>
<label for="pin">PIN:</label><br>
<input type="text" id="pin" name="pin" size="4">
</form>
Try it Yourself »
Note: When a maxlength is set, the input field will not accept more than the
specified number of characters. However, this attribute does not provide any
feedback. So, if you want to alert the user, you must write JavaScript code.
Example
Set a maximum length for an input field:
<form>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" size="50"><br>
<label for="pin">PIN:</label><br>
<input type="text" id="pin" name="pin" maxlength="4" size="4">
</form>
Try it Yourself »
The min and max attributes work with the following input types: number,
range, date, datetime-local, month, time and week.
Tip: Use the max and min attributes together to create a range of legal
values.
Example
Set a max date, a min date, and a range of legal values:
<form>
<label for="datemax">Enter a date before 1980-01-01:</label>
<input type="date" id="datemax" name="datemax" max="1979-12-
31"><br><br>
Try it Yourself »
The multiple attribute works with the following input types: email, and file.
Example
A file upload field that accepts multiple values:
<form>
<label for="files">Select files:</label>
<input type="file" id="files" name="files" multiple>
</form>
Try it Yourself »
The pattern attribute works with the following input types: text, date, search,
url, tel, email, and password.
Tip: Use the global title attribute to describe the pattern to help the user.
Example
An input field that can contain only three letters (no numbers or special
characters):
<form>
<label for="country_code">Country code:</label>
<input type="text" id="country_code" name="country_code"
pattern="[A-Za-z]{3}" title="Three letter country code">
</form>
Try it Yourself »
The short hint is displayed in the input field before the user enters a value.
The placeholder attribute works with the following input types: text, search,
url, tel, email, and password.
Example
An input field with a placeholder text:
<form>
<label for="phone">Enter a phone number:</label>
<input type="tel" id="phone" name="phone"
placeholder="123-45-678"
pattern="[0-9]{3}-[0-9]{2}-[0-9]{3}">
</form>
Try it Yourself »
The required attribute works with the following input types: text, search, url,
tel, email, password, date pickers, number, checkbox, radio, and file.
Example
A required input field:
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
</form>
Try it Yourself »
Tip: This attribute can be used together with the max and min attributes to
create a range of legal values.
The step attribute works with the following input types: number, range, date,
datetime-local, month, time and week.
Example
An input field with a specified legal number intervals:
<form>
<label for="points">Points:</label>
<input type="number" id="points" name="points" step="3">
</form>
Try it Yourself »
Note: Input restrictions are not foolproof, and JavaScript provides many
ways to add illegal input. To safely restrict input, it must also be checked by
the receiver (the server)!
Example
Let the "First name" input field automatically get focus when the page loads:
<form>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" autofocus><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname">
</form>
Try it Yourself »
Tip: Always specify both the height and width attributes for images. If height
and width are set, the space required for the image is reserved when the
page is loaded. Without these attributes, the browser does not know the size
of the image, and cannot reserve the appropriate space to it. The effect will
be that the page layout will change during loading (while the images load).
Example
Define an image as the submit button, with height and width attributes:
<form>
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname"><br><br>
<input type="image" src="img_submit.gif" alt="Submit" width="48" heig
ht="48">
</form>
Try it Yourself »
Example
An <input> element with pre-defined values in a <datalist>:
<form>
<input list="browsers">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
</form>
Try it Yourself »
Autocomplete allows the browser to predict the value. When a user starts to
type in a field, the browser should display options to fill in the field, based on
earlier typed values.
The autocomplete attribute works with <form> and the following <input> types:
text, search, url, tel, email, password, datepickers, range, and color.
Example
An HTML form with autocomplete on, and off for one input field:
Try it Yourself »