HTML
HTML
HTML | Basics
In this article, we will go through all the basic stuff of HTML coding. There are
various tags that we must consider and include while starting to code in HTML.
These tags help in organization and basic formatting of elements in our script or web
pages. These step by step procedures will guide you through the process of writing
HTML.
Basic HTML Document
Below mentioned are the basic HTML tags which divides the whole document into
various parts like head, body etc.
Every HTML document begins with a HTML document tag. Although this is not
mandatory but it is a good convention to start the document with this below
mentioned tag:
<!DOCTYPE html>
<html> : Every HTML code must be enclosed between basic HTML tags. It
begins with <html> and ends with </html> tag.
<head> : The head tag comes next which contains all the header information of
the web page or document like the title of the page and other miscellaneous
information. These informations are enclosed within head tag which opens
with <head> and ends with </head>. The contents will of this tag will be
explained in the later sections of course.
<title> : We can mention the title of a web page using the <title> tag. This is a
header information and hence mentioned within the header tags. The tag
begins with <title> and ends with </title>
<body> : Next step is the most important of all the tags we have learned so far.
The body tag contains the actual body of the page which will be visible to all the
users. This opens with <body> and ends with </body>. Every content enclosed
within this tag will be shown on the web page be it writings or images or audios
or videos or even links. We will see later in the section how using various tags
we may insert mentioned contents into our web pages.
HTML | Layout
Page layout is the part of graphic design that deals with the arrangement of visual
elements on a page. Page layout is used to make the web pages look better. It
establishes the overall appearance, relative importance, and relationships between
the graphic elements to achieve a smooth flow of information and eye movement for
maximum effectiveness or impact.
2
1 HTML | Heading
There are six levels of headings defined by HTML. These six heading elements are
H1, H2, H3, H4, H5, and H6; with H1 being the highest level and H6 the least.
Importance of Heading :
1. Search Engines use headings for indexing the structure and content of the
webpage.
2. Headings are used for highlighting important topics.
3. They provide valuable information and tell us about the structure of the
document.
Horizontal rule in HTML :
The “hr” tag which stands for the horizontal rule is used to define a thematic break in an
HTML page. It is basically used to separate content.
2 HTML | Paragraph
Last Updated: 01-11-2019
<p> tag:
The <p> tag in HTML defines a paragraph. These have both opening and closing
tag. 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 both a good convention and
we must use the closing tag.
Syntax:
<p> Content </p>
When we look at the webpage, we see that there are few spaces added before and after a
paragraph. HTML does this by default. Let‟s look at few properties of paragraph tag:
3
<br> tag:
There is a way to let the HTML know where does the browser need to change the
lines by the use of <br> tag. These tags do not have any closing tag. So, just a
single opening tag will change the line.
Syntax:
<br>
<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">
3 HTML | Links
What is a link?
It is a connection from one web resource to another.A link has two ends,An anchor
and direction. The link starts at the “source” anchor and points to the “destination”
anchor, which may be any Web resource such as an image, a video clip, a sound
bite, a program, an HTML document or an element within an HTML document.
HTML Link Syntax
Links are specified in HTML using the “a” tag.
Syntax Explanation:
href : The href attribute is used to specify
the destination address of the link used.
Text link : The text link
is the visible part of the link.
4
4 HTML | Images
Last Updated: 30-07-2019
Earlier, the webpages used to comprise of only texts, which made them appear quite
boring and uninteresting. Fortunately, it wasn‟t long enough that the ability to embed
images on web pages was added for users. Let‟s see how to add images on a
webpage.
Adding images on a webpage :
The “img” tag is used to add images on a webpage. The “img” tag is an empty tag,
which means it can contain only a list of attributes and it has no closing tag.
Syntax :
<img src="url" alt="some_text">
Attribute:
src:
src stands for source. Every image has a src attribute which tells the browser
where to find the image you want to display. The URL of the image provided
points to the location where the image is stored.
alt:If the image cannot be displayed then the alt attribute acts as an alternative
description for the image. The value of the alt attribute is an user-defined text.
Setting width and height of Image :The width and height attributes are used to specify the
height and width of an image. The attribute values are specified in pixels by default.
Adding animated Image: Animated images in .gif format can also be added using
the “img” tag.
filter_none
edit
play_arrow
brightness_4
<!DOCTYPE html>
<html>
<h3>Adding a gif file on a webpage</h3>
<body>
<img src = "smiley.gif" alt = "smiley"
style = "width:200px; height:200px;">
</body>
</html>
Adding titles to Image: Along with the images, titles can also be added to images
to provide further information related to the inserted image. For inserting a title, the
title attribute is used.
Setting a border to Image :By default, every picture has a border around it. By using the
border attribute, the thickness of the border can be changed. A thickness of “0” means that
there will be no border around the picture.
5
Aligning a Picture: By default, an image is aligned at the left side of the page, but it can be
aligned to center or right using the align attribute.
Adding Image as a Link: An image can work as a link with a URL embedded in it. It can be
done by using the “img” tag inside an “a” tag.
<a href="https://ide.geeksforgeeks.org/tryit.php">
<img src= "https://download3.png" alt="GeeksforGeeks logo"> </a>
There are three basic html elements which are required for creating a mapped
image.
1. Map : It is used to create a map of the image with clickable areas.
2. Image : It is used for the image source on which mapping is done.
3. Area : It is used within the map for defining clickable areas.
6 HTML | Lists
What is a list?
A list is a record of short pieces of information, such as people‟s names, usually
written or printed with a single thing on each line and ordered in a way that makes a
particular thing easy to find.
For example:
A shopping list
To-do list
Lists in HTML
HTML offers three ways for specifying lists of information. All lists must contain one
or more list
elements.
The types of lists that can be used in HTML are :
ul : An unordered list. This will list items using plain bullets.
ol : An ordered list. This will use different schemes of numbers to list your
items.
dl : A definition list. This arranges your items in the same way as they are
arranged in a dictionary.
The Unordered HTML List
An unordered list starts with the “ul” tag. Each list item starts with the “li” tag.The list
items are marked with bullets i.e small black circles by default.
<ul> Output
<li>Bread</li>
<li>Eggs</li>
<li>Milk</li>
<li>Coffee</li>
</ul>
<ul style="list-style-type:disc">
<li>Bread</li> ….
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.
7
<ol>
<li>Bread</li>
<li>Eggs</li>
<li>Milk</li>
<li>Coffee</li>
</ol>
1. Making text Bold or Strong: We can make the text bold using the <b> tag.
The tag uses both opening and closing tag. The text that needs to be made
bold must be within <b> and </b> tag.
We can also use the <strong> tag to make the text strong, with added
semantic importance. It also opens with <strong> and ends with </strong> tag.
2 Making text Italic or emphasize: The <i> tag is used to italicise the text. It opens with <i>
and ends with </i> tag.
The <em> tag is used to emphasize the text, with added semantic importance. It opens with
<em> and ends with </em> tag.
3 Highlighting a text: It is also possible to highlight a text in HTML using the <mark> tag. It
has a opening tag <mark> and a closing tag </mark>.
4 Making a text Subscript or Superscript: The <sup> element is used to superscript a text
and <sub> element is used to subscript a text. They both have opening and a closing tag.
<!--Text in Normal-->
<p>Hello GeeksforGeeks</p>
<!--Text in Superscript-->
<p>Hello
<sup>GeeksforGeeks</sup></p>
<!--Text in Subcript-->
<p>Hello
<sub>GeeksforGeeks</sub></p>
5 Making text smaller: The <small> element is used to make the text smaller. The text that
needs to be displayed smaller should be written inside <small> and </small> tag.
6 Striking through the text: The <del> element is used to strike through the text marking
the part as deleted. It also has an opening and a closing tag.
7 Adding a text: The <ins> element is used to underline a text marking the part as inserted
or added. It also has an opening and a closing tag.
8 HTML | Attributes
An attribute is used to provide extra or additional information about an element.
9
All HTML elements can have attributes. Attributes provide additional information
about an element.
It takes two parameters : a name and a value. These define the properties of
the element and is placed inside the opening tag of the element. The name
parameter takes the name of the property we would like to assign to the
element and the value takes the properties value or extent of the property
names that can be aligned over the element.
Every name has some value that must be written within quotes.
Syntax:
<element attribute_name="attribute_value">
Below are some of the most commonly used Attributes in HTML:
1. src Attribute :If we want to insert an image into a webpage, then we need to
use the <img> tag and the src attribute. We will need to specify the address of
the image as the attribute‟s value inside the double quote.
2. alt Attribute : As the name goes this is an alternate tag that is used to show or display
something if the primary attribute i.e., the <img> tag, fails to display the value assigned
to it. This can also be used to describe the image to a developer who is actually sitting
at the coding end.
<img Output
src="https://media.geeksforgeeks.org/
wp-content/cdn-uploads/Geek_logi_-
low_res.png" alt="The Logo"><br>
<img src="" alt="Since the src
value is blank,the alt value is
displayed">
</body>
</html>
3. The width and height Attribute : This attribute is used to adjust the width and height
of an image.
5. The title Attribute : The title attribute is used to explain an element on hovering the
mouse over it. The behavior differs with various elements but generally the value is
displayed while loading or hovering mouse pointer over it.
6. The href Attribute : This attribute is used to specify a link to any address. This
attribute is used along with <a> tag. The link put inside the href attribute gets linked to
the text displayed inside the<a> tag.
On clicking on the text we will be redirected to the link. By default, the link gets opened
10
in the same tag but by using the target attribute and setting its value to “_blank”, we
will be redirected to another tab or another window based on the browsers
configuration.
<a Output:
href="https://www.geeksforgeeks.org/">
Click to open in the same tab </a><br>
<a
href="https://www.geeksforgeeks.org/"
target="_blank">
Click to open in a different tab </a>
7. The style Attribute : This attribute is used to provide various CSS(Cascading Style
Sheets) effects to the HTML elements such as increasing font-size, changing font-
family, coloring etc. For a detailed explanation refer HTML | Style Tag. The program
below shows few name and values that go along the style attribute.
<h2 style="font-family:Chaparral
Pro Light;">Hello
GeeksforGeeks.</h2>
<h3 style="font-size:20px;">Hello
GeeksforGeeks.</h3>
<h2 style="color:#8CCEF9;">Hello
GeeksforGeeks.</h2>
<h2 style="text-
align:center;">Hello
GeeksforGeeks.</h2>
8. The lang attribute : The language is declared with the lang attribute. Declaring a
language is can be important for accessibility applications and search engines.
9 HTML | Iframes
Significance of Iframe in HTML :
The iframe in HTML stands for Inline Frame. The ” iframe ” tag defines a rectangular
region within the document in which the browser can display a separate document,
including scrollbars and borders. An inline frame is used to embed another document
within the current HTML document.
The „ src „ attribute is used to specify the URL of the document that occupies the
iframe.
Syntax :
<iframe src="URL"></iframe>
Setting Height and Width in Iframe :
The height and width attributes are used to specify the size of the iframe. The
attribute values are specified in pixels by default, but they can also be specified in
percentage like ” 80% “.
11
<iframe
src="https://ide.geeksforgeeks.org/tryit
.php"
height="
300" width="400">
</iframe>
Explanation: In the above example CSS styles all elements with the class name
“country”.
Using the class attribute in JavaScript: JavaScript can access elements with a
specified class name by using the getElementsByClassName() method.
11 HTML | Id Attributes
The id attribute is a unique identifier which is used to specify the document. It is used
by CSS and JavaScript to perform a certain task for a unique element. In CSS, the id
12
External Style Sheet: External Style Sheets method can be useful when the CSS has to be
applied to various web pages. An external style sheet holds all the style rules in a separate
document that you can link from an HTML file on your site.
There are two ways of attaching external style sheets –
h1{
color:#00FF00;
}
2. RGB Style [Red Green Blue] : In this we need to give 3 numbers indicating the
amount of red, green and blue colors respectively required in the mixed color.
The range of each color is from 0 to 255.
Example: If we want all „h1‟ tags of green color.
3. h1{
4. color:rgb(0, 255, 0);
5. }
Note: rgba(0, 0, 0) is Black color and rgb(255, 255, 255) is White color.
6. RGBA Style [Red Green Blue Alpha] : This style allows us to make the color
transparent according to our will. Alpha indicates the degree of transparency.
The range of green, blue and red is from 0 to 255 and that of alpha is from 0 to
1.
Example: If we want all „h1‟ tags of green color.
h1{
color:rgba(11, 99, 150, 1);}
7. HSL colors : Here „H‟ stands for hue, „S‟ for Saturation and „L‟ for Lightness.
HSL color values are specified as:
Syntax:
hsl(hue, saturation, lightness)
Hue is the color of the image itself. It‟s range is from 0 to 360. 0 is for red,
120 is for green and 240 is for blue.
Saturation is the intensity/purity of the hue. 0% is for a shade of gray and
100% is the full color.
When color is fully saturated, the color is considered in purest/truest
version.
Lightness is the color space‟s brightness. 0% is for black, 100% is for
white.
Example : If we apply hue on the above example given in example 1.
h1{
color:#00FF00;
background-color: hsl(200, 20%, 40%);
color: hsl(300, 30%, 90%);
}
In total we have 4096 different color combinations as we have the range of red,
green and blue from 00 to FF each so we have 16*16*16 different combinations
14
of colors. Then with hue, saturation and lightness we can achieve even more
creative and large number of colors.
15 HTML | Quotations
The Quotation elements in HTML are used to insert quoted texts in a web page, that
is, portion of texts different from the normal texts in the web page.
Below are some of the most used quotation elements of HTML:
1. <q> element:
The <q> element is used to set a set of text inside the quotation marks. It has
both opening and closing tags.
16 HTML | Calculator
Here a Calculator is going to be formed with HTML code.
Calculator Title: This is the title at the top of our application, “GeeksforGeeks
Calculator”.
15
Output Screen: This will be our output screen, where all text will be shown.
Like the input that the user will type and the answer calculated from the user
input. So, we can again break down this into two smaller pieces as shown
below:
Question Output: This will be the input given by the user.
Answer Output: This will be the result calculated from user input.
17 HTML | Geolocation
Geo-location in HTML5 is used to share the location with some web site and aware
of the exact location. It is mainly used for local businesses, restaurants or showing
location on the map. It uses JavaScript to give the latitude and longitude to the
backend server. Most of the browsers supports Geo-location API. Geo-location API
uses global navigator object which can be created as follows:
var loc = navigator.geolocation
To determine whether or not the drop target is to accept the drop. If the drop is
Occurs when the mouse leaves an element before a valid drop target while the
ondrop Specifies where the drop was occurred at the end of the drag operation
16
The Data Transfer Object: The data transfer property is used when the whole
process of Drag and Drop happens. It is used to hold the dragged data from the
source and drop it to the desired location. These are the properties associated with
it:
ATTRIBUTES DESCRIPTION
dataTransfer.files dropped.
and uninitialized.
ev.dataTransfer.setData("text", ev.target.id);
}
Step 2: Dropping the Object
The ondragover event specifies where the dragged data can be dropped.
By default, data/elements cannot be dropped in other elements. To allow a
drop, it must prevent the default handling of the element. This is done by
calling the event.preventDefault() method
Finally, the drop event, which allows the actual drop to be performed
function dragDrop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
ev.target.appendChild(document.getElementById(data));
}
18
Reserved Characters: There are certain characters which sometimes have special
meanings in the URL and it can be used in both ways. For example the „/‟ character
is a reserved character and it has a special meaning when being used as a delimiter
to separate the paths of a URL. Here it is used by encoding it to „%2F‟. Else when it
has no special purpose it can be used normally.
Some characters need to be encoded while some don‟t need to be. Here is the
classification shows the group of characters that need to be encoded.
Safe Characters: Alphanumeric i.e. 0-9, a-z and A-Z, special characters $, -, _,
., +, !, *, „, (, ), and reserved characters used for their reserved purposes. These
characters have no need to be encoded.
ASCII Control characters: Ii includes the characters ranging from 00-1F in hex
(0-31 decimal) and 7F (127 decimal). These characters needs to be encoded.
Non-ASCII Control characters: It includes 80-FF in hex (128-255 decimal).
These characters needs to be encoded.
Reserved characters: These characters are used for special purpose an they
requires encoding.
Unsafe characters: These character can be misunderstood within URLs for
various reasons. So it requires encoding. The characters < and > are unsafe
because they are used as the delimiters around URLs in free text, the quote
mark (” “) is unsafe as it is used to delimit URLs in some systems.
21 HTML | Tables
A table is an arrangement of data in rows and columns, or possibly in a more
complex structure. Tables are widely used in communication, research, and data
analysis.
Tables are useful for various tasks such as presenting text information and
numerical data.
Tables can be used to compare two or more items in tabular form layout.
Tables are used to create databases.
Defining Tables in HTML
An HTML table is defined with the “table” tag. Each table row is defined with the “tr”
tag. A table header is defined with the “th” tag. By default, table headings are bold
and centered. A table data/cell is defined with the “td” tag.
1. Adding a border to a HTML Table: A border is set using the CSS border
property. If you do not specify a border for the table, it will be displayed without
borders.
2. Adding Collapsed Borders in a HTML Table: For borders to collapse into one
border, add the CSS border-collapse property.
3. Adding Cell Padding in a HTML Table: Cell padding specifies the space between
the cell content and its borders.If we do not specify a padding, the table cells will be
20
4. Adding Left Align Headings in a HTML Table : By default the table headings are
bold and centered. To left-align the table headings,we must use the CSS text-align
property.
5. Adding Border Spacing in a HTML Table: Border spacing specifies the space
between the cells. To set the border spacing for a table,we must use the CSS border-
spacing property.
22 HTML | Comments
The comment tag (<!– Comment –>) is used to insert comments in the HTML code.
It is a good practice of coding so that coder and the reader can get help to
understand the code. It is helpful to understand the complex code. The comment tag
is useful during the debugging of codes.
It is simple piece of code which is wiped off (ignore) by web browsers i.e, not
displayed by browser.
It helps the coder and reader to understand the piece of code used for specially
in complex source code.
Syntax:
<!-- Comments here -->
Types of HTML Comments: There are three types of comments in HTML which
are:
Single-line comment
Multi-lines comment
Using <comment> tag
23 HTML | Doctypes
What is doctype?
A doctype or document type declaration is an instruction which tells the web browser
about the markup language in which the current page is written. The Doctype is not
an element or tag, it lets the browser know about the version of or standard of HTML
or any other markup language that is being used in the document.
21
Input Elements are the most common elements which are used in HTML Forms.
Various user input fields can be created such as textfield, check box, password field,
radio button, submit button etc. The most common input elements are listed below:
1. Text Field in HTML Forms :
The text field is a one line input field allowing the user to input text. Text Field input
controls are created using the “input” element with a type attribute having value as
“text”.
2. Password Field in HTML Forms :
Password fields are a type of text field in which the text entered is masked using
asterisk or dots for prevention of user identity from another person who is looking onto
the screen. Password Field input controls are created using the “input” element with a
type attribute having value as “password”.
3. Radio Buttons in HTML Form :
Radio Buttons are used to let the user select exactly one option from a list of
predefined options. Radio Button input controls are created using the “input” element
with a type attribute having value as “radio”.
4. Checkboxes in HTML Form :
Checkboxes are used to let the user select one or more options from a pre-defined set
22
of options. Checkbox input controls are created using the “input” element with a type
attribute having value as “checkbox”.
5. File select boxes in HTML Forms :
File select boxes are used to allow the user to select a local file and send it as an
attachment to the web server.It is similar to a text box with a button which allows the
user to browse for a file.Instead of browsing for the file, the path and the name of the
file can also be written. File select boxes are created using the “input” element with a
type attribute having value as “file”.
CSS Introduction
Course Navigation
Selector => h1
Declaration => {color:blue;font size:12px;}
The selector points to the HTML element you want to style.
The declaration block contains one or more declarations separated by
semicolons.
Each declaration includes a CSS property name and a value, separated by a
colon.
For Example:
-> color is property and blue is value.
-> font size is property and 12px is value.
A CSS declaration always ends with a semicolon, and declaration blocks are
surrounded by curly braces.
Example :
In the following example all p elements will be center-aligned, with a blue text color:
filter_none
brightness_4
p {
color: blue;
text-align: center;
}
CSS Selectors
CSS selectors are used to “find” (or select) HTML elements based on their element
name, id, class, attribute, and more.
1. THE UNIVERSAL SELECTORS : Rather than selecting elements of a specific
type, the universal selector quite simply matches the name of any element type
This rule renders the content of every element in our document in black.
2. THE ELEMENT SELECTOR : The element selector selects elements based on the
element name. You can select all p elements on a page like this (in this case, all p
elements will be center-aligned, with a red text color) :
3. THE DESCENDANT SELECTOR : Suppose you want to apply a style rule to a
particular element only when it lies inside a particular element. As given in the following
example, style rule will apply to em element only when it lies inside ul tag.
24
4. THE ID SELECTOR :
CSS Versions
1. CSS1
2. CSS2
3. CSS3
4. CSS4
Version 4 comes with:-
CSS-Pro CSS-Mobile
JavaScript
JavaScript is a lightweight, cross-platform and interpreted scripting language. It is well-
known for the development of web pages, many non-browser environments also use it.
JavaScript can be used for Client-side developments as well as Server-side developments
JavaScript was created in the first place for DOM manipulation. Earlier websites
were mostly static, after JS was created dynamic Web sites were made.
Functions in JS are objects. They may have properties and methods just like
another object. They can be passed as arguments in other functions.
Can handle date and time.
Performs Form Validation although the forms are created using HTML.
No compiler needed.
Introduction to JavaScript
JavaScript is a lightweight, cross-platform and interpreted scripting language. It is
well-known for the development of web pages, many non-browser environments also
use it. JavaScript can be used for Client-side developments as well as Server-
side developments. JavaScript contains a standard library of objects,
like Array, Date, and Math, and a core set of language elements
like operators, control structures, and statements.
25
Art: Artists and designers can create whatever they want using JavaScript to
draw on HTML 5 canvas, make sound more effective also can be
used p5.js library.
Machine Learning: This JavaScript ml5.js library can be used in web
development by using machine learning.
DHTML JavaScript
Last Updated: 03-12-2018
DHTML stands for Dynamic HTML. Dynamic means that the content of the web page
can be customized or changed according to user inputs i.e. a page that is interactive
with the user. In earlier times, HTML was used to create a static page. It only defined
the structure of the content that was displayed on the page. With the help of CSS,
we can beautify the HTML page by changing various properties like text size,
background color etc. The HTML and CSS could manage to navigate between static
pages but couldn‟t do anything else. If 1000 users view a page that had their
information for eg. Admit card then there was a problem because 1000 static pages
for this application build to work. As the number of users increase, the problem also
increases and at some point it becomes impossible to handle this problem.
To overcome this problem, DHTML came into existence. DHTML included
JavaScript along with HTML and CSS to make the page dynamic. This combo made
the web pages dynamic and eliminated this problem of creating static page for each
user. To integrate JavaScript into HTML, a Document Object Model(DOM) is made
for the HTML document. In DOM, the document is represented as nodes and objects
which are accessed by different languages like JavaScript to manipulate the
document.
Validating inputs
<body>
<h1>GeeksforGeeks</h1>
<p id = "geeks">Hello Geeks!</p>
<script style = "text/javascript">
document.getElementById("geeks").innerHTML =
"A computer science portal for geeks";
</script>
</body>
</html>
Output: