HTML Theory
HTML Theory
<!DOCTYPE html>
<html>
<body>
<blockquote cite="http://www.worldwildlife.org/who/index.html">
For 60 years, WWF has worked to help people and nature thrive.
</blockquote>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<blockquote cite="http://www.worldwildlife.org/who/index.html">
For 60 years, WWF has worked to help people and nature thrive.
</blockquote>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p>WWF's goal is to: <q>Build a future where people live in harmony with nature.</q></p>
</body>
</html>
<address>
Written by John Doe.<br>
Visit us at:<br>
Example.com<br>
Box 564, Disneyland<br>
USA
<!DOCTYPE html>
<html>
<body>
<p>Marking up abbreviations can give useful information to browsers, translation systems and
search-engines.</p>
</body>
</html>
Example
<p>This is a paragraph.</p>
Example
<p>This is a paragraph.</p>
Example
<p>This is a paragraph.</p>
<!--
<p>Look at this cool image:</p>
<img border="0" src="pic_trulli.jpg"
alt="Trulli">
-->
<p>This is a paragraph too.</p>
Example
Color Names
In HTML, a color can be specified by using a color
name:
Tomato
Orange
DodgerBlue
MediumSeaGreen
Gray
SlateBlue
Violet
Example
<h1 style="background-color:DodgerBlue;">Hello
World</h1>
<p style="background-color:Tomato;">Lorem
ipsum...</p>
Text Color
You can set the color of text:
Hello World
Border Color
You can set the color of borders:
Hello World
Hello World
Hello World
Example
Color Values
In HTML, colors can also be specified using RGB
values, HEX values, HSL values, RGBA values, and
HSLA values.
The following three <div> elements have their
background color set with RGB, HEX, and HSL
values:
#ff6347
Example
M a n i p u l a t e T e x t
C o l o r s , B o x e s
What is CSS?
Cascading Style Sheets (CSS) is used to format
the layout of a webpage.
With CSS, you can control the color, font, the size
of text, the spacing between elements, how
elements are positioned and laid out, what
background images or background colors are to be
used, different displays for different devices and
screen sizes, and much more!
Using CSS
CSS can be added to HTML documents in 3 ways:
Internal CSS
An internal CSS is used to define a style for a
single HTML page.
An internal CSS is defined in the <head> section of
an HTML page, within a <style> element.
The following example sets the text color of ALL
the <h1> elements (on that page) to blue, and the
text color of ALL the <p> elements to red. In
addition, the page will be displayed with a "<!
DOCTYPE html>
<html>
<head>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>powderblue" background color:
External CSS
An external style sheet is used to define the style
for many HTML pages.
To use an external style sheet, add a link to it in
the <head> section of each HTML page:
Example
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
The external style sheet can be written in any text
editor. The file must not contain any HTML code,
and must be saved with a .css extension.
Here is what the "styles.css" file looks like:
"styles.css":
body {
background-color: powderblue;
}
h1 {
color: blue;
}
p {
color: red;
}
Example
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: blue;
font-family: verdana;
font-size: 300%;
}
p {
color: red;
font-family: courier;
font-size: 160%;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Try it Yourself »
CSS Border
The CSS border property defines a border around
an HTML element.
Tip: You can define a border for nearly all HTML
elements.
Example
p {
border: 2px solid powderblue;
CSS Padding
The CSS padding property defines a padding
(space) between the text and the border.
Example
p {
border: 2px solid powderblue;
padding: 30px;
CSS Margin
The CSS margin property defines a margin (space)
outside the border.
Example
p {
border: 2px solid powderblue;
margin: 50px;
Example
Example
This example links to a style sheet located in the
html folder on the current web site:
<link rel="stylesheet" href="/html/styles.css">
HTML Links - Hyperlinks
HTML links are hyperlinks.
You can click on a link and jump to another
document.
When you move the mouse over a link, the mouse
arrow will turn into a little hand.
Example
This example shows how to create a link to
W3Schools.com:
<a href="https://www.w3schools.com/">Visit
W3Schools.com!</a>
By default, links will appear as follows in all
browsers:
Example
<h2>Absolute URLs</h2>
<p><a href="https://www.w3.org/">W3C</a></p>
<p><a href="https://www.google.com/">Google</a></
p>
<h2>Relative URLs</h2>
<p><a href="html_images.asp">HTML Images</a></p>
<p><a href="/css/default.asp">CSS
Tutorial</a></p>
<a href="default.asp">
<img src="smiley.gif" alt="HTML
tutorial" style="width:42px;height:42px;">
Example
<a href="mailto:[email protected]">Send
email</a>
Button as a Link
To use an HTML button as a link, you have to add
some JavaScript code.
JavaScript allows you to specify what happens at
certain events, such as a click of a button:
Example
<button onclick="document.location='default.asp'"
>HTML Tutorial</button>
Tip: Learn more about JavaScript in our JavaScript
Tutorial.
Link Titles
The title attribute specifies extra information
about an element. The information is most often
shown as a tooltip text when the mouse moves
over the element.
Example
Example
<a href="https://www.w3schools.com/html/
default.asp">HTML tutorial</a>
Example
Link to a page located in the html folder on the
current web site:
<a href="/html/default.asp">HTML tutorial</a>
Example
Link to a page located in the same folder as the
current page:
<a href="default.asp">HTML tutorial</a>
Example
Here, an unvisited link will be green with no
underline. A visited link will be pink with no
underline. An active link will be yellow and
underlined. In addition, when mousing over a link
(a:hover) it will become red and underlined:
<style>
a:link {
color: green;
background-color: transparent;
text-decoration: none;
}
a:visited {
color: pink;
background-color: transparent;
text-decoration: none;
}
a:hover {
color: red;
background-color: transparent;
text-decoration: underline;
}
a:active {
color: yellow;
background-color: transparent;
text-decoration: underline;
}
</style>
Link Buttons
A link can also be styled as a button, by using
CSS:
Example
<style>
a:link, a:visited {
background-color: #f44336;
color: white;
padding: 15px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
}
a:hover, a:active {
background-color: red;
}
</style>
Example
<a href="#C4You can also add a link to a bookmark
on another page:
<a href="html_demo.html#C4">Jump to Chapter 4</a>
">Jump to Chapter 4</a>
Chapter Summary
HTML Images
Example
<img src="pic_trulli.jpg" alt="Italian Trulli">
HTML Images Syntax
The HTML <img> tag is used to embed an image in
a web page.
Images are not technically inserted into a web
page; images are linked to web pages.
The <img> tag creates a holding space for the
referenced image.
The <img> tag is empty, it contains attributes only,
and does not have a closing tag.
The <img> tag has two required attributes:
Syntax
<img src="url" alt="alternatetext">
The src Attribute
The required src attribute specifies the path (URL)
to the image.
Note: When a web page loads, it is the browser, at
that moment, that gets the image from a web
server and inserts it into the page. Therefore,
make sure that the image actually stays in the
same spot in relation to the web page, otherwise
your visitors will get a broken link icon. The broken
link icon and the alt text are shown if the browser
cannot find the image.
Example
Example
<img src="img_chania.jpg" alt="Flowers in
Chania">
Example
Example
Example
Example
<!DOCTYPE html>
<html>
<head>
<style>
img {
width: 100%;
}
</style>
</head>
<body>
</body>
</html>
Example
<img src="https://www.w3schools.com/images/
w3schools_green.jpg" alt="W3Schools.com">
Animated Images
HTML allows animated GIFs:
Example
Image as a Link
To use an image as a link, put the <img> tag inside
the <a> tag:
Example
<a href="default.asp">
<img src="smiley.gif" alt="HTML
tutorial" style="width:42px;height:42px;">
</a>
Image Floating
Use the CSS float property to let the image float
to the right or to the left of a text:
Example
Image Maps
The HTML <map> tag defines an image map. An
image map is an image with clickable areas. The
areas are defined with one or more <area> tags.
Try to click on the computer, phone, or the cup of
coffee in the image below:
Example
<map name="workmap">
<area shape="rect" coords="34,44,270,350" alt="
Computer" href="computer.htm">
<area shape="rect" coords="290,172,333,250" alt
="Phone" href="phone.htm">
<area shape="circle" coords="337,300,44" alt="C
offee" href="coffee.htm">
</map>
How Does it Work?
The idea behind an image map is that you should
be able to perform different actions depending on
where in the image you click.
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:
<img src="workplace.jpg" alt="Workplace" usemap="
#workmap">
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 Areas
Then, add the clickable areas.
A clickable area is defined using
an <area> element.
Shape
You must define the shape of the clickable area,
and you can choose one of these values:
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:
Shape="circle"
To add a circle area, first locate the coordinates of
the center of the circle:
337,300
Then specify the radius of the circle:
44 pixels
Try it Yourself »
Shape="poly"
The shape="poly" contains several coordinate
points, which creates a shape formed with straight
lines (a polygon).
This can be used to create any shape.
Like maybe a croissant shape!
How can we make the croissant in the image below
become a clickable link?
Example
<map name="workmap">
<area shape="circle" coords="337,300,44" href="
coffee.htm" onclick="myFunction()">
</map>
<script>
function myFunction() {
alert("You clicked the coffee cup!");
}
</script>
Example
<p style="background-image:
url('img_girl.jpg');">
Example
<style>
p {
background-image: url('img_girl.jpg');
}
</style>
Try it Yourself »
<style>
body {
background-image: url('img_girl.jpg');
}
</style>
Try it Yourself »
Background Repeat
If the background image is smaller than the
element, the image will repeat itself, horizontally
and vertically, until it reaches the end of the
element:
Example
<style>
body {
background-image: url('example_img_girl.jpg');
}
</style>
Try it Yourself »
Example
<style>
body {
background-image: url('example_img_girl.jpg');
background-repeat: no-repeat;
}
</style>
Try it Yourself »
Background Cover
If you want the background image to cover the
entire element, you can set the background-
size property to cover.
Also, to make sure the entire element is always
covered, set the background-attachment property
to fixed:
This way, the background image will cover the
entire element, with no stretching (the image will
keep its original proportions):
Example
<style>
body {
background-image: url('img_girl.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
}
</style>
Try it Yourself »
Background Stretch
If you want the background image to stretch to fit
the entire element, you can set the background-
size property to 100% 100%:
Try resizing the browser window, and you will see
that the image will stretch, but always cover the
entire element.
Example
<style>
body {
background-image: url('img_girl.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
background-size: 100% 100%;
}
</style>
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Tutorial</title>
</head>
<body>
</body>
</html>
Example
<table>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
</table>
Try it Yourself »
Table Cells
Each table cell is defined by a <td> and
a </td> tag.
Example
<table>
<tr>
<td>Emil</td>
<td>Tobias</td>
<td>Linus</td>
</tr>
</table>
Note: A table cell can contain all sorts of HTML
elements: text, images, lists, links, other tables,
etc.
Example
table, th, td {
border: 1px solid black;
}
Try it Yourself »
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
Try it Yourself »
ADVERTISEMENT
Example
table, th, td {
border: 1px solid white;
border-collapse: collapse;
}
th, td {
background-color: #96D4D4;
}
Try it Yourself »
Round Table Borders
With the border-radius property, the borders get
rounded corners:
Example
table, th, td {
border: 1px solid black;
border-radius: 10px;
}
Try it Yourself »
Example
th, td {
border: 1px solid black;
border-radius: 10px;
}
Try it Yourself »
dotted
dashed
solid
double
groove
ridge
inset
outset
none
hidden
Example
th, td {
border-style: dotted;
}
Try it Yourself »
Border Color
With the border-color property, you can set the
color of the border.
Example
th, td {
border-color: #96D4D4;
}
Example
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
Try it Yourself »
Example
<table style="width:100%">
<tr>
<th style="width:70%">Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
Example
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr style="height:200px">
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
8:00
9:00
10:00
11:00
12:00
13:00
MO TU WE TH FR
N E D U I
8:00
9:00
10:0
0
11:0
0
12:0
0
DECEMBER
HTML Table Headers
Table headers are defined with th elements.
Each th element represents a table cell.
Example
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
Try it Yourself »
Example
<table>
<tr>
<th>Firstname</th>
<td>Jill</td>
<td>Eve</td>
</tr>
<tr>
<th>Lastname</th>
<td>Smith</td>
<td>Jackson</td>
</tr>
<tr>
<th>Age</th>
<td>94</td>
<td>50</td>
</tr>
</table>
Align Table Headers
By default, table headers are bold and centered:
Example
th {
text-align: left;
}
Name Age
Jill Smith 50
Eve Jackson 94
Example
<table>
<tr>
<th colspan="2">Name</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
Table Caption
You can add a caption that serves as a heading for
the entire table.
Monthly savings
Month Savings
January $100
February $50
Example
<table style="width:100%">
<caption>Monthly savings</caption>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$50</td>
</tr>
</table>
With Padding
hello hello hello
hello hello hello
hello hello hello
With Spacing
hello hello hello
hello hello hello
hello hello hello
Example
th, td {
padding: 15px;
}
To add padding only above the content, use
the padding-top property.
And the others sides with the padding-
bottom, padding-left, and padding-
right properties:
Example
th, td {
padding-top: 10px;
padding-bottom: 20px;
padding-left: 30px;
padding-right: 40px;
}
Example
table {
border-spacing: 30px;
}
NAME
APRIL
2022
FIESTA
Example
<table>
<tr>
<th colspan="2">Name</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>43</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>57</td>
</tr>
</table>
Example
<table>
<tr>
<th>Name</th>
<td>Jill</td>
</tr>
<tr>
<th rowspan="2">Phone</th>
<td>555-1234</td>
</tr>
<tr>
<td>555-8745</td>
</tr>
</table>
Example
tr:nth-child(even) {
background-color: #D6EEEE;
}
Example
td:nth-child(even), th:nth-child(even) {
background-color: #D6EEEE;
}
ADVERTISEMENT
Example
tr:nth-child(even) {
background-color: rgba(150, 212, 212, 0.4);
}
th:nth-child(even),td:nth-child(even) {
background-color: rgba(150, 212, 212, 0.4);
}
Horizontal Dividers
First Name Last Name Savings
Peter Griffin $100
Lois Griffin $150
Joe Swanson $300
Example
tr {
border-bottom: 1px solid #ddd;
}
Hoverable Table
Use the :hover selector on tr to highlight table
rows on mouse over:
First Name Last Name Savings
Peter Griffin $100
Lois Griffin $150
Joe Swanson $300
Example
Example
<ol>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Example
<ol start="50">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Example
<ol>
<li>Coffee</li>
<li>Tea
<ol>
<li>Black tea</li>
<li>Green tea</li>
</ol>
</li>
<li>Milk</li>
</ol>
<dl>
<dt>Coffee</dt>
<dd>- black hot drink</dd>
<dt>Milk</dt>
<dd>- white cold drink</dd>
</dl>