Untitled Document
Untitled Document
What is HTML?
● HTML stands for Hyper Text Markup Language
● HTML is the standard markup language for creating Web pages
● HTML describes the structure of a Web page
● HTML consists of a series of elements
● HTML elements tell the browser how to display the content
● HTML elements label pieces of content such as "this is a heading", "this is
a paragraph", "this is a link", etc.
Example
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
</html>
Try it Yourself »
Example Explained
● The <!DOCTYPE html> declaration defines that this document is an
HTML5 document
● The <html> element is the root element of an HTML page
● The <head> element contains meta information about the HTML page
● The <title> element specifies a title for the HTML page (which is shown
in the browser's title bar or in the page's tab)
● The <body> element defines the document's body, and is a container for
all the visible contents, such as headings, paragraphs, images, hyperlinks,
tables, lists, etc.
● The <h1> element defines a large heading
● The <p> element defines a paragraph
The HTML element is everything from the start tag to the end tag:
Note: Some HTML elements have no content (like the <br> element). These elements
are called empty elements. Empty elements do not have an end tag!
Web Browsers
The purpose of a web browser (Chrome, Edge, Firefox, Safari) is to read HTML
documents and display them correctly.
A browser does not display the HTML tags, but uses them to determine how to
display the document:
HTML Page Structure
Below is a visualization of an HTML page structure:
<html>
<head>
<title>Page title</title>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Note: The content inside the <body> section (the white area above) will be displayed in
a browser. The content inside the <title> element will be shown in the browser's title
bar or in the page's tab.
Don't worry if we use tags you have not learned about yet.
HTML Documents
All HTML documents must start with a document type declaration: <!DOCTYPE
html>.
The HTML document itself begins with <html> and ends with </html>.
The visible part of the HTML document is between <body> and </body>.
Example
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Try it Yourself »
It must only appear once, at the top of the page (before any HTML tags).
<!DOCTYPE html>
HTML Headings
HTML headings are defined with the <h1> to <h6> tags.
<h1> defines the most important heading. <h6> defines the least important
heading:
Example
Try it Yourself »
HTML Paragraphs
HTML paragraphs are defined with the <p> tag:
Example
<p>This is a paragraph.</p>
Try it Yourself »
HTML Links
HTML links are defined with the <a> tag:
Example
Try it Yourself »
HTML Images
HTML images are defined with the <img> tag.
The source file (src), alternative text (alt), width, and height are provided
as attributes:
Example
Try it Yourself »
HTML Elements
❮ PreviousNext ❯
An HTML element is defined by a start tag, some content, and an end tag.
HTML Elements
The HTML element is everything from the start tag to the end tag:
Note: Some HTML elements have no content (like the <br> element). These elements
are called empty elements. Empty elements do not have an end tag!
The following example contains four HTML elements (<html>, <body>, <h1> and
<p>):
Example
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Try it Yourself »
Example Explained
The <html> element is the root element and it defines the whole HTML
document.
<body>
</body>
Then, inside the <body> element there are two other elements: <h1> and <p>:
Example
<html>
<body>
<p>This is a paragraph
<p>This is a paragraph
</body>
</html>
Try it Yourself »
However, never rely on this! Unexpected results and errors may occur if you
forget the end tag!
The <br> tag defines a line break, and is an empty element without a closing
tag:
Example
Try it Yourself »
Tag Description
HTML Attributes
❮ PreviousNext ❯
Example
Try it Yourself »
You will learn more about links in our HTML Links chapter.
Example
<img src="img_girl.jpg">
Try it Yourself »
There are two ways to specify the URL in the src attribute:
Notes: External images might be under copyright. If you do not get permission
to use it, you may be in violation of copyright laws. In addition, you cannot
control external images; it can suddenly be removed or changed.
2. Relative URL - Links to an image that is hosted within the website. Here, the
URL does not include the domain name. If the URL begins without a slash, it will
be relative to the current page. Example: src="img_girl.jpg". If the URL begins
with a slash, it will be relative to the domain. Example:
src="/images/img_girl.jpg".
Tip: It is almost always best to use relative URLs. They will not break if you
change domain.
Example
Try it Yourself »
Example
Try it Yourself »
Example
See what happens if we try to display an image that does not exist:
Try it Yourself »
You will learn more about images in our HTML Images chapter.
Example
Try it Yourself »
You will learn more about styles in our HTML Styles chapter.
<!DOCTYPE html>
<html lang="en">
<body>
...
</body>
</html>
Country codes can also be added to the language code in the lang attribute.
So, the first two characters define the language of the HTML page, and the last
two characters define the country.
The following example specifies English as the language and United States as
the country:
<!DOCTYPE html>
<html lang="en-US">
<body>
...
</body>
</html>
You can see all the language codes in our HTML Language Code Reference.
The value of the title attribute will be displayed as a tooltip when you mouse
over the element:
Example
Try it Yourself »
The title attribute (and all other attributes) can be written with uppercase or
lowercase like title or TITLE.
However, W3C recommends quotes in HTML, and demands quotes for stricter
document types like XHTML.
Good:
Bad:
Sometimes you have to use quotes. This example will not display the title
attribute correctly, because it contains a space:
Example
Try it Yourself »
In some situations, when the attribute value itself contains double quotes, it is
necessary to use single quotes:
Or vice versa:
Try it Yourself »
Chapter Summary
● All HTML elements can have attributes
● The href attribute of <a> specifies the URL of the page the link goes to
● The src attribute of <img> specifies the path to the image to be displayed
● The width and height attributes of <img> provide size information for
images
● The alt attribute of <img> provides an alternate text for an image
● The style attribute is used to add styles to an element, such as color,
font, size, and more
● The lang attribute of the <html> tag declares the language of the Web
page
● The title attribute defines some extra information about an element
HTML Headings
❮ PreviousNext ❯
HTML headings are titles or subtitles that you want to display on a webpage.
Example
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Try it Yourself »
HTML Headings
HTML headings are defined with the <h1> to <h6> tags.
<h1> defines the most important heading. <h6> defines the least important
heading.
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 »
Note: Browsers automatically add some white space (a margin) before and after a
heading.
Users often skim a page by its headings. It is important to use headings to show
the document structure.
<h1> headings should be used for main headings, followed by <h2> headings,
then the less important <h3>, and so on.
Note: Use HTML headings for headings only. Don't use headings to make text BIG or
bold.
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
Try it Yourself »
HTML Paragraphs
❮ PreviousNext ❯
HTML Paragraphs
The HTML <p> element defines a paragraph.
A paragraph always starts on a new line, and browsers automatically add some
white space (a margin) before and after a paragraph.
Example
<p>This is a 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 display by adding extra spaces or extra lines
in your HTML code.
The browser will automatically remove any extra spaces and lines when the
page is displayed:
Example
<p>
This paragraph
ignores it.
</p>
<p>
This paragraph
ignores it.
</p>
Try it Yourself »
The <hr> element is used to separate content (or define a change) in an HTML
page:
Example
<hr>
<hr>
Try it Yourself »
The <hr> tag is an empty tag, which means that it has no end tag.
Use <br> if you want a line break (a new line) without starting a new
paragraph:
Example
Try it Yourself »
The <br> tag is an empty tag, which means that it has no end tag.
Example
<p>
My Bonnie lies over the ocean.
</p>
Try it Yourself »
Example
<pre>
</pre>
Try it Yourself »
HTML Exercises
Exercise:
Use the correct HTML tag to add a paragraph with the text "Hello World!".
<html>
<body>
</body>
</html>
Submit Answer »
Tag Description
HTML Styles
❮ PreviousNext ❯
The HTML style attribute is used to add styles to an element, such as color,
font, size, and more.
Example
I am Red
I am Blue
I am Big
Try it Yourself »
<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 »
Example
<body>
<h1 style="background-color:powderblue;">This is a heading</h1>
</body>
Try it Yourself »
Text Color
The CSS color property defines the text color for an HTML element:
Example
Try it Yourself »
Fonts
The CSS font-family property defines the font to be used for an HTML
element:
Example
Try it Yourself »
Text Size
The CSS font-size property defines the text size for an HTML element:
Example
Try it Yourself »
Text Alignment
The CSS text-align property defines the horizontal text alignment for an
HTML element:
Example
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 contains several elements for defining text with a special meaning.
Example
Example
Try it Yourself »
The HTML <strong> element defines text with strong importance. The content
inside is typically displayed in bold.
Example
<strong>This text is important!</strong>
Try it Yourself »
Tip: The <i> tag is often used to indicate a technical term, a phrase from
another language, a thought, a ship name, etc.
Example
Try it Yourself »
The HTML <em> element defines emphasized text. The content inside is typically
displayed in italic.
Tip: A screen reader will pronounce the words in <em> with an emphasis, using
verbal stress.
Example
Try it Yourself »
HTML <small> Element
The HTML <small> element defines smaller text:
Example
Try it Yourself »
Example
Try it Yourself »
Example
Example
Try it Yourself »
Example
Try it Yourself »
Example
Try it Yourself »
HTML Comments
❮ PreviousNext ❯
HTML comments are not displayed in the browser, but they can help
document your HTML source code.
Notice that there is an exclamation point (!) in the start tag, but not in the end
tag.
Note: Comments are not displayed by the browser, but they can help document your
HTML source code.
Add Comments
With comments you can place notifications and reminders in your HTML code:
Example
<p>This is a paragraph.</p>
Try it Yourself »
Hide Content
Comments can be used to hide content.
Example
<p>This is a paragraph.</p>
Try it Yourself »
You can also hide more than one line, everything between the <!-- and the -->
will be hidden from the display.
Example
<p>This is a paragraph.</p>
<!--
-->
Try it Yourself »
Comments are also great for debugging HTML, because you can comment out
HTML lines of code, one at a time, to search for errors.
HTML Colors
❮ PreviousNext ❯
HTML colors are specified with predefined color names, or with RGB, HEX,
HSL, RGBA, or 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
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.
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit
lobortis nisl ut aliquip ex ea commodo consequat.
Example
Try it Yourself »
Border Color
You can set the color of borders:
Hello World
Hello World
Hello World
Example
Try it Yourself »
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
The following two <div> elements have their background color set with RGBA
and HSLA values, which adds an Alpha channel to the color (here we have 50%
transparency):
Example
<h1 style="background-color:#ff6347;">...</h1>
<h1 style="background-color:hsl(9, 100%, 64%);">...</h1>
Try it Yourself »
You will learn more about RGB, HEX and HSL in the next chapters.
An RGB color value represents RED, GREEN, and BLUE light sources.
This means that there are 256 x 256 x 256 = 16777216 possible colors!
For example, rgb(255, 0, 0) is displayed as red, because red is set to its highest
value (255), and the other two (green and blue) are set to 0.
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).
RED
255
GREEN
99
BLUE
71
Example
rgb(255, 0, 0)
rgb(0, 0, 255)
rgb(255, 165, 0)
Try it Yourself »
Shades of Gray
Shades of gray are often defined using equal values for all three parameters:
Example
Try it Yourself »
The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (not
transparent at all):
RED
255
GREEN
99
BLUE
71
ALPHA
0.5
Example
#rrggbb
For example, #ff0000 is displayed as red, because red is set to its highest value
(ff), and the other two (green and blue) are set to 00.
To display black, set all color parameters to 00, like this: #000000.
To display white, set all color parameters to ff, like this: #ffffff.
#ff6347
RED
ff
GREEN
63
BLUE
47
Example
#ff0000
#0000ff
#3cb371
#ee82ee
#ffa500
#6a5acd
Try it Yourself »
Shades of Gray
Shades of gray are often defined using equal values for all three parameters:
Example
#404040
#686868
#a0a0a0
#bebebe
#dcdcdc
#f8f8f8
HSLA color values are an extension of HSL with an Alpha channel (opacity).
Hue is a degree on the color wheel from 0 to 360. 0 is red, 120 is green, and
240 is blue.
HUE
SATURATION
100%
LIGHTNESS
50%
Example
Saturation
Saturation can be described as the intensity of a color.
50% is 50% gray, but you can still see the color.
Example
Try it Yourself »
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).
Example
Try it Yourself »
Shades of Gray
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:
Example
Try it Yourself »
The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (not
transparent at all):
HUE
SATURATION
100%
LIGHTNESS
50%
ALPHA
0.5
Example
CSS saves a lot of work. It can control the layout of multiple web pages all
at once.
CSS = Styles and Colors
Manipulate Text
Colors, Boxes
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!
Tip: The word cascading means that a style applied to a parent element will also apply
to all children elements within the parent. So, if you set the color of the body text to
"blue", all headings, paragraphs, and other text elements within the body will also get
the same color (unless you specify something else)!
Using CSS
CSS can be added to HTML documents in 3 ways:
Inline CSS
An inline CSS is used to apply a unique style to a single HTML element.
The following example sets the text color of the <h1> element to blue, and the
text color of the <p> element to red:
Example
Try it Yourself »
Internal CSS
An internal CSS is used to define a style for a single HTML page.
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 "powderblue" background color:
Example
<!DOCTYPE html>
<html>
<head>
<style>
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.
To use an external style sheet, add a link to it in the <head> section of each
HTML page:
Example
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Try it Yourself »
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;
Tip: With an external style sheet, you can change the look of an entire web site, by
changing one file!
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 {
}
Try it Yourself »
CSS Padding
The CSS padding property defines a padding (space) between the text and the
border.
Example
p {
padding: 30px;
Try it Yourself »
CSS Margin
The CSS margin property defines a margin (space) outside the border.
Example
margin: 50px;
Try it Yourself »
Example
Try it Yourself »
Example
This example links to a style sheet located in the html folder on the current web site:
Try it Yourself »
Example
This example links to a style sheet located in the same folder as the current page:
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
Tip: You can learn much more about CSS in our CSS Tutorial.
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>
<h1>My Home Page</h1>
</body>
</html>
Submit Answer »
Tag Description
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.
Note: A link does not have to be text. A link can be an image or any other HTML
element!
The most important attribute of the <a> element is the href attribute, which
indicates the link's destination.
The link text is the part that will be visible to the reader.
Clicking on the link text, will send the reader to the specified URL address.
Example
Try it Yourself »
Tip: Links can of course be styled with CSS, to get another look!
Example
Use target="_blank" to open the linked document in a new browser window or tab:
Try it Yourself »
A local link (a link to a page within the same website) is specified with a relative
URL (without the "https://www" part):
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>
Try it Yourself »
HTML Links - Use an Image as a Link
To use an image as a link, just put the <img> tag inside the <a> tag:
Example
<a href="default.asp">
</a>
Try it Yourself »
Example
Try it Yourself »
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
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
Try it Yourself »
Example
Use a full URL to link to a web page:
Try it Yourself »
Example
Link to a page located in the html folder on the current web site:
Try it Yourself »
Example
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
● Use the mailto: scheme inside the href attribute to create a link that
opens the user's email program
Tag Description
HTML Images
❮ PreviousNext ❯
Images can improve the design and the appearance of a web page.
Example
Try it Yourself »
Example
<img src="img_girl.jpg" alt="Girl in a jacket">
Try it Yourself »
Example
Try it Yourself »
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.
Syntax
Example
Try it Yourself »
Example
Try it Yourself »
If a browser cannot find an image, it will display the value of the alt attribute:
Example
Tip: A screen reader is a software program that reads the HTML code, and allows the
user to "listen" to the content. Screen readers are useful for people who are visually
impaired or learning disabled.
Example
Try it Yourself »
Example
Try it Yourself »
The width and height attributes always define the width and height of the
image in pixels.
Note: Always specify the width and height of an image. If width and height are not
specified, the web page might flicker while the image loads.
Width and Height, or Style?
The width, height, and style attributes are all valid in HTML.
However, we suggest using the style attribute. It prevents styles sheets from
changing the size of images:
Example
<!DOCTYPE html>
<html>
<head>
<style>
img {
width: 100%;
</style>
</head>
<body>
</body>
</html>
Try it Yourself »
Example
Try it Yourself »
To point to an image on another server, you must specify an absolute (full) URL
in the src attribute:
Example
<img src="https://www.w3schools.com/images/w3schools_green.jpg"
alt="W3Schools.com">
Try it Yourself »
Animated Images
HTML allows animated GIFs:
Example
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">
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
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 or the CSS width and height
properties to define the size of the image
● Use the CSS float property to let the image float to the left or to the
right
HTML Tables
❮ PreviousNext ❯
HTML tables allow web developers to arrange data into rows and columns.
Example
Try it Yourself »
Example
A simple HTML table:
<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>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.
Everything between <td> and </td> are the content of the table cell.
Example
<table>
<tr>
<td>Emil</td>
<td>Tobias</td>
<td>Linus</td>
</tr>
</table>
Try it Yourself »
Note: table data elements are the data containers of the table.
They can contain all sorts of HTML elements; text, images, lists, other tables, etc.
Table Rows
Each table row starts with a <tr> and end with a </tr> tag.
Example
<table>
<tr>
<td>Emil</td>
<td>Tobias</td>
<td>Linus</td>
</tr>
<tr>
<td>16</td>
<td>14</td>
<td>10</td>
</tr>
</table>
Try it Yourself »
You can have as many rows as you like in a table, just make sure that the
number of cells are the same in each row.
Note: There are times where a row can have less or more cells than another. You will
learn about that in a later chapter.
Table Headers
Sometimes you want your cells to be headers, in those cases use the <th> tag
instead of the <td> tag:
Example
<table>
<tr>
<th>Person 1</th>
<th>Person 2</th>
<th>Person 3</th>
</tr>
<tr>
<td>Emil</td>
<td>Tobias</td>
<td>Linus</td>
</tr>
<tr>
<td>16</td>
<td>14</td>
<td>10</td>
</tr>
</table>
Try it Yourself »
By default, the text in <th> elements are bold and centered, but you can
change that with CSS.
HTML Exercises
Exercise:
The two table headers should have the value "Name" and "Age".
<table>
<tr>
<td>Jill Smith</td>
<td>50</td>
</tr>
</table>
Submit Answer »
HTML Lists
❮ PreviousNext ❯
HTML lists allow web developers to group a set of related items in lists.
Example
● Item
● Item
● Item
● Item
An ordered HTML 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 »
Ordered HTML List
An ordered list starts with the <ol> tag. Each list item starts with the <li> tag.
Example
<ol>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Try it Yourself »
The <dl> tag defines the description list, the <dt> tag defines the term (name),
and the <dd> tag describes each term:
Example
<dl>
<dt>Coffee</dt>
<dt>Milk</dt>
</dl>
Try it Yourself »
Tag Description
For a complete list of all available HTML tags, visit our HTML Tag Reference.
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
<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: A list item (<li>) can contain a new list, and other HTML elements, like images
and links, etc.
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>
The HTML <ol> tag defines an ordered list. An ordered list can be numerical
or alphabetical.
<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 uppercase roman numbers
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 »
<ol type="I">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Try it Yourself »
<ol type="i">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Try it Yourself »
Example
<ol start="50">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Try it Yourself »
Example
<ol>
<li>Coffee</li>
<li>Tea
<ol>
<li>Black tea</li>
<li>Green tea</li>
</ol>
</li>
<li>Milk</li>
</ol>
Try it Yourself »
Note: A list item (<li>) can contain a new list, and other HTML elements, like images
and links, etc.
Chapter Summary
● 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
● Lists can be nested
● List items can contain other HTML elements
For a complete list of all available HTML tags, visit our HTML Tag Reference.
The <dl> tag defines the description list, the <dt> tag defines the term (name),
and the <dd> tag describes each term:
Example
<dl>
<dt>Coffee</dt>
<dt>Milk</dt>
</dl>
Try it Yourself »
Chapter Summary
● 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
Block-level Elements
A block-level element always starts on a new line.
A block-level element always takes up the full width available (stretches out to
the left and right as far as it can).
A block level element has a top and a bottom margin, whereas an inline element
does not.
Example
<div>Hello World</div>
Try it Yourself »
<address>
<article>
<aside>
<blockquote>
<canvas>
<dd>
<div>
<dl>
<dt>
<fieldset>
<figcaption>
<figure>
<footer>
<form>
<h1>-<h6>
<header>
<hr>
<li>
<main>
<nav>
<noscript>
<ol>
<p>
<pre>
<section>
<table>
<tfoot>
<ul>
<video>
Inline Elements
An inline element does not start on a new line.
Example
<span>Hello World</span>
Try it Yourself »
<a>
<abbr>
<acronym>
<b>
<bdo>
<big>
<br>
<button>
<cite>
<code>
<dfn>
<em>
<i>
<img>
<input>
<kbd>
<label>
<map>
<object>
<output>
<q>
<samp>
<script>
<select>
<small>
<span>
<strong>
<sub>
<sup>
<textarea>
<time>
<tt>
<var>
The <div> element has no required attributes, but style, class and id are
common.
When used together with CSS, the <div> element can be used to style blocks of
content:
Example
<div style="background-color:black;color:white;padding:20px;">
<h2>London</h2>
</div>
Try it Yourself »
The <span> element has no required attributes, but style, class and id are
common.
When used together with CSS, the <span> element can be used to style parts of
the text:
Example
Try it Yourself »
Chapter Summary
● There are two display values: block and inline
● A block-level element always starts on a new line and takes up the full
width available
● An inline element does not start on a new line and it only takes up as
much width as necessary
● The <div> element is a block-level and is often used as a container for
other HTML elements
● The <span> element is an inline container used to mark up a part of a
text, or a part of a document
HTML Tags
Tag Description
For a complete list of all available HTML tags, visit our HTML Tag Reference.
The HTML class attribute is used to specify a class for an HTML element.
TML id Attribute
❮ PreviousNext ❯
The HTML id attribute is used to specify a unique id for an HTML element.
You cannot have more than one element with the same id in an HTML
document.
The syntax for id is: write a hash character (#), followed by an id name. Then,
define the CSS properties within curly braces {}.
HTML metadata is data about the HTML document. Metadata is not displayed.
Metadata typically define the document title, character set, styles, scripts, and
other meta information.
The contents of a page title is very important for search engine optimization
(SEO)! The page title is used by search engine algorithms to decide the order
when listing pages in search results.
Example
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
Try it Yourself »
Example
<style>
h1 {color: red;}
p {color: blue;}
</style>
Try it Yourself »
The HTML <link> Element
The <link> element defines the relationship between the current document and
an external resource.
The <link> tag is most often used to link to external style sheets:
Example
Try it Yourself »
The metadata will not be displayed on the page, but are used by browsers (how
to display content or reload page), by search engines (keywords), and other
web services.
Examples
Define the character set used:
<meta charset="UTF-8">
Setting the viewport to make your website look good on all devices:
Example
<meta charset="UTF-8">
HTML Forms
❮ PreviousNext ❯
An HTML form is used to collect user input. The user input is most often sent
to a server for processing.
Example
First name:
Last name:
Try it Yourself »
<form>
form elements
</form>
The <form> element is a container for different types of input elements, such
as: text fields, checkboxes, radio buttons, submit buttons, etc.
All the different form elements are covered in this chapter: HTML Form
Elements.
The <input> Element
The HTML <input> element is the most used form element.
Type Description
All the different input types are covered in this chapter: HTML Input Types.
Text Fields
The <input type="text"> defines a single-line input field for text input.
Example
<form>
</form>
Try it Yourself »
First name:
Last name:
Note: The form itself is not visible. Also note that the default width of an input field is 20
characters.
The <label> Element
Notice the use of the <label> element in the example above.
The <label> element also help users who have difficulty clicking on very small
regions (such as radio buttons or checkboxes) - because when the user clicks
the text within the <label> element, it toggles the radio button/checkbox.
The for attribute of the <label> tag should be equal to the id attribute of the
<input> element to bind them together.
Radio Buttons
The <input type="radio"> defines a radio button.
Example
<form>
<label for="css">CSS</label><br>
<label for="javascript">JavaScript</label>
</form>
Try it Yourself »
HTML
CSS
JavaScript
Checkboxes
The <input type="checkbox"> defines a checkbox.
Example
<form>
<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">
</form>
Try it Yourself »
I have a bike
I have a car
I have a boat
The form-handler is typically a file on the server with a script for processing
input data.
Example
</form>
Try it Yourself »
First name:
Last name:
If the name attribute is omitted, the value of the input field will not be sent at
all.
Example
This example will not submit the value of the "First name" input field:
<form action="/action_page.php">
<label for="fname">First name:</label><br>
</form>
● <input>
● <label>
● <select>
● <textarea>
● <button>
● <fieldset>
● <legend>
● <datalist>
● <output>
● <option>
● <optgroup>
The <input> element can be displayed in several ways, depending on the type
attribute.
Example
Try it Yourself »
All the different values of the type attribute are covered in the next chapter:
HTML Input Types.
The <label> element also help users who have difficulty clicking on very small
regions (such as radio buttons or checkboxes) - because when the user clicks
the text within the <label> element, it toggles the radio button/checkbox.
The for attribute of the <label> tag should be equal to the id attribute of the
<input> element to bind them together.
Example
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
Try it Yourself »
Example
Try it Yourself »
Visible Values:
Use the size attribute to specify the number of visible values:
Example
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
Try it Yourself »
Example
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
Try it Yourself »
Example
</textarea>
Try it Yourself »
The rows attribute specifies the visible number of lines in a text area.
You can also define the size of the text area by using CSS:
Example
</textarea>
Try it Yourself »
Example
Try it Yourself »
Click Me!
Note: Always specify the type attribute for the button element. Different browsers may
use different default types for the button element.
Example
<form action="/action_page.php">
<fieldset>
<legend>Personalia:</legend>
</fieldset>
</form>
Try it Yourself »
Personalia:
First name:
Last name:
Users will see a drop-down list of the pre-defined options as they input data.
The list attribute of the <input> element, must refer to the id attribute of the
<datalist> element.
Example
<form action="/action_page.php">
<input list="browsers">
<datalist id="browsers">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
</form>
Try it Yourself »
Example
oninput="x.value=parseInt(a.value)+parseInt(b.value)">
100 +
<br><br>
<input type="submit">
</form>
Try it Yourself »
HTML Exercises
Exercise:
In the form below, add an empty drop down list with the name "cars".
<form action="/action_page.php">
<>
</>
</form>
Submit Answer »
Tag Description
For a complete list of all available HTML tags, visit our HTML Tag Reference.
This chapter describes the different types for the HTML <input> element.
● <input type="button">
● <input type="checkbox">
● <input type="color">
● <input type="date">
● <input type="datetime-local">
● <input type="email">
● <input type="file">
● <input type="hidden">
● <input type="image">
● <input type="month">
● <input type="number">
● <input type="password">
● <input type="radio">
● <input type="range">
● <input type="reset">
● <input type="search">
● <input type="submit">
● <input type="tel">
● <input type="text">
● <input type="time">
● <input type="url">
● <input type="week">
Example
<form>
</form>
Try it Yourself »
First name:
Last name:
Example
<form>
<label for="username">Username:</label><br>
<label for="pwd">Password:</label><br>
</form>
Try it Yourself »
Username:
Password:
The form-handler is typically a server page with a script for processing input
data.
Example
<form action="/action_page.php">
<label for="fname">First name:</label><br>
</form>
Try it Yourself »
First name:
Last name:
If you omit the submit button's value attribute, the button will get a default
text:
Example
<form action="/action_page.php">
<input type="submit">
</form>
Try it Yourself »
Example
<form action="/action_page.php">
<input type="reset">
</form>
Try it Yourself »
First name:
Last name:
If you change the input values and then click the "Reset" button, the form-data will be
reset to the default values.
Radio buttons let a user select ONLY ONE of a limited number of choices:
Example
<form>
<label for="html">HTML</label><br>
<label for="css">CSS</label><br>
<label for="javascript">JavaScript</label>
</form>
Try it Yourself »
HTML
CSS
JavaScript
Example
<form>
</form>
Try it Yourself »
Example
Try it Yourself »
Depending on browser support, a color picker can show up in the input field.
Example
<form>
</form>
Try it Yourself »
Input Type Date
The <input type="date"> is used for input fields that should contain a date.
Depending on browser support, a date picker can show up in the input field.
Example
<form>
<label for="birthday">Birthday:</label>
</form>
Try it Yourself »
You can also use the min and max attributes to add restrictions to dates:
Example
<form>
</form>
Try it Yourself »
Depending on browser support, a date picker can show up in the input field.
Example
<form>
</form>
Try it Yourself »
Some smartphones recognize the email type, and add ".com" to the keyboard to
match email input.
Example
<form>
</form>
Try it Yourself »
Example
<form>
</form>
Try it Yourself »
A hidden field let web developers include data that cannot be seen or modified
by users when a form is submitted.
A hidden field often stores what database record that needs to be updated when
the form is submitted.
Note: While the value is not displayed to the user in the page's content, it is
visible (and can be edited) using any browser's developer tools or "View Source"
functionality. Do not use hidden inputs as a form of security!
Example
<form>
</form>
Try it Yourself »
Depending on browser support, a date picker can show up in the input field.
Example
<form>
</form>
Try it Yourself »
The following example displays a numeric input field, where you can enter a
value from 1 to 5:
Example
<form>
</form>
Try it Yourself »
Input Restrictions
Here is a list of some common input restrictions:
Attribute Description
You will learn more about input restrictions in the next chapter.
The following example displays a numeric input field, where you can enter a
value from 0 to 100, in steps of 10. The default value is 30:
Example
<form>
<label for="quantity">Quantity:</label>
</form>
Try it Yourself »
Example
<form>
</form>
Try it Yourself »
Example
<form>
</form>
Try it Yourself »
Input Type Tel
The <input type="tel"> is used for input fields that should contain a
telephone number.
Example
<form>
</form>
Try it Yourself »
Depending on browser support, a time picker can show up in the input field.
Example
<form>
Try it Yourself »
Some smartphones recognize the url type, and adds ".com" to the keyboard to
match url input.
Example
<form>
</form>
Try it Yourself »
Depending on browser support, a date picker can show up in the input field.
Example
<form>
</form>
Try it Yourself »
HTML Exercises
Exercise:
In the form below, add an input field for text, with the name "username" .
<form action="/action_page.php">
<>
</form>
Submit Answer »
Tag Description
This chapter describes the different attributes for the HTML <input>
element.
Example
Input fields with initial (default) values:
<form>
</form>
Try it Yourself »
A read-only input field cannot be modified (however, a user can tab to it,
highlight it, and copy the text from it).
The value of a read-only input field will be sent when submitting the form!
Example
<form>
</form>
Try it Yourself »
The value of a disabled input field will not be sent when submitting the form!
Example
<form>
</form>
Try it Yourself »
The size Attribute
The input size attribute specifies the visible width, in characters, of an input
field.
Note: The size attribute works with the following input types: text, search, tel,
url, email, and password.
Example
<form>
<label for="pin">PIN:</label><br>
</form>
Try it Yourself »
Note: When a maxlength is set, the input field will not accept more than the
specified number of characters. However, this attribute does not provide any
feedback. So, if you want to alert the user, you must write JavaScript code.
Example
<form>
<label for="pin">PIN:</label><br>
</form>
Try it Yourself »
The min and max attributes work with the following input types: number, range,
date, datetime-local, month, time and week.
Tip: Use the max and min attributes together to create a range of legal values.
Example
<form>
</form>
Try it Yourself »
The multiple attribute works with the following input types: email, and file.
Example
<form>
</form>
Try it Yourself »
The pattern attribute works with the following input types: text, date, search,
url, tel, email, and password.
Tip: Use the global title attribute to describe the pattern to help the user.
Example
An input field that can contain only three letters (no numbers or special characters):
<form>
</form>
Try it Yourself »
The short hint is displayed in the input field before the user enters a value.
The placeholder attribute works with the following input types: text, search,
url, tel, email, and password.
Example
<form>
placeholder="123-45-678"
pattern="[0-9]{3}-[0-9]{2}-[0-9]{3}">
</form>