HTML
HTML
Example
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Try it Yourself »
HTML Headings
HTML headings are defined with the <h1> to <h6> tags.
Example
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<h3>This is heading 3</h3>
HTML Paragraphs
HTML paragraphs are defined with the <p> tag:
Example
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
Try it Yourself »
HTML Links
HTML links are defined with the <a> tag:
Example
<a href="https://www.w3schools.com">This is a link</a>
Try it Yourself »
HTML Images
HTML images are defined with the <img> tag.
Example
<img src="w3schools.jpg" alt="W3Schools.com" width="104" height="142
">
Try it Yourself »
HTML Buttons
HTML buttons are defined with the <button> tag:
Example
<button>Click me</button>
Try it Yourself »
HTML Lists
HTML lists are defined with the <ul> (unordered/bullet list) or
the <ol> (ordered/numbered list) tag, followed by <li> tags (list items):
Example
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
<ol>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Try it Yourself »
HTML Elements
An HTML element usually consists of a start tag and an end tag, with the
content inserted in between:
<br>
Example
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Try it Yourself »
Example Explained
The <html> element defines the whole document.
<html>
<body>
</body>
</html>
<body>
</body>
Example
<html>
<body>
<p>This is a paragraph
<p>This is a paragraph
</body>
</html>
Try it Yourself »
The example above works in all browsers, because the closing tag is
considered optional.
Example
<p>This is a <br> paragraph with a line break.</p>
Try it Yourself »
Empty elements can be "closed" in the opening tag like this: <br />.
HTML5 does not require empty elements to be closed. But if you want
stricter validation, or if you need to make your document readable by
XML parsers, you must close all HTML elements properly.
HTML Is Not Case Sensitive
HTML tags are not case sensitive: <P> means the same as <p>.
HTML Attributes
All HTML elements can have attributes
Attributes provide additional information about an element
Attributes are always specified in the start tag
Attributes usually come in name/value pairs like: name="value"
Example
<a href="https://www.w3schools.com">This is a link</a>
Try it Yourself »
You will learn more about links and the <a> tag later in this tutorial.
Example
<img src="img_girl.jpg">
Try it Yourself »
Example
<img src="img_girl.jpg" width="500" height="600">
Try it Yourself »
Example
<img src="img_girl.jpg" alt="Girl with a jacket">
Try it Yourself »
Example
<p style="color:red">This is a paragraph.</p>
Try it Yourself »
You will learn more about styling later in this tutorial, and in our CSS
Tutorial.
<!DOCTYPE html>
<html lang="en-US">
<body>
...
</body>
</html>
The first two letters specify the language (en). If there is a dialect, add
two more letters (US).
Example
<p title="I'm a tooltip">
This is a paragraph.
</p>
Try it Yourself »
Good
<a href="https://www.w3schools.com">
Try it Yourself »
Example
<p title=About W3Schools>
Try it Yourself »
Using quotes are the most common. Omitting quotes can produce errors.
At W3Schools we always use quotes around attribute values.
In some situations, when the attribute value itself contains double quotes,
it is necessary to use single quotes:
Or vice versa:
HTML Exercises
Test Yourself With Exercises
Exercise:
Add a "tooltip" to the paragraph below with the text "About W3Schools".
Submit Answer »
HTML Attributes
Below is an alphabetical list of some attributes often used in HTML, which you will learn
more about in this tutorial:
Attribute Description
alt Specifies an alternative text for an image, when the image cannot be displ
Headings
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Try it Yourself »
HTML Headings
Headings are defined with the <h1> to <h6> tags.
Example
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<h4>Heading 4</h4>
<h5>Heading 5</h5>
<h6>Heading 6</h6>
Try it Yourself »
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:
Example
<h1 style="font-size:60px;">Heading 1</h1>
Try it Yourself »
Example
<h1>This is heading 1</h1>
<p>This is some text.</p>
<hr>
<h2>This is heading 2</h2>
<p>This is some other text.</p>
<hr>
Try it Yourself »
Example
<!DOCTYPE html>
<html>
<head>
<title>My First HTML</title>
<meta charset="UTF-8">
</head>
<body>
.
.
.
Try it Yourself »
Submit Answer »
You will learn more about HTML tags and attributes in the next chapters of this tutorial.
Tag Description
For a complete list of all available HTML tags, visit our HTML Tag
Reference.
HTML Paragraphs
HTML Paragraphs
The HTML <p> element defines a paragraph:
Example
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
Try it Yourself »
HTML Display
You cannot be sure how HTML will be displayed.
Large or small screens, and resized windows will create different results.
With HTML, you cannot change the output by adding extra spaces or
extra lines in your HTML code.
The browser will remove any extra spaces and extra lines when the page
is displayed:
Example
<p>
This paragraph
contains a lot of lines
in the source code,
but the browser
ignores it.
</p>
<p>
This paragraph
contains a lot of spaces
in the source code,
but the browser
ignores it.
</p>
Try it Yourself »
Example
<p>This is a paragraph.
<p>This is another paragraph.
Try it Yourself »
The example above will work in most browsers, but do not rely on it.
Use <br> if you want a line break (a new line) without starting a new
paragraph:
Example
<p>This is<br>a paragraph<br>with line breaks.</p>
Try it Yourself »
Example
<p>
My Bonnie lies over the ocean.
Example
<pre>
My Bonnie lies over the ocean.
HTML Exercises
Test Yourself With Exercises
Exercise:
Use the correct HTML tag to add a paragraph with the text "Hello World!".
<html>
<body>
</body>
</html>
Submit Answer »
Tag Description
For a complete list of all available HTML tags, visit our HTML Tag
Reference.
HTML Styles
❮ PreviousNext ❯
Example
I am Red
I am Blue
I am Big
Try it Yourself »
The HTML Style Attribute
Setting the style of an HTML element, can be done with
the style attribute.
<tagname style="property:value;">
Background Color
The CSS background-color property defines the background color for an
HTML element.
Example
<body style="background-color:powderblue;">
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
Try it Yourself »
Text Color
The CSS color property defines the text color for an HTML element:
Example
<h1 style="color:blue;">This is a heading</h1>
<p style="color:red;">This is a paragraph.</p>
Try it Yourself »
Fonts
The CSS font-family property defines the font to be used for an HTML
element:
Example
<h1 style="font-family:verdana;">This is a heading</h1>
<p style="font-family:courier;">This is a paragraph.</p>
Try it Yourself »
Text Size
The CSS font-size property defines the text size for an HTML element:
Example
<h1 style="font-size:300%;">This is a heading</h1>
<p style="font-size:160%;">This is a paragraph.</p>
Try it Yourself »
Text Alignment
The CSS text-align property defines the horizontal text alignment for an
HTML element:
Example
<h1 style="text-align:center;">Centered Heading</h1>
<p style="text-align:center;">Centered paragraph.</p>
Try it Yourself »
Chapter Summary
Use the style attribute for styling HTML elements
Use background-color for background color
Use color for text colors
Use font-family for text fonts
Use font-size for text sizes
Use text-align for text alignment
HTML Exercises
Test Yourself With Exercises
Exercise:
Use the correct HTML attribute, and CSS, to set the color of the paragraph to
"blue".
Submit Answer »
HTML Text Formatting
❮ PreviousNext ❯
Text Formatting
This text is bold
This is subscript and superscript
Try it Yourself »
Example
<b>This text is bold</b>
Try it Yourself »
Example
<strong>This text is strong</strong>
Try it Yourself »
Example
<i>This text is italic</i>
Try it Yourself »
Example
<em>This text is emphasized</em>
Try it Yourself »
Example
<h2>HTML <small>Small</small> Formatting</h2>
Try it Yourself »
HTML <mark> Element
The HTML <mark> element defines marked/highlighted text:
Example
<h2>HTML <mark>Marked</mark> Formatting</h2>
Try it Yourself »
Example
<p>My favorite color is <del>blue</del> red.</p>
Try it Yourself »
Example
<p>My favorite <ins>color</ins> is red.</p>
Try it Yourself »
Example
<p>This is <sub>subscripted</sub> text.</p>
Try it Yourself »
Example
<p>This is <sup>superscripted</sup> text.</p>
Try it Yourself »
HTML Exercises
Test Yourself With Exercises
Exercise:
Add extra importance to the word "degradation" in the paragraph below.
<p>
WWF's mission is to stop the degradation of our
planet's natural environment.
</p>
Submit Answer »
Tag Description
<b> Defines bold text
Quotation
Here is a quote from WWF's website:
For nearly 60 years, WWF has been protecting the future of nature. The
world's leading conservation organization, WWF works in 100 countries
and is supported by more than one million members in the United States
and close to five million globally.
Try it Yourself »
Example
<p>WWF's goal is to: <q>Build a future where people live in harmony
with nature.</q></p>
Try it Yourself »
Example
<p>The <abbr title="World Health Organization">WHO</abbr> was
founded in 1948.</p>
Try it Yourself »
Example
<address>
Written by John Doe.<br>
Visit us at:<br>
Example.com<br>
Box 564, Disneyland<br>
USA
</address>
Try it Yourself »
Example
<p><cite>The Scream</cite> by Edvard Munch. Painted in 1893.</p>
Try it Yourself »
Example
<bdo dir="rtl">This text will be written from right to left</bdo>
Try it Yourself »
HTML Exercises
Test Yourself With Exercises
Exercise:
Use an HTML element to add quotation marks around the letters "cool".
<p>
I am so cool .
</p>
Submit Answer »
Tag Description
Comment tags are used to insert comments in the HTML source code.
Notice that there is an exclamation point (!) in the opening tag, but not in
the closing tag.
Note: Comments are not displayed by the browser, but they can help
document your HTML source code.
With comments you can place notifications and reminders in your HTML:
Example
<!-- This is a comment -->
<p>This is a paragraph.</p>
Comments are also great for debugging HTML, because you can comment
out HTML lines of code, one at a time, to search for errors:
Example
<!-- Do not display this image at the moment
<img border="0" src="pic_trulli.jpg" alt="Trulli">
-->
Try it Yourself »
HTML Exercises
Test Yourself With Exercises
Exercise:
Use the HTML comment tag to make a comment out of the "This is a comment"
text.
<h1>This is a heading</h1>
This is a comment
<p>This is a paragraph.</p>
HTML Colors
HTML colors are specified using predefined color names, or RGB, HEX,
HSL, RGBA, HSLA values.
Color Names
In HTML, a color can be specified by using a color name:
Tomato
Orange
DodgerBlue
MediumSeaGreen
Gray
SlateBlue
Violet
LightGray
Try it Yourself »
Background Color
You can set the background color for HTML elements:
Hello World
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat
volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation
ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
Example
<h1 style="background-color:DodgerBlue;">Hello World</h1>
<p style="background-color:Tomato;">Lorem ipsum...</p>
Try it Yourself »
Text Color
You can set the color of text:
Hello World
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat
volutpat.
Example
<h1 style="color:Tomato;">Hello World</h1>
<p style="color:DodgerBlue;">Lorem ipsum...</p>
<p style="color:MediumSeaGreen;">Ut wisi enim...</p>
Try it Yourself »
Border Color
You can set the color of borders:
Hello World
Hello World
Hello World
Example
<h1 style="border:2px solid Tomato;">Hello World</h1>
<h1 style="border:2px solid DodgerBlue;">Hello World</h1>
<h1 style="border:2px solid Violet;">Hello World</h1>
Try it Yourself »
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>
<h1 style="background-color:rgba(255, 99, 71, 0.5);">...</h1>
<h1 style="background-color:hsla(9, 100%, 64%, 0.5);">...</h1>
Try it Yourself »
RGB Value
In HTML, a color can be specified as an RGB value, using this formula:
rgb(red, green, blue)
Each parameter (red, green, and blue) defines the intensity of the color
between 0 and 255.
To display black, set all color parameters to 0, like this: rgb(0, 0, 0).
To display white, set all color parameters to 255, like this: rgb(255, 255,
255).
Shades of gray are often defined using equal values for all the 3 light
sources:
Example
rgb(0, 0, 0)
HEX Value
In HTML, a color can be specified using a hexadecimal value in the form:
#rrggbb
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).
Example
#ff0000
#0000ff
#3cb371
#ee82ee
#ffa500
#6a5acd
Try it Yourself »
Shades of gray are often defined using equal values for all the 3 light sources:
Try it Yourself »
HSL Value
In HTML, a color can be specified using hue, saturation, and lightness
(HSL) in the form:
hsl(hue, saturation, lightness)
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.
Lightness is also a percentage, 0% is black, 50% is neither light or dark, 100% is white
Try it Yourself »
Saturation
Saturation can be described as the intensity of a color.
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).
Try it Yourself »
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:
Try it Yourself »
RGBA Value
RGBA color values are an extension of RGB color values with an alpha
channel - which specifies the opacity for a color.
rgba(red, green, blue, alpha)
The alpha parameter is a number between 0.0 (fully transparent) and 1.0
(not transparent at all):
Try it Yourself »
HSLA Value
HSLA color values are an extension of HSL color values with an alpha
channel - which specifies the opacity for a color.
hsla(hue, saturation, lightness, alpha)
The alpha parameter is a number between 0.0 (fully transparent) and 1.0
(not transparent at all):
Example
HTML Styles - CSS
CSS saves a lot of work. It can control the layout of multiple web pages
all at once.
The most common way to add CSS, is to keep the styles in separate CSS
files. However, here we will use inline and internal styling, because this is
easier to demonstrate, and easier for you to try it yourself.
Inline CSS
An inline CSS is used to apply a unique style to a single HTML element.
An inline CSS uses the style attribute of an HTML element.
Example
<h1 style="color:blue;">This is a Blue Heading</h1>
Try it Yourself »
Internal CSS
An internal CSS is used to define a style for a single HTML page.
Example
<!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>
Try it Yourself »
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!
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>
Try it Yourself »
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
The CSS color property defines the text color to be used.
The CSS font-family property defines the font to be used.
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:
Example
p {
border: 1px solid powderblue;
}
Try it Yourself »
CSS Padding
The CSS padding property defines a padding (space) between the text and
the border:
Example
p {
border: 1px solid powderblue;
padding: 30px;
}
Try it Yourself »
CSS Margin
The CSS margin property defines a margin (space) outside the border:
Example
p {
border: 1px solid powderblue;
margin: 50px;
}
Try it Yourself »
The id Attribute
To define a specific style for one special element, add an id attribute to
the element:
<p id="p01">I am different</p>
then define a style for the element with the specific id:
Example
#p01 {
color: blue;
}
Try it Yourself »
<p class="error">I am different</p>
then define a style for the elements with the specific class:
Example
p.error {
color: red;
}
Try it Yourself »
External References
External style sheets can be referenced with a full URL or with a path
relative to the current web page.
Example
<link rel="stylesheet" href="https://www.w3schools.com/html/styles.c
ss">
Try it Yourself »
This example links to a style sheet located in the html folder on the
current web site:
Example
<link rel="stylesheet" href="/html/styles.css">
Try it Yourself »
This example links to a style sheet located in the same folder as the
current page:
Example
<link rel="stylesheet" href="styles.css">
Try it Yourself »
You can read more about file paths in the chapter HTML File Paths.
Chapter Summary
Use the HTML style attribute for inline styling
Use the HTML <style> element to define internal CSS
Use the HTML <link> element to refer to an external CSS file
Use the HTML <head> element to store <style> and <link> elements
Use the CSS color property for text colors
Use the CSS font-family property for text fonts
Use the CSS font-size property for text sizes
Use the CSS border property for borders
Use the CSS padding property for space inside the border
Use the CSS margin property for space outside the border
HTML Exercises
Test Yourself With Exercises
Exercise:
Use CSS to set the background color of the document (body) to yellow.
<!DOCTYPE html>
<html>
<head>
<style>
:yellow;
</style>
</head>
<body>
</body>
</html>
Submit Answer »
Tag Description
When you move the mouse over a link, the mouse arrow will turn into a
little hand.
Note: A link does not have to be text. It can be an image or any other
HTML element.
<a href="url">link text</a>
Example
<a href="https://www.w3schools.com/html/">Visit our HTML
tutorial</a>
Try it Yourself »
Clicking on the link text will send you to the specified address.
Note: Without a forward slash at the end of subfolder addresses, you
might generate two requests to the server. Many servers will
automatically add a forward slash to the end of the address, and then
create a new request.
Local Links
The example above used an absolute URL (a full web address).
A local link (link to the same web site) is specified with a relative URL
(without https://www....).
Example
<a href="html_images.asp">HTML Images</a>
Try it Yourself »
Example
<a href="https://www.w3schools.com/" target="_blank">Visit
W3Schools!</a>
Try it Yourself »
Example
<a href="https://www.w3schools.com/html/" target="_top">HTML5
tutorial!</a>
Try it Yourself »
Example
<a href="default.asp">
<img src="smiley.gif" alt="HTML
tutorial" style="width:42px;height:42px;border:0;">
</a>
Try it Yourself »
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>
Try it Yourself »
External Paths
External pages can be referenced with a full URL or with a path relative to
the current web page.
Example
<a href="https://www.w3schools.com/html/default.asp">HTML
tutorial</a>
Try it Yourself »
This example links to a page located in the html folder on the current web
site:
Example
<a href="/html/default.asp">HTML tutorial</a>
Try it Yourself »
This example links to a page located in the same folder as the current
page:
Example
<a href="default.asp">HTML tutorial</a>
Try it Yourself »
You can read more about file paths in the chapter HTML File Paths.
Chapter Summary
Use the <a> element to define a link
Use the href attribute to define the link address
Use the target attribute to define where to open the linked
document
Use the <img> element (inside <a>) to use an image as a link
HTML Exercises
Test Yourself With Exercises
Exercise:
Use the correct HTML to make the text below into a link to "default.html".
Submit Answer »
Tag Description
HTML Link Colors
❮ PreviousNext ❯
HTML Link Colors
By default, a link will appear like this (in all browsers):
Example
<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>
Try it Yourself »
This is a link
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>
Try it Yourself »
Tag Description
HTML Link Bookmarks
When the link is clicked, the page will scroll to the location with the
bookmark.
Example
First, create a bookmark with the id attribute:
<h2 id="C4">Chapter 4</h2>
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:
Example
<a href="html_demo.html#C4">Jump to Chapter 4</a>
Try it Yourself »
Chapter Summary
Use the id attribute (id="value") to define bookmarks in a page
Use the href attribute (href="#value") to link to the bookmark
Tag Description
Images can improve the design and the appearance of a web page.
Example
<img src="pic_trulli.jpg" alt="Italian Trulli">
Try it Yourself »
Example
<img src="img_girl.jpg" alt="Girl in a jacket">
Try it Yourself »
Example
<img src="img_chania.jpg" alt="Flowers in Chania">
Try it Yourself »
<img src="url">
Example
<img src="img_chania.jpg" alt="Flowers in Chania">
Try it Yourself »
Example
<img src="wrongname.gif" alt="Flowers in Chania">
Try it Yourself »
Example
<img src="img_girl.jpg" alt="Girl in a
jacket" style="width:500px;height:600px;">
Try it Yourself »
Example
<img src="img_girl.jpg" alt="Girl in a
jacket" width="500" height="600">
Try it Yourself »
Example
<!DOCTYPE html>
<html>
<head>
<style>
img {
width: 100%;
}
</style>
</head>
<body>
<img src="html5.gif" alt="HTML5 Icon" width="128" height="128">
<img src="html5.gif" alt="HTML5
Icon" style="width:128px;height:128px;">
</body>
</html>
Try it Yourself »
Example
<img src="/images/html5.gif" alt="HTML5
Icon" style="width:128px;height:128px;">
Try it Yourself »
Actually, you can access images from any web address in the world:
Example
<img src="https://www.w3schools.com/images/w3schools_green.jpg" alt=
"W3Schools.com">
Try it Yourself »
You can read more about file paths in the chapter HTML File Paths.
Animated Images
HTML allows animated GIFs:
Example
<img src="programming.gif" alt="Computer
Man" style="width:48px;height:48px;">
Try it Yourself »
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;border:0;">
</a>
Try it Yourself »
Image Floating
Use the CSS float property to let the image float to the right or to the left
of a text:
Example
<p><img src="smiley.gif" alt="Smiley
face" style="float:right;width:42px;height:42px;">
The image will float to the right of the text.</p>
<p><img src="smiley.gif" alt="Smiley
face" style="float:left;width:42px;height:42px;">
The image will float to the left of the text.</p>
Try it Yourself »
Tip: To learn more about CSS Float, read our CSS Float Tutorial.
Chapter Summary
Use the HTML <img> element to define an image
Use the HTML src attribute to define the URL of the image
Use the HTML alt attribute to define an alternate text for an image,
if it cannot be displayed
Use the HTML width and height attributes to define the size of the
image
Use the CSS width and height properties to define the size of the
image (alternatively)
Use the CSS float property to let the image float
Loading images takes time. Large images can slow down your page. Use
images carefully.
HTML Exercises
Test Yourself With Exercises
Exercise:
Use the HTML image attributes to set the size of the image to 250 pixels wide
and 400 pixels tall.
Submit Answer »
Tag Description
HTML Image Maps
Click on the computer, the phone, or the cup of coffee in the image
below:
Example
<img src="workplace.jpg" alt="Workplace" usemap="#workmap">
<map name="workmap">
<area shape="rect" coords="34,44,270,350" alt="Computer" href="com
puter.htm">
<area shape="rect" coords="290,172,333,250" alt="Phone" href="phon
e.htm">
<area shape="circle" coords="337,300,44" alt="Coffee" href="coffee
.htm">
</map>
Try it Yourself »
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 a map containing some
rules that describe 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 Map
Then add a <map> element.
<map name="workmap">
The Areas
Then add the clickable areas.
Shape
You must define the shape of the area, and you can choose one of these
values:
Coordinates
You must define some coordinates to be able to place the clickable area
onto the image.
The coordinates come in pairs, one for the x-axis and one for the y-axis.
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:
Now you have enough data to create a clickable rectangular area:
This is the area that becomes clickable and will send the user to the page
computer.htm:
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
Now you have enough data to create a clickable circular area:
This is the area that becomes clickable and will send the user to the page
coffee.htm:
Example
You can use the onclick attribute to execute a JavaScript function when
the area is clicked
<area shape="circle" coords="337,300,44" onclick="myFunction()">
Try it Yourself »
Chapter Summary
Use the HTML <map> element to define an image-map
Use the HTML <area> element to define the clickable areas in the
image-map
Use the HTML <img>'s element usemap attribute to point to an image-
map
HTML Background Images
Background Images
A background image can be specified on almost any HTML element.
Example
Add a background image on a HTML element:
<div style="background-image: url('img_girl.jpg');">
Try it Yourself »
Example
Specify the background image in the style element:
<style>
div {
background-image: url('img_girl.jpg');
}
</style>
Try it Yourself »
Example
Add a background image on a HTML page:
<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 has reach the end of the
element.
To explain, see what happens when we use a small image as a
background inside a large div 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 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:
As you can see, the 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 stretch to fit the entire image in the
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>
Try it Yourself »
Example
Show different images on different screen sizes:
<picture>
<source media="(min-width: 650px)" srcset="img_food.jpg">
<source media="(min-width: 465px)" srcset="img_car.jpg">
<img src="img_girl.jpg">
</picture>
Try it Yourself »
1. Bandwidth
If you have a small screen or device, it is not necessary to load a large
image file. The browser will use the first <source> element with matching
attribute values, and ignore any of the following elements.
2. Format Support
Some browsers or devices may not support all image formats. By using
the <picture> element, you can add images of all formats, and the browser
will use the first format it recognizes and ignore any of the following.
Example
The browser will use the first image format it recognizes:
<picture>
<source srcset="img_avatar.png">
<source srcset="img_girl.jpg">
<img src="img_beatles.gif" alt="Beatles" style="width:auto;">
</picture>
Try it Yourself »
Company Contact
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, th, td {
border: 1px solid black;
}
Try it Yourself »
Remember to define borders for both the table and the table cells.
HTML Table - Collapsed Borders
If you want the borders to collapse into one border, add the
CSS border-collapse property:
Example
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
Try it Yourself »
Example
th, td {
padding: 15px;
}
Try it Yourself »
Example
th {
text-align: left;
}
Try it Yourself »
Example
table {
border-spacing: 5px;
}
Try it Yourself »
Example
<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>
Try it Yourself »
HTML Table - Adding a Caption
To add a caption to a table, use the <caption> tag:
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>
Try it Yourself »
Example
<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>
Chapter Summary
Use the HTML <table> element to define a table
Use the HTML <tr> element to define a table row
Use the HTML <td> element to define a table data
Use the HTML <th> element to define a table heading
Use the HTML <caption> element to define a table caption
Use the CSS border property to define a border
Use the CSS border-collapse property to collapse cell
borders
Use the CSS padding property to add padding to cells
Use the CSS text-align property to align cell text
Use the CSS border-spacing property to set the spacing
between cells
Use the colspan attribute to make a cell span many columns
Use the rowspan attribute to make a cell span many rows
Use the id attribute to uniquely define one table
HTML Exercises
Test Yourself With Exercises
Exercise:
Add a table row with two table headers.
The two table headers should have the value "Name" and "Age".
<table>
<tr>
<td>Jill Smith</td>
<td>50</td>
</tr>
</table>
Submit Answer »
For a complete list of all available HTML tags, visit our HTML Tag
Reference.
HTML Lists
❮ PreviousNext ❯
An Ordered List:
1. First item
2. Second item
3. Third item
4. Fourth item
Try it Yourself »
The list items will be marked with bullets (small black circles) by default:
Example
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Try it Yourself »
Value Description
Example - Disc
<ul style="list-style-type:disc;">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Try it Yourself »
Example - Circle
<ul style="list-style-type:circle;">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Try it Yourself »
Example - Square
<ul style="list-style-type:square;">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Try it Yourself »
Example - None
<ul style="list-style-type:none;">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Try it Yourself »
Example
<ol>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Try it Yourself »
Type Description
type="i" The list items will be numbered with lowercase roman numbers
Numbers:
<ol type="1">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Try it Yourself »
Uppercase Letters:
<ol type="A">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Try it Yourself »
Lowercase Letters:
<ol type="a">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Try it Yourself »
Example
<dl>
<dt>Coffee</dt>
<dd>- black hot drink</dd>
<dt>Milk</dt>
<dd>- white cold drink</dd>
</dl>
Try it Yourself »
Example
<ul>
<li>Coffee</li>
<li>Tea
<ul>
<li>Black tea</li>
<li>Green tea</li>
</ul>
</li>
<li>Milk</li>
</ul>
Try it Yourself »
Note: List items can contain new list, and other HTML elements, like
images and links, etc.
Example
<ol start="50">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Try it Yourself »
Example
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333333;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 16px;
text-decoration: none;
}
li a:hover {
background-color: #111111;
}
</style>
</head>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
Try it Yourself »
Chapter Summary
Use the HTML <ul> element to define an unordered list
Use the CSS list-style-type property to define the list item marker
Use the HTML <ol> element to define an ordered list
Use the HTML type attribute to define the numbering type
Use the HTML <li> element to define a list item
Use the HTML <dl> element to define a description list
Use the HTML <dt> element to define the description term
Use the HTML <dd> element to describe the term in a description list
Lists can be nested inside lists
List items can contain other HTML elements
Use the CSS property float:left or display:inline to display a list
horizontally
HTML Block and Inline
Elements
Block-level Elements
A block-level element always starts on a new line and takes up the full
width available (stretches out to the left and right as far as it can).
Example
<div>Hello World</div>
Try it Yourself »
Inline Elements
An inline element does not start on a new line and only takes up as much
width as necessary.
Example
<div style="background-color:black;color:white;padding:20px;">
<h2>London</h2>
<p>London is the capital city of England. It is the most populous
city in the United Kingdom, with a metropolitan area of over 13
million inhabitants.</p>
</div>
Try it Yourself »
Example
<h1>My <span style="color:red">Important</span> Heading</h1>
Try it Yourself »
Tag Description
For a complete list of all available HTML tags, visit our HTML Tag
Reference.