HTML Headings
HTML Headings
<h1> defines the most important heading. <h6> defines the least important heading
HTML Paragraphs
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
HTML Links
HTML Images
The source file (src), alternative text (alt), width, and height are provided as
attributes.
HTML Buttons
<button>Click me</button>
HTML Lists
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Empty HTML Elements
<br> is an empty element without a closing tag (the <br> tag defines a line break):
HTML links are defined with the <a> tag. The link address is specified in
the href attribute:
<img src="img_girl.jpg">
Images in HTML have a set of size attributes, which specifies the width and height
of the image:
The alt attribute specifies an alternative text to be used, when an image cannot be
displayed.
The value of the attribute can be read by screen readers. This way, someone
"listening" to the webpage, e.g. a vision impaired person, can "hear" the element.
The style attribute is used to specify the styling of an element, like color, font, size
etc.
<!DOCTYPE html>
<html lang="en-US">
<body>
...
</body>
</html>
Here, a title attribute is added to the <p> element. The value of the title attribute
will be displayed as a tooltip when you mouse over the paragraph:
Bigger Headings
Each HTML heading has a default size. However, you can specify the size for any
heading with the style attribute, using the CSS font-size property
The <hr> tag defines a thematic break in an HTML page, and is most often displayed
as a horizontal rule.
The <hr> element is used to separate content (or define a change) in an HTML
page:
The <head> element is a container for metadata. HTML metadata is data about the
HTML document. Metadata is not displayed.
The <head> element is placed between the <html> tag and the <body> tag:
<!DOCTYPE html>
<html>
<head>
<title>My First HTML</title>
<meta charset="UTF-8">
</head>
<body>
.
.
.
HTML Paragraphs
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
Use <br> if you want a line break (a new line) without starting a new paragraph:
The text inside a <pre> element is displayed in a fixed-width font (usually Courier),
and it preserves both spaces and line breaks:
<pre>
My Bonnie lies over the ocean.
My Bonnie lies over the sea.
Setting the style of an HTML element, can be done with the style attribute.
<tagname style="property:value;">
The background-color property defines the background color for an HTML element.
<body style="background-color:powderblue;">
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
The color property defines the text color for an HTML element:
HTML Fonts
The font-family property defines the font to be used for an HTML element:
The font-size property defines the text size for an HTML element:
<h1 style="font-size:300%;">This is a heading</h1>
<p style="font-size:160%;">This is a paragraph.</p>
The text-align property defines the horizontal text alignment for an HTML
element:
In the previous chapter, you learned about the HTML style attribute.
HTML also defines special elements for defining text with a special meaning.
HTML uses elements like <b> and <i> for formatting output, like bold or italictext.
The HTML <b> element defines bold text, without any extra importance.
The HTML <strong> element defines strong text, with added semantic "strong"
importance.
The HTML <i> element defines italic text, without any extra importance.
<i>This text is italic</i>
The HTML <em> element defines emphasized text, with added semantic importance.
The HTML <blockquote> element defines a section that is quoted from another
source.
The <address> element is usually displayed in italic. Most browsers will add a line
break before and after the element.
<address>
Written by John Doe.<br>
Visit us at:<br>
Example.com<br>
Box 564, Disneyland<br>
USA
</address>
You can add comments to your HTML source by using the following syntax:
HTML colors are specified using predefined color names, or RGB, HEX, HSL, RGBA,
HSLA values.
Color Names
Tomato Orange
DodgerBlue MediumSeaGreen
Gray SlateBlue
Violent LightGray
<body>
<h1 style="background-color:Tomato;">Tomato</h1>
<h1 style="background-color:Orange;">Orange</h1>
<h1 style="background-color:DodgerBlue;">DodgerBlue</h1>
<h1 style="background-color:MediumSeaGreen;">MediumSeaGreen</h1>
<h1 style="background-color:Gray;">Gray</h1>
<h1 style="background-color:SlateBlue;">SlateBlue</h1>
<h1 style="background-color:Violet;">Violet</h1>
<h1 style="background-color:LightGray;">LightGray</h1>
</body>
Background Color
Example
<h1 style="background-color:DodgerBlue;">Hello World</h1>
<p style="background-color:Tomato;">Lorem ipsum...</p>
Text Color
Border Color
Color Values
In HTML, colors can also be specified using RGB values, HEX values, HSL values,
RGBA values, and HSLA values:
Example
<h1 style="background-color:rgb(255, 99, 71);">...</h1>
<h1 style="background-color:#ff6347;">...</h1>
<h1 style="background-color:hsl(9, 100%, 64%);">...</h1>
HEX Value
Where rr (red), gg (green) and bb (blue) are hexadecimal values between 00 and ff
(same as decimal 0-255).
For example, #ff0000 is displayed as red, because red is set to its highest value (ff)
and the others are set to the lowest value (00).
<h1 style="background-color:#ff0000;">#ff0000</h1>
<h1 style="background-color:#0000ff;">#0000ff</h1>
<h1 style="background-color:#3cb371;">#3cb371</h1>
<h1 style="background-color:#ee82ee;">#ee82ee</h1>
<h1 style="background-color:#ffa500;">#ffa500</h1>
<h1 style="background-color:#6a5acd;">#6a5acd</h1>
HSL Value
In HTML, a color can be specified using hue, saturation, and lightness (HSL) in the
form:
Hue is a degree on the color wheel from 0 to 360. 0 is red, 120 is green, and 240 is
blue.
Saturation is a percentage value, 0% means a shade of gray, and 100% is the full
color.
Saturation
50% is 50% gray, but you can still see the color.
Lightness
The lightness of a color can be described as how much light you want to give the
color, where 0% means no light (black), 50% means 50% light (neither dark nor
light) 100% means full lightness (white).
Shades of gray are often defined by setting the hue and saturation to 0, and adjust
the lightness from 0% to 100% to get darker/lighter shades:
<h1 style="background-color:hsl(0, 0%, 0%);">hsl(0, 0%, 0%)</h1>
RGBA color values are an extension of RGB color values with an alpha channel -
which specifies the opacity for a color.
The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (not
transparent at all):
HSLA Value
HSLA color values are an extension of HSL color values with an alpha channel -
which specifies the opacity for a color.
The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (not
transparent at all):
CSS saves a lot of work. It can control the layout of multiple web pages all at
once.
Inline CSS
This example sets the text color of the <h1> element to blue:
Internal CSS
<!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>
External CSS
An external style sheet is used to define the style for many HTML pages.
With an external style sheet, you can change the look of an entire web site,
by changing one file!
To use an external style sheet, add a link to it in the <head> section of the HTML
page:
<!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>
An 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.
body {
background-color: powderblue;
}
h1 {
color: blue;
}
p {
color: red;
}
CSS Fonts
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
CSS Border
p {
border: 1px solid powderblue;
}
CSS Padding
The CSS padding property defines a padding (space) between the text and the
border:
p {
border: 1px solid powderblue;
padding: 30px;
}
CSS Margin
The CSS margin property defines a margin (space) outside the border:
p {
border: 1px solid powderblue;
margin: 50px;
}
The id Attribute
To define a specific style for one special element, add an id attribute to the
element:
then define a style for the element with the specific id:
#p01 {
color: blue;
}
To define a style for special types of elements, add a class attribute to the
element:
then define a style for the elements with the specific class:
p.error {
color: red;
}
External References
External style sheets can be referenced with a full URL or with a path relative to the
current web page.
<link rel="stylesheet"href="https://www.w3schools.com/html/styles.css">
This example links to a style sheet located in the html folder on the current web
site:
This example links to a style sheet located in the same folder as the current page:
Links are found in nearly all web pages. Links allow users to click their way from
page to page.
When you move the mouse over a link, the mouse arrow will turn into a little hand.
The link text is the visible part (Visit our HTML tutorial).
Clicking on the link text will send you to the specified address.
Local Links
A local link (link to the same web site) is specified with a relative URL (without
https://www....).
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>
This example will open the linked document in a new browser window/tab:
Tip: If your webpage is locked in a frame, you can use target="_top" to break out
of the frame:
<a href="default.asp">
<img src="smiley.gif" alt="HTML
tutorial"style="width:42px;height:42px;border:0;">
</a>
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
<a href="https://www.w3schools.com/html/" title="Go to W3Schools HTML
section">Visit our HTML Tutorial</a>
HTML bookmarks are used to allow readers to jump to specific parts of a Web page.
To make a bookmark, you must first create the bookmark, and then add a link to it.
When the link is clicked, the page will scroll to the location with the bookmark.
Example
Then, add a link to the bookmark ("Jump to Chapter 4"), from within the same
page:
Or, add a link to the bookmark ("Jump to Chapter 4"), from another page:
External Paths
External pages can be referenced with a full URL or with a path relative to the
current web page.
This example uses a full URL to link to a web page:
This example links to a page located in the html folder on the current web site:
This example links to a page located in the same folder as the current page:
HTML Images
Example
<img src="pic_trulli.jpg" alt="Italian Trulli">
Example
<img src="img_girl.jpg" alt="Girl in a jacket">
The <img> tag is empty, it contains attributes only, and does not have a closing tag.
The src attribute specifies the URL (web address) of the image:
<img src="url">
The alt attribute provides an alternate text for an image, if the user for some
reason cannot view it (because of slow connection, an error in the src attribute, or
if the user uses a screen reader).
If a browser cannot find an image, it will display the value of the alt attribute:
You can use the style attribute to specify the width and height of an image.
<img src="img_girl.jpg" alt="Girl in a
jacket"style="width:500px;height:600px;">
However, we suggest using the style attribute. It prevents styles sheets from
changing the size of images:
<!DOCTYPE html>
<html>
<head>
<style>
img {
width: 100%;
}
</style>
</head>
<body>
</body>
</html>
If not specified, the browser expects to find the image in the same folder as the
web page.
However, it is common to store images in a sub-folder. You must then include the
folder name in the src attribute:
Actually, you can access images from any web address in the world:
<img src="https://www.w3schools.com/images/w3schools_green.jpg"alt="W3Schools
.com">
Animated Images
Image as a Link
To use an image as a link, put the <img> tag inside the <a> tag:
<a href="default.asp">
<img src="smiley.gif" alt="HTML
tutorial"style="width:42px;height:42px;border:0;">
</a>
Image Floating
Use the CSS float property to let the image float to the right or to the left of a
text:
Image Maps
In the image below, click on the computer, the phone, or the cup of coffee:
<img src="workplace.jpg" alt="Workplace" usemap="#workmap">
<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="Coffee"href="coffee.htm">
</map>
The name attribute of the <map> tag is associated with the <img>'s usemap attribute
and creates a relationship between the image and the map.
The <map> element contains a number of <area> tags, that define the clickable areas
in the image-map.
Background Image
To add a background image on a web page, specify the background-image property on the BODY
element:
<body style="background-image:url('clouds.jpg');">
<h2>Background Image</h2>
</body>
Example
<body>
<p style="background-image:url('clouds.jpg');">
...
</p>
</body>
HTML5 introduced the <picture> element to add more flexibility when specifying
image resources.
The <picture> element contains a number of <source> elements, each referring to
different image sources. This way the browser can choose the image that best fits
the current view and/or device.
Each <source> element have attributes describing when their image is the most
suitable.
The browser will use the first <source> element with matching attribute values, and
ignore any following <source> elements.
Example
Show one picture if the browser window (viewport) is a minimum of 650 pixels, and another image if
not, but larger than 465 pixels.
<picture>
<source media="(min-width: 650px)"srcset="img_pink_flowers.jpg">
<source media="(min-width: 465px)"srcset="img_white_flower.jpg">
<img src="img_orange_flowers.jpg" alt="Flowers"style="width:auto;">
</picture>
HTML Tables
<!DOCTYPE html>
<html>
<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<h2>HTML Table</h2>
<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>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
</table>
</body>
</html>
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
<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>
If you do not specify a border for the table, it will be displayed without borders.
table, th, td {
border: 1px solid black;
}
If you want the borders to collapse into one border, add the CSS border-
collapse property:
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
Cell padding specifies the space between the cell content and its borders.
If you do not specify a padding, the table cells will be displayed without padding.
th, td {
padding: 15px;
}
To set the border spacing for a table, use the CSS border-spacing property:
table {
border-spacing: 5px;
}
To make a cell span more than one column, use the colspan attribute:
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
</head>
<body>
</body>
</html>
To make a cell span more than one row, use the rowspan attribute:
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
</head>
<body>
<table style="width:100%">
<tr>
<th>Name:</th>
<td>Bill Gates</td>
</tr>
<tr>
<th rowspan="2">Telephone:</th>
<td>55577854</td>
</tr>
<tr>
<td>55577855</td>
</tr>
</table>
</body>
</html>
<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>
To define a special style for a special table, add an id attribute to the table:
<table id="t01">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
Example
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 15px;
text-align: left;
}
table#t01 {
width: 100%;
background-color: #f1f1c1;
}
</style>
</head>
<body>
<h2>Styling Tables</h2>
<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>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
<br>
<table id="t01">
<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>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
</body>
</html>
Now you can define a special style for this table:
table#t01 {
width: 100%;
background-color: #f1f1c1;
}
Add more style
<!DOCTYPE html>
<html>
<head>
<style>
table {
width:100%;
table, th, td {
border-collapse: collapse;
th, td {
padding: 15px;
text-align: left;
table#t01 tr:nth-child(even) {
background-color: #eee;
table#t01 tr:nth-child(odd) {
background-color: #fff;
table#t01 th {
background-color: black;
color: white;
}
</style>
</head>
<body>
<h2>Styling Tables</h2>
<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>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
<br>
<table id="t01">
<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>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
</body>
</html>