HTML Basics
HTML Basics
Tags Descriptions
Encloses the entire HTML document, serving as the root element for all HTML
<html>
content.
Contains header information about the webpage, including title, meta tags, and
<head> linked stylesheets. It is part of the document’s structure but is not displayed on the
webpage.
Used within the <head> section to define the title of the HTML document. It
<title> appears in the browser tab or window and provides a brief description of the
webpage’s content.
<body> Encloses the visible content of the webpage, such as text, images, audio, videos, and
Basic HTML Tags for Document Structure
Tags Descriptions
links. All elements within this tag are displayed on the actual webpage when viewed
in a browser.
HTML Headings
The HTML heading tags are used to create headings for the
content of a webpage. These tags are typically placed inside
the body tag. HTML offers six heading tags, from <h1> to
<h6>, each displaying the heading in a different font size.
Syntax:
<h1></h1>
<h2></h2>
<h3></h3>
<h4></h4>
<h5></h5>
<h6></h6>
Example 2: To demonstrate the use of 6 heading tags
from <h1> to <h6> in HTML.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>HTML heading tag</title>
</head>
<body>
<h1>Heading 1 (h1)</h1>
<h2>Heading 2 (h2)</h2>
<h3>Heading 3 (h3)</h3>
<h4>Heading 4 (h4)</h4>
<h5>Heading 5 (h5)</h5>
<h6>Heading 6 (h6)</h6>
</body>
</html>
Output:
// for Break
<br>
Example 3: To illustrates the use of <p> tag for writing a
paragraph statement in HTML.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>
Example of paragraph and break elements
</title>
</head>
<body>
<p> HTML stands for HyperText Markup Language.<br>
It is used to design web pages using a markup
language.<br>HTML is a combination of Hypertext
and Markup language.<br>Hypertext defines the
link between web pages.<br>A markup language
is used to define the text document within the
tag which defines the structure of web pages.
</p>
</body>
</html>
Output:
</html>
Output:
HTML Images
The <img> tag is used to insert an image into a webpage. The
source of the image is specified within the src attribute, like
this: <img src=”source_of_image”>.
Syntax: <img src="geeks.png">
Example 5: To illustrates the use of the <img> tag for
inserting the images in HTML.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>HTML img tag</title>
</head>
<body>
<img src=
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/Geek_logi_-
low_res.png">
</body>
</html>
Output:
Tags Description
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Text Formatting Example</title>
</head>
<body>
<p>
<strong>Strong:</strong>
This text is important and bold.
</p>
<p>
<em>Emphasized:</em>
This text is emphasized and italic.
</p>
<p>
<b>Bold:</b>
This text is bold.
</p>
<p>
<i>Italic:</i>
This text is italic.
</p>
<p>
<mark>Marked:</mark>
This text is highlighted.
</p>
</body>
</html>Output:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Advanced Text Formatting</title>
</head>
<body>
<p>This is a
<strong><em>very important</em></strong> message.
</p>
<p>The chemical formula of water is H
<sub>2</sub>O.
</p>
<p>
<del>Deleted text</del> and
<ins>inserted text</ins>
are shown here.
</p>
<p><small>Smaller text</small>
can be used for disclaimers.
</p>
<p>E = mc<sup>2</sup></p>
</body>
</html>
Output:
HTML Quotations
The HTML Quotation elements are used to insert quoted
texts in a web page, that is the portion of texts different from
the normal texts in the web page. Below are some of the most
used quotation elements of the HTML.
Tag Description
<q>
Defines short inline quotation, enclosed in quotation
marks.
<!DOCTYPE html>
<html>
<head>
<title>HTML Quotations</title>
</head>
<body>
<h3>GeeksforGeeks</h3>
<!--Normal text-->
<p>
The quick brown fox jumps over the lazy dog<br />
</p>
<!--Inside <bdo> tag-->
<p>
<bdo dir="rtl">The quick brown fox jumps over the lazy
dog</bdo>
</p>
<p>
Welcome to
<abbr title="GeeksforGeeks">GfG</abbr>
</p>
<address>
<p>
Address:<br />
710-B, Advant Navis Business Park,<br />
Sector-142, Noida Uttar Pradesh – 201305
</p>
</address>
</body>
</html>
Output:
<!DOCTYPE html>
<html>
<head>
<title>HTML Quotations Example</title>
</head>
<body>
<h3>GeeksforGeeks</h3>
<p>The quick brown fox jumps over the lazy dog<br></p>
<!-- Inside blockquotes -->
<blockquote>
<p>The quick brown fox jumps over the lazy dog
</p>
</blockquote>
<!-- Inside quotes -->
<q>The quick brown fox jumps over the lazy dog</q>
<!-- Cite with title -->
<p>
The <cite>GeeksforGeeks</cite>
is the best site to<br>
search for articles and practice problems.
</p>
</body>
</html>
Output:
HTML Colors
HTML Colors can be applied to text, backgrounds, borders,
links, forms, tables, etc. This article provides an in-depth look
at how colors can be applied to various elements such as text,
backgrounds, borders, links, forms, and tables in HTML. We
will explore different color formats including hexadecimal,
RGB, RGBA, HSL, and named colors, offering you precise
control over the color presentation on your web pages.
HTML Colors Name
HTML color names offer a user-friendly way to specify colors.
From classic colors like Red, Green, Blue, Pink, Purple, Sky
Blue, Gray, and Orange, to more exotic shades, HTML provides
a wide palette for web designers. Whether you’re designing a
serene theme or a vibrant layout, HTML color names have got
you covered.
Red
Green
Blue
Pink
Purple
Sky Blue
Gray
Orange
HTML Link Color specifies the color of the <a href="#" style="color:
anchor tag within a webpage, allowing us to blue;">
Link Color
define the color of clickable text, and making Link has a blue color
user navigation more visual. </a>
HTML Color
HTML Colors Example Explanation:
In this example we create HTML to structure content with
elements like headings, divs, links, and paragraphs.
Heading displays a gray background for emphasis.
Div is bordered with skyblue color for visual distinction.
Link text appears in tomato color for better visibility.
Paragraph text is styled with a dark green color for
readability.
Color Values
Color values in HTML define the color of elements. They can
be specified using various formats such as hexadecimal, RGB,
RGBA, HSL, HSLA, color names, and system color keywords.
RGB Color Value
RGB, which stands for Red, Green, and Blue, is a method used
in CSS to describe colors. It works by mixing different amounts
of three primary colors, each with values ranging from 0 to
255. By adjusting these values, we can produce an extensive
range of colors, allowing for the creation of diverse and better
color palettes across websites.
RGB Color Value Properties
Some properties are:
It's representation is as rgb(red, green, blue).
By adjusting these values from 0 to 255, we can produce
16,777,216 unique colors.
For instance, specifying rgb(0, 255, 0) results in green
because the green value is at its maximum (255), while red
and blue are at 0. Conversely, using rgb(0, 0, 255) produces
blue, with the blue channel set to its peak (255), and red
and green at 0.
To render black, all color parameters are set to 0 (rgb(0, 0,
0)), and for white, all parameters are set to their maximum
(rgb(255, 255, 255)).
RGB Color Value Syntax
// Blue background
<p style="background-color: rgb(0, 0, 255);">
Is the sky background Blue by using RGB
</p>
RGB Color Value Example
The example we are using the RGB color values to style our
html elements.
<!DOCTYPE html>
<html>
<head>
<title>RGB Color Value</title>
<style>
center {
width: 50%;
margin: 0 auto;
}
h2,
div,
p,
span {
padding: 10px;
margin-bottom: 20px;
}
</style>
</head>
<body>
<center>
<h2 style="background-color: rgb(109, 102, 197);">
Heading with blue Background color
</h2>
<div style="border: 2px solid rgb(135, 206, 235);">
<li>
<a href="#html-color-usage">
HTML Color Usage
</a>
</li>
Div with Skyblue Border color
</div>
<span>
<a href="#" style="color: rgb(241, 76, 89);">
Link has a tomato color
</a>
</span>
<p style="color: rgb(0, 100, 0);">
Paragraph with Dark Green Text color
</p>
</center>
</body>
</html>
Output:
1
<!DOCTYPE html>
2
<html>
3
4
<head>
5
<title>HTML Links</title>
6
</head>
7
8
<body>
9
<p>Click on the following link</p>
10
<a href="https://www.geeksforgeeks.org">
11
GeeksforGeeks
12
</a>
13
</body>
14
15
</html>
Output:
By default, links will appear as follows in all browsers:
An unvisited link is underlined and blue.
A visited link is underlined and purple.
An active link is underlined and red.
_self
Opens the linked document in the same frame or window as the link. (Default
behavior)
_top Opens the linked document in the full body of the window.
Attribute Description
framename
Opens the linked document in a specified frame. The frame’s name is specified in
the attribute.
1
<!DOCTYPE html>
2
<html>
3
4
<head>
5
<title>Target Attribute Example</title>
6
</head>
7
8
<body>
9
<h3>
10
Various options available in the
11
Target Attribute
12
</h3>
13
14
<p>
15
If you set the target attribute to
16
"_blank", the link will open in a new
17
browser window or tab.
18
</p>
19
<a href="https://www.geeksforgeeks.org"
20
target="_blank">
21
GeeksforGeeks
22
</a>
23
24
<p>
25
If you set the target attribute to
26
"_self", the link will open in the
27
same window or tab.
28
</p>
29
<a href="https://www.geeksforgeeks.org"
30
target="_self">
31
GeeksforGeeks
32
</a>
33
34
<p>
35
If you set the target attribute to
36
"_top", the link will open in the full
37
body of the window.
38
</p>
39
<a href="https://www.geeksforgeeks.org"
40
target="_top">
41
GeeksforGeeks
42
</a>
43
44
<p>
45
If you set the target attribute to
46
"_parent", the link will open in the
47
parent frame.
48
</p>
49
<a href="https://www.geeksforgeeks.org"
50
target="_parent">
51
GeeksforGeeks
52
</a>
53
</body>
54
55
</html>
Output:
Linking Different HTML Elements
Below are examples of how to link different HTML elements
with their respective code snippets
Element to
Interlink Specific Code
HTML Favicon
Last Updated : 19 Dec, 2024
1
<!DOCTYPE html>
2
<html>
3
4
<head>
5
<meta charset="utf-8" />
6
<title>HTML Favicon</title>
7
8
<!-- Add icon link -->
9
<link rel="icon" href="https://media.geeksforgeeks.org/wp-
content/cdn-uploads/gfg_200X200.png" type="image/x-icon">
10
</head>
11
12
<body>
13
<h3 style="color:green;">
14
GeeksforGeeks
15
</h3>
16
<p>Welcome to my website</p>
17
</body>
18
19
</html>
Output:
In this example:
rel=”icon”: Indicates that the linked resource is an icon for
the document. This relationship is essential for browsers to
understand that the specified file is meant to be used as
the website’s favicon.
href=”https://media.geeksforgeeks.org/wp-content/
cdn-uploads/gfg_200X200.png”: Specifies the path to
the favicon image. This can be a URL (as used here)
pointing to an image that serves as the icon.
type=”image/x-icon”: Specifies the MIME type of the
favicon file. While commonly image/x-icon is used for .ico
files, modern browsers support PNG and other formats as
well.
Note: Major browsers are not supported by the sizing
property of the favicon.
Edge
Chrome Supports multiple sizes
Larger file size compared
ICO Firefox in a single file; widely
to others
Opera supported
Safari
High-quality image,
Does not support multiple
PNG All Five supports transparency,
sizes in one file
smaller file size
HTML Favicon
Get 90% Course fee refund on completing 90% course
in 90 days! Take the Three 90 Challenge today.
5
<body>
6
<table>
7
<tr>
8
<th>Firstname</th>
9
<th>Lastname</th>
10
<th>Age</th>
11
</tr>
12
<tr>
13
<td>Priya</td>
14
<td>Sharma</td>
15
<td>24</td>
16
</tr>
17
<tr>
18
<td>Arun</td>
19
<td>Singh</td>
20
<td>32</td>
21
</tr>
22
<tr>
23
<td>Sam</td>
24
<td>Watson</td>
25
<td>41</td>
26
</tr>
27
</table>
28
</body>
29
30
</html>
Output:
In this example:
<table>: This tag starts the table. Everything between the
opening <table> and closing </table> tags makes up the
table.
<tr>: Stands for “table row”. Each <tr> tag defines a row
in the table.
<th>: Stands for “table header”. It’s used for the headers
of the columns. In this case, “Firstname“, “Lastname“,
and “Age” are headers. Text in <th> tags is usually bold
and centered by default.
<td>: Stands for “table data”. This tag is used for actual
data cells under each column. For instance, “Priya” is the
data under the “Firstname” header, “Sharma” under the
“Lastname“, and “24” under the “Age“.
The first <tr> has three <th> elements, setting up the
column titles.
The subsequent <tr> tags each contain three <td>
elements, representing the data for each person listed in
the table.
When this HTML code is rendered in a web browser, it will
display a table with four rows (one header row plus three data
rows) and three columns (Firstname, Lastname, Age), showing
the names and ages of Priya, Arun, and Sam.
Tags used in HTML Tables
HTML Tags Descriptions
1
<!-- index.html -->
2
<!DOCTYPE html>
3
<html>
4
5
<body>
6
<table>
7
<tr>
8
<th>Book Name</th>
9
<th>Author Name</th>
10
<th>Genre</th>
11
</tr>
12
<tr>
13
<td>The Book Thief</td>
14
<td>Markus Zusak</td>
15
<td>Historical Fiction</td>
16
</tr>
17
<tr>
18
<td>The Cruel Prince</td>
19
<td>Holly Black</td>
20
<td>Fantasy</td>
21
</tr>
22
<tr>
23
<td>The Silent Patient</td>
24
<td> Alex Michaelides</td>
25
<td>Psychological Fiction</td>
26
</tr>
27
</table>
28
</body>
29
30
</html>
Output:
HTML Table
1
<!-- index.html -->
2
<!DOCTYPE html>
3
<html>
4
5
<head>
6
<style>
7
table,
8
th,
9
td {
10
border: 1px solid black;
11
}
12
</style>
13
</head>
14
15
<body>
16
<table style="width:100%">
17
<tr>
18
<th>Firstname</th>
19
<th>Lastname</th>
20
<th>Age</th>
21
</tr>
22
<tr>
23
<td>Priya</td>
24
<td>Sharma</td>
25
<td>24</td>
26
</tr>
27
<tr>
28
<td>Arun</td>
29
<td>Singh</td>
30
<td>32</td>
31
</tr>
32
<tr>
33
<td>Sam</td>
34
<td>Watson</td>
35
<td>41</td>
36
</tr>
37
</table>
38
</body>
39
40
</html>
Output:
1
<!-- index.html -->
2
<!DOCTYPE html>
3
<html>
4
5
<head>
6
<style>
7
table,
8
th,
9
td {
10
border: 1px solid black;
11
border-collapse: collapse;
12
}
13
</style>
14
</head>
15
16
<body>
17
<table style="width:100%">
18
<tr>
19
<th>Firstname</th>
20
<th>Lastname</th>
21
<th>Age</th>
22
</tr>
23
<tr>
24
<td>Priya</td>
25
<td>Sharma</td>
26
<td>24</td>
27
</tr>
28
<tr>
29
<td>Arun</td>
30
<td>Singh</td>
31
<td>32</td>
32
</tr>
33
<tr>
34
<td>Sam</td>
35
<td>Watson</td>
36
<td>41</td>
37
</tr>
38
</table>
39
</body>
40
41
</html>
Output:
5
<head>
6
<style>
7
table,
8
th,
9
td {
10
border: 1px solid black;
11
border-collapse: collapse;
12
}
13
14
th,
15
td {
16
padding: 20px;
17
}
18
</style>
19
</head>
20
21
<body>
22
<table style="width:100%">
23
<tr>
24
<th>Firstname</th>
25
<th>Lastname</th>
26
<th>Age</th>
27
</tr>
28
<tr>
29
<td>Priya</td>
30
<td>Sharma</td>
31
<td>24</td>
32
</tr>
33
<tr>
34
<td>Arun</td>
35
<td>Singh</td>
36
<td>32</td>
37
</tr>
38
<tr>
39
<td>Sam</td>
40
<td>Watson</td>
41
<td>41</td>
42
</tr>
43
</table>
44
</body>
45
46
</html>
Output:
1
<!-- index.html -->
2
<!DOCTYPE html>
3
<html>
4
5
<head>
6
<style>
7
table,
8
th,
9
td {
10
border: 1px solid black;
11
border-collapse: collapse;
12
}
13
14
th,
15
td {
16
padding: 20px;
17
}
18
19
th {
20
text-align: left;
21
}
22
</style>
23
</head>
24
25
<body>
26
<table style="width:100%">
27
<tr>
28
<th>Firstname</th>
29
<th>Lastname</th>
30
<th>Age</th>
31
</tr>
32
<tr>
33
<td>Priya</td>
34
<td>Sharma</td>
35
<td>24</td>
36
</tr>
37
<tr>
38
<td>Arun</td>
39
<td>Singh</td>
40
<td>32</td>
41
</tr>
42
<tr>
43
<td>Sam</td>
44
<td>Watson</td>
45
<td>41</td>
46
</tr>
47
</table>
48
</body>
49
50
</html>
Output:
text-align Property
1
<!-- index.html -->
2
<!DOCTYPE html>
3
<html>
4
5
<head>
6
<style>
7
table,
8
th,
9
td {
10
border: 1px solid black;
11
}
12
13
table {
14
border-spacing: 5px;
15
}
16
</style>
17
</head>
18
19
<body>
20
<table style="width:100%">
21
<tr>
22
<th>Firstname</th>
23
<th>Lastname</th>
24
<th>Age</th>
25
</tr>
26
<tr>
27
<td>Priya</td>
28
<td>Sharma</td>
29
<td>24</td>
30
</tr>
31
<tr>
32
<td>Arun</td>
33
<td>Singh</td>
34
<td>32</td>
35
</tr>
36
<tr>
37
<td>Sam</td>
38
<td>Watson</td>
39
<td>41</td>
40
</tr>
41
</table>
42
</body>
43
44
</html>
Output:
1
<!-- index.html -->
2
<!DOCTYPE html>
3
<html>
4
5
<head>
6
<style>
7
table,
8
th,
9
td {
10
border: 1px solid black;
11
border-collapse: collapse;
12
}
13
14
th,
15
td {
16
padding: 5px;
17
text-align: left;
18
}
19
</style>
20
</head>
21
22
<body>
23
<h2>Cell that spans two columns:</h2>
24
<table style="width:100%">
25
<tr>
26
<th>Name</th>
27
<th colspan="2">Telephone</th>
28
</tr>
29
<tr>
30
<td>Vikas Rawat</td>
31
<td>9125577854</td>
32
<td>8565557785</td>
33
</tr>
34
</table>
35
</body>
36
37
</html>
Output:
colspan attribute
1
<!-- index.html -->
2
<!DOCTYPE html>
3
<html>
4
5
<head>
6
<style>
7
table,
8
th,
9
td {
10
border: 1px solid black;
11
border-collapse: collapse;
12
}
13
14
th,
15
td {
16
padding: 5px;
17
text-align: left;
18
}
19
</style>
20
</head>
21
22
<body>
23
<h2>Cell that spans two rows:</h2>
24
<table style="width:100%">
25
<tr>
26
<th>Name:</th>
27
<td>Vikas Rawat</td>
28
</tr>
29
<tr>
30
<th rowspan="2">Telephone:</th>
31
<td>9125577854</td>
32
</tr>
33
<tr>
34
<td>8565557785</td>
35
</tr>
36
</table>
37
</body>
38
39
</html>
Output:
1
<!-- index.html -->
2
<!DOCTYPE html>
3
<html>
4
5
<head>
6
<style>
7
table,
8
th,
9
td {
10
border: 1px solid black;
11
border-collapse: collapse;
12
}
13
14
th,
15
td {
16
padding: 20px;
17
}
18
19
th {
20
text-align: left;
21
}
22
</style>
23
</head>
24
25
<body>
26
<table style="width:100%">
27
<caption>DETAILS</caption>
28
<tr>
29
<th>Firstname</th>
30
<th>Lastname</th>
31
<th>Age</th>
32
</tr>
33
<tr>
34
<td>Priya</td>
35
<td>Sharma</td>
36
<td>24</td>
37
</tr>
38
<tr>
39
<td>Arun</td>
40
<td>Singh</td>
41
<td>32</td>
42
</tr>
43
<tr>
44
<td>Sam</td>
45
<td>Watson</td>
46
<td>41</td>
47
</tr>
48
</table>
49
</body>
50
51
</html>
Output:
Adding the caption using the tag
1
<!-- index.html -->
2
<!DOCTYPE html>
3
<html>
4
5
<head>
6
<style>
7
table,
8
th,
9
td {
10
border: 1px solid black;
11
border-collapse: collapse;
12
}
13
14
th,
15
td {
16
padding: 5px;
17
text-align: left;
18
}
19
20
table#t01 {
21
width: 100%;
22
background-color: #f2f2d1;
23
}
24
</style>
25
</head>
26
27
<body>
28
<table style="width:100%">
29
<tr>
30
<th>Firstname</th>
31
<th>Lastname</th>
32
<th>Age</th>
33
</tr>
34
<tr>
35
<td>Priya</td>
36
<td>Sharma</td>
37
<td>24</td>
38
</tr>
39
<tr>
40
<td>Arun</td>
41
<td>Singh</td>
42
<td>32</td>
43
</tr>
44
<tr>
45
<td>Sam</td>
46
<td>Watson</td>
47
<td>41</td>
48
</tr>
49
</table>
50
<br />
51
<br />
52
<table id="t01">
53
<tr>
54
<th>Firstname</th>
55
<th>Lastname</th>
56
<th>Age</th>
57
</tr>
58
<tr>
59
<td>Priya</td>
60
<td>Sharma</td>
61
<td>24</td>
62
</tr>
63
<tr>
64
<td>Arun</td>
65
<td>Singh</td>
66
<td>32</td>
67
</tr>
68
<tr>
69
<td>Sam</td>
70
<td>Watson</td>
71
<td>41</td>
72
</tr>
73
</table>
74
</body>
75
76
</html>
Output:
1
<!-- index.html -->
2
<!DOCTYPE html>
3
<html>
4
5
<body>
6
<table border=5 bordercolor=black>
7
<tr>
8
<td> First Column of Outer Table </td>
9
<td>
10
<table border=5 bordercolor=grey>
11
<tr>
12
<td> First row of Inner Table </td>
13
</tr>
14
<tr>
15
<td> Second row of Inner Table </td>
16
</tr>
17
</table>
18
</td>
19
</tr>
20
</table>
21
</body>
22
23
</html>
Output:
Supported Browsers
Google Chrome 15
Edge 12
Firefox 1
Opera 14
Safari 6
—> HTML is the foundation of webpages, is used for webpage
development by structuring websites and web apps. You can
learn HTML from the ground up by following this HTML
Tutorial and HTML Examples.
HTML Tables – FAQs
How do I create a table in HTML?
Use the <table> tag with nested <tr> (table row) and <td>
(table data) tags to create rows and cells.
What are HTML tables used for?
HTML tables organize and display data in a structured format,
useful for comparing information like schedules, reports, and
product listings.
What are the tables in HTML5?
HTML5 tables use traditional tags: <table>, <tr>, <td>,
<th>, <thead>, <tbody>, and <tfoot>, with enhanced
attributes and CSS support for better styling and
responsiveness.
What is the table data tag in HTML?
The <td> tag defines a cell within a table that contains data.
HTML Tables
Last Updated : 22 Nov, 2024
1
<!-- index.html -->
2
<!DOCTYPE html>
3
<html>
4
5
<body>
6
<table>
7
<tr>
8
<th>Firstname</th>
9
<th>Lastname</th>
10
<th>Age</th>
11
</tr>
12
<tr>
13
<td>Priya</td>
14
<td>Sharma</td>
15
<td>24</td>
16
</tr>
17
<tr>
18
<td>Arun</td>
19
<td>Singh</td>
20
<td>32</td>
21
</tr>
22
<tr>
23
<td>Sam</td>
24
<td>Watson</td>
25
<td>41</td>
26
</tr>
27
</table>
28
</body>
29
30
</html>
Output:
In this example:
<table>: This tag starts the table. Everything between the
opening <table> and closing </table> tags makes up the
table.
<tr>: Stands for “table row”. Each <tr> tag defines a row
in the table.
<th>: Stands for “table header”. It’s used for the headers
of the columns. In this case, “Firstname“, “Lastname“,
and “Age” are headers. Text in <th> tags is usually bold
and centered by default.
<td>: Stands for “table data”. This tag is used for actual
data cells under each column. For instance, “Priya” is the
data under the “Firstname” header, “Sharma” under the
“Lastname“, and “24” under the “Age“.
The first <tr> has three <th> elements, setting up the
column titles.
The subsequent <tr> tags each contain three <td>
elements, representing the data for each person listed in
the table.
When this HTML code is rendered in a web browser, it will
display a table with four rows (one header row plus three data
rows) and three columns (Firstname, Lastname, Age), showing
the names and ages of Priya, Arun, and Sam.
Tags used in HTML Tables
HTML Tags Descriptions
1
<!-- index.html -->
2
<!DOCTYPE html>
3
<html>
4
5
<body>
6
<table>
7
<tr>
8
<th>Book Name</th>
9
<th>Author Name</th>
10
<th>Genre</th>
11
</tr>
12
<tr>
13
<td>The Book Thief</td>
14
<td>Markus Zusak</td>
15
<td>Historical Fiction</td>
16
</tr>
17
<tr>
18
<td>The Cruel Prince</td>
19
<td>Holly Black</td>
20
<td>Fantasy</td>
21
</tr>
22
<tr>
23
<td>The Silent Patient</td>
24
<td> Alex Michaelides</td>
25
<td>Psychological Fiction</td>
26
</tr>
27
</table>
28
</body>
29
30
</html>
Output:
HTML Table
1
<!-- index.html -->
2
<!DOCTYPE html>
3
<html>
4
5
<head>
6
<style>
7
table,
8
th,
9
td {
10
border: 1px solid black;
11
}
12
</style>
13
</head>
14
15
<body>
16
<table style="width:100%">
17
<tr>
18
<th>Firstname</th>
19
<th>Lastname</th>
20
<th>Age</th>
21
</tr>
22
<tr>
23
<td>Priya</td>
24
<td>Sharma</td>
25
<td>24</td>
26
</tr>
27
<tr>
28
<td>Arun</td>
29
<td>Singh</td>
30
<td>32</td>
31
</tr>
32
<tr>
33
<td>Sam</td>
34
<td>Watson</td>
35
<td>41</td>
36
</tr>
37
</table>
38
</body>
39
40
</html>
Output:
5
<head>
6
<style>
7
table,
8
th,
9
td {
10
border: 1px solid black;
11
border-collapse: collapse;
12
}
13
</style>
14
</head>
15
16
<body>
17
<table style="width:100%">
18
<tr>
19
<th>Firstname</th>
20
<th>Lastname</th>
21
<th>Age</th>
22
</tr>
23
<tr>
24
<td>Priya</td>
25
<td>Sharma</td>
26
<td>24</td>
27
</tr>
28
<tr>
29
<td>Arun</td>
30
<td>Singh</td>
31
<td>32</td>
32
</tr>
33
<tr>
34
<td>Sam</td>
35
<td>Watson</td>
36
<td>41</td>
37
</tr>
38
</table>
39
</body>
40
41
</html>
Output:
HTML Table with Collapsed Borders
1
<!-- index.html -->
2
<!DOCTYPE html>
3
<html>
4
5
<head>
6
<style>
7
table,
8
th,
9
td {
10
border: 1px solid black;
11
border-collapse: collapse;
12
}
13
14
th,
15
td {
16
padding: 20px;
17
}
18
</style>
19
</head>
20
21
<body>
22
<table style="width:100%">
23
<tr>
24
<th>Firstname</th>
25
<th>Lastname</th>
26
<th>Age</th>
27
</tr>
28
<tr>
29
<td>Priya</td>
30
<td>Sharma</td>
31
<td>24</td>
32
</tr>
33
<tr>
34
<td>Arun</td>
35
<td>Singh</td>
36
<td>32</td>
37
</tr>
38
<tr>
39
<td>Sam</td>
40
<td>Watson</td>
41
<td>41</td>
42
</tr>
43
</table>
44
</body>
45
46
</html>
Output:
1
<!-- index.html -->
2
<!DOCTYPE html>
3
<html>
4
5
<head>
6
<style>
7
table,
8
th,
9
td {
10
border: 1px solid black;
11
border-collapse: collapse;
12
}
13
14
th,
15
td {
16
padding: 20px;
17
}
18
19
th {
20
text-align: left;
21
}
22
</style>
23
</head>
24
25
<body>
26
<table style="width:100%">
27
<tr>
28
<th>Firstname</th>
29
<th>Lastname</th>
30
<th>Age</th>
31
</tr>
32
<tr>
33
<td>Priya</td>
34
<td>Sharma</td>
35
<td>24</td>
36
</tr>
37
<tr>
38
<td>Arun</td>
39
<td>Singh</td>
40
<td>32</td>
41
</tr>
42
<tr>
43
<td>Sam</td>
44
<td>Watson</td>
45
<td>41</td>
46
</tr>
47
</table>
48
</body>
49
50
</html>
Output:
text-align Property
1
<!-- index.html -->
2
<!DOCTYPE html>
3
<html>
4
5
<head>
6
<style>
7
table,
8
th,
9
td {
10
border: 1px solid black;
11
}
12
13
table {
14
border-spacing: 5px;
15
}
16
</style>
17
</head>
18
19
<body>
20
<table style="width:100%">
21
<tr>
22
<th>Firstname</th>
23
<th>Lastname</th>
24
<th>Age</th>
25
</tr>
26
<tr>
27
<td>Priya</td>
28
<td>Sharma</td>
29
<td>24</td>
30
</tr>
31
<tr>
32
<td>Arun</td>
33
<td>Singh</td>
34
<td>32</td>
35
</tr>
36
<tr>
37
<td>Sam</td>
38
<td>Watson</td>
39
<td>41</td>
40
</tr>
41
</table>
42
</body>
43
44
</html>
Output:
1
<!-- index.html -->
2
<!DOCTYPE html>
3
<html>
4
5
<head>
6
<style>
7
table,
8
th,
9
td {
10
border: 1px solid black;
11
border-collapse: collapse;
12
}
13
14
th,
15
td {
16
padding: 5px;
17
text-align: left;
18
}
19
</style>
20
</head>
21
22
<body>
23
<h2>Cell that spans two columns:</h2>
24
<table style="width:100%">
25
<tr>
26
<th>Name</th>
27
<th colspan="2">Telephone</th>
28
</tr>
29
<tr>
30
<td>Vikas Rawat</td>
31
<td>9125577854</td>
32
<td>8565557785</td>
33
</tr>
34
</table>
35
</body>
36
37
</html>
Output:
colspan attribute
1
<!-- index.html -->
2
<!DOCTYPE html>
3
<html>
4
5
<head>
6
<style>
7
table,
8
th,
9
td {
10
border: 1px solid black;
11
border-collapse: collapse;
12
}
13
14
th,
15
td {
16
padding: 5px;
17
text-align: left;
18
}
19
</style>
20
</head>
21
22
<body>
23
<h2>Cell that spans two rows:</h2>
24
<table style="width:100%">
25
<tr>
26
<th>Name:</th>
27
<td>Vikas Rawat</td>
28
</tr>
29
<tr>
30
<th rowspan="2">Telephone:</th>
31
<td>9125577854</td>
32
</tr>
33
<tr>
34
<td>8565557785</td>
35
</tr>
36
</table>
37
</body>
38
39
</html>
Output:
5
<head>
6
<style>
7
table,
8
th,
9
td {
10
border: 1px solid black;
11
border-collapse: collapse;
12
}
13
14
th,
15
td {
16
padding: 20px;
17
}
18
19
th {
20
text-align: left;
21
}
22
</style>
23
</head>
24
25
<body>
26
<table style="width:100%">
27
<caption>DETAILS</caption>
28
<tr>
29
<th>Firstname</th>
30
<th>Lastname</th>
31
<th>Age</th>
32
</tr>
33
<tr>
34
<td>Priya</td>
35
<td>Sharma</td>
36
<td>24</td>
37
</tr>
38
<tr>
39
<td>Arun</td>
40
<td>Singh</td>
41
<td>32</td>
42
</tr>
43
<tr>
44
<td>Sam</td>
45
<td>Watson</td>
46
<td>41</td>
47
</tr>
48
</table>
49
</body>
50
51
</html>
Output:
5
<head>
6
<style>
7
table,
8
th,
9
td {
10
border: 1px solid black;
11
border-collapse: collapse;
12
}
13
14
th,
15
td {
16
padding: 5px;
17
text-align: left;
18
}
19
20
table#t01 {
21
width: 100%;
22
background-color: #f2f2d1;
23
}
24
</style>
25
</head>
26
27
<body>
28
<table style="width:100%">
29
<tr>
30
<th>Firstname</th>
31
<th>Lastname</th>
32
<th>Age</th>
33
</tr>
34
<tr>
35
<td>Priya</td>
36
<td>Sharma</td>
37
<td>24</td>
38
</tr>
39
<tr>
40
<td>Arun</td>
41
<td>Singh</td>
42
<td>32</td>
43
</tr>
44
<tr>
45
<td>Sam</td>
46
<td>Watson</td>
47
<td>41</td>
48
</tr>
49
</table>
50
<br />
51
<br />
52
<table id="t01">
53
<tr>
54
<th>Firstname</th>
55
<th>Lastname</th>
56
<th>Age</th>
57
</tr>
58
<tr>
59
<td>Priya</td>
60
<td>Sharma</td>
61
<td>24</td>
62
</tr>
63
<tr>
64
<td>Arun</td>
65
<td>Singh</td>
66
<td>32</td>
67
</tr>
68
<tr>
69
<td>Sam</td>
70
<td>Watson</td>
71
<td>41</td>
72
</tr>
73
</table>
74
</body>
75
76
</html>
Output:
5
<body>
6
<table border=5 bordercolor=black>
7
<tr>
8
<td> First Column of Outer Table </td>
9
<td>
10
<table border=5 bordercolor=grey>
11
<tr>
12
<td> First row of Inner Table </td>
13
</tr>
14
<tr>
15
<td> Second row of Inner Table </td>
16
</tr>
17
</table>
18
</td>
19
</tr>
20
</table>
21
</body>
22
23
</html>
Output:
Supported Browsers
Google Chrome 15
Edge 12
Firefox 1
Opera 14
Safari 6
—> HTML is the foundation of webpages, is used for webpage
development by structuring websites and web apps. You can
learn HTML from the ground up by following this HTML
Tutorial and HTML Examples.
HTML Tables – FAQs
How do I create a table in HTML?
Use the <table> tag with nested <tr> (table row) and <td>
(table data) tags to create rows and cells.
What are HTML tables used for?
HTML tables organize and display data in a structured format,
useful for comparing information like schedules, reports, and
product listings.
What are the tables in HTML5?
HTML5 tables use traditional tags: <table>, <tr>, <td>,
<th>, <thead>, <tbody>, and <tfoot>, with enhanced
attributes and CSS support for better styling and
responsiveness.
What is the table data tag in HTML?
The <td> tag defines a cell within a table that contains data.
Why is the table tag used?
The <table> tag organizes data into rows and columns,
presenting structured information clearly and efficiently.
Comment
More info
Next Article
HTML Lists
Similar Reads
CSS Syntax
CSS (Cascading Style Sheets) is a stylesheet language used to describe
the presentation of a document written in HTML. Understanding CSS
syntax is fundamental for creating visually appealing and well-
structured web pages.
Basic CSS Syntax
CSS is written as rulesets. A ruleset consists of a selector and a declaration
block. Here’s the basic structure
<html>
<head>
<style>
/* CSS Rule */
h1 {
color: blue;
/* Property: value */
font-size: 24px;
}
p{
color: green;
font-size: 16px;
}
</style>
</head>
<body>
<h1>Hello, World!</h1>
<p>This is a simple paragraph.</p>
</body>
</html>
h1: This selector targets all <h1> elements on the page. The style
applied to <h1> will set the text color to blue and the font size to 24px.
p: This selector targets all <p> elements. The text color will be green
and the font size will be 16px.
CSS Syntax Breakdown
1. Selectors: Selectors are used to “select” the HTML element you want
to style. It can be an element type (e.g., h1), a class (e.g., .class-name),
an ID (e.g., #id-name), or a combination of these.
1. Type Selector: Targets all elements of a specific type, like h1, p,
div, etc.
2. Class Selector: Targets elements with a specific class.
Example: .my-class { }
3. ID Selector: Targets an element with a specific ID. Example: #my-
id { }
4. Universal Selector: Targets all elements (*).
2. Properties: Properties are the aspects of the selected elements you
want to style (like color, width, height, etc.).
1. color: Defines the text color.
2. background-color: Defines the background color of an element.
3. font-size: Sets the size of the font.
4. margin: Specifies the space around an element.
5. padding: Defines the space between the element’s content and its
border.
3. Values: Values define the specifics of the property you want to apply,
such as a color name, a number (e.g., 16px), or percentages (e.g., 50%).
Selectors in CSS
Selectors define which HTML elements are styled. CSS offers several
types of selectors.
Universal Selector: Applies styles to all elements.
*{
margin: 0;
padding: 0;
}
Type Selector: Targets specific HTML elements.
h1 {
font-family: Arial, sans-serif;
}
Class Selector: Styles elements with a specific class attribute.
.box {
border: 1px solid black;
padding: 10px;
}
ID Selector: Targets a single element with a specific ID.
#header {
background-color: lightgray;
}
CSS Selectors
<html>
<head>
<style>
h1 {
color: red;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
</body>
</html>
External CSS: Linked through an external stylesheet .
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
</html>
Media Queries
Media queries allow you to create responsive designs .
@media (max-width: 600px) {
body {
background-color: lightblue;
}
}
This changes the background color for screens narrower than 600 pixels.
Animations
CSS can be used to create animations for elements.
@keyframes move {
from {
left: 0px;
}
to {
left: 100px;
}
}
div {
position: relative;
animation: move 2s infinite;
}
In case of animation syntax the time can also be divided into time frames
in form of percentage’s.
CSS Variables
CSS variables helps to crate reusable values for any property.
:root {
--main-color: #3498db;
}
h1 {
color: var(--main-color);
}
Comments in CSS
Comments are used to explain code and are ignored by the browser.
/* Write your comment comment */
p{
font-size: 16px;
}
Comments are always written between /* these symbols */.
SASS with $ and & for Nesting
SASS allows the use of variables, prefixed with $, to store reusable values
such as colors, fonts, or sizes. This makes it easy to maintain and update
styles consistently across a project. Key Feature: Using Variables ,Here’s
an example demonstrating how to use variables in SASS Indented Syntax.
SASSCSS
$primary-color: #3498db;
$secondary-color: #2ecc71;
$padding: 15px;
button {
background-color: $primary-color;
color: white;
padding: $padding;
border: none;
border-radius: 5px;
cursor: pointer;
&:hover {
background-color: $secondary-color;
}
}
Variables(@)
$primary-color: Stores the main color for the button.
$secondary-color: Defines the hover state color.
$padding: Sets consistent spacing around the button.
Variables improve maintainability—changing $primary-color updates
all instances of it.
&:hover(& operator): The & operator appends the :hover pseudo-
class to the parent selector (button), ensuring the styles are tied to the
specific button element.
This avoids repetition by preventing the need to retype the
parent selector (button: hover).
Advantages of Combining $ and & operator
Using $ variables ensures consistent styling across the project.
The & operator streamlines nested rules, keeping the code DRY
(Don’t Repeat Yourself) and organized.
LESS @ and & for Nesting
LESS is another popular CSS preprocessor that allows for the use of
variables (prefixed with @) and the & operator for nesting and
referencing parent selectors. This syntax makes it easy to manage styles
dynamically and keep your code clean and maintainable.
LESSCSS
@primary-color: #3498db;
@secondary-color: #2ecc71;
@padding: 15px;
button {
background-color: @primary-color;
color: white;
padding: @padding;
border: none;
border-radius: 5px;
cursor: pointer;
&:hover {
background-color: @secondary-color;
}
}
Variables (@)
@primary-color: Stores the main color for the button.
@secondary-color: Defines the hover color for the button.
@padding: Specifies the padding value for the button.
&:hover: The & operator references the parent selector (button) and
appends the :hover pseudo-class. This ensures that the hover styles are
scoped to the button element, without the need to repeat the full
selector (button :hover).
This makes the code more concise and keeps it DRY (Don’t Repeat
Yourself).
Advantages of Combining @ and & operator
Variables (@) improve code consistency and reduce errors by
centralizing values like colors and spacing.
The & operator helps you handle pseudo-classes and other states
(like :hover) within the context of the parent selector, keeping your
code organized and easy to manage.
Using LESS with these features allows for better flexibility and
readability in your CSS, making your stylesheets cleaner and easier to
maintain.