Tutorial: Examples in Each Chapter
Tutorial: Examples in Each Chapter
Tutorial: Examples in Each Chapter
With our online HTML editor, you can edit the HTML, and click on a button to view the result.
Example
<html>
<body>
</body>
</html>
Try it yourself »
Click on the "Try it yourself" button to see how it works
HTML Introduction
« Previous Next Chapter »
Example
<html>
<body>
</body>
</html>
Try it yourself »
What is HTML?
The purpose of a web browser (like Internet Explorer or Firefox) is to read HTML documents and display them as web pages. The
browser does not display the HTML tags, but uses the tags to interpret the content of the page:
<html>
<body>
</body>
</html>
Example Explained
The text between <html> and </html> describes the web page
The text between <body> and </body> is the visible page content
The text between <h1> and </h1> is displayed as a heading
The text between <p> and </p> is displayed as a paragraph
Editing HTML
HTML can be written and edited using many different editors like Dreamweaver and Visual Studio.
However, in this tutorial we use a plain text editor (like Notepad) to edit HTML. We believe using a plain text editor is the best way to
learn HTML.
Create Your Own Test Web
If you just want to learn HTML, skip the rest of this chapter.
If you want to create a test page on your own computer, just copy the 3 files below to your desktop.
(Right click on each link, and select "save target as" or "save link as")
mainpage.htm
page1.htm
page2.htm
After you have copied the files, you can double-click on the file called "mainpage.htm" and see your first web site in action.
We suggest you experiment with everything you learn at W3Schools by editing your web files with a text editor (like Notepad).
Note: If your test web contains HTML markup tags you have not learned, don't panic. You will learn all about it in the next chapters.
When you save an HTML file, you can use either the .htm or the .html file extension. There is no difference, it is entirely up to you.
Don't worry if the examples use tags you have not learned.
HTML Headings
Example
<h1>This is a heading</h1>
<h2>This is a heading</h2>
<h3>This is a heading</h3>
Try it yourself »
HTML Paragraphs
Example
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
Try it yourself »
HTML Links
Example
Try it yourself »
HTML Images
Example
Try it yourself »
Note: The name and the size of the image are provided as attributes.
HTML Elements
HTML Elements
An HTML element is everything from the start tag to the end tag:
* The start tag is often called the opening tag. The end tag is often called the closing tag.
HTML Element Syntax
Tip: You will learn about attributes in the next chapter of this tutorial.
Most HTML elements can be nested (can contain other HTML elements).
<body>
<p>This is my first paragraph.</p>
</body>
</html>
<body>
<p>This is my first paragraph.</p>
</body>
<html>
<body>
<p>This is my first paragraph.</p>
</body>
</html>
The <html> element defines the whole HTML document.
The element has a start tag <html> and an end tag </html>.
The element content is another HTML element (the body element).
Some HTML elements might display correctly even if you forget the end tag:
<p>This is a paragraph
<p>This is a paragraph
The example above works in most browsers, because the closing tag is considered optional.
Never rely on this. Many HTML elements will produce unexpected results and/or errors if you forget the end tag .
<br> is an empty element without a closing tag (the <br> tag defines a line break).
Tip: In XHTML, all elements must be closed. Adding a slash inside the start tag, like <br />, is the proper way of closing empty elements
in XHTML (and XML).
HTML tags are not case sensitive: <P> means the same as <p>. Many web sites use uppercase HTML tags.
W3Schools use lowercase tags because the World Wide Web Consortium (W3C) recommends lowercase in HTML 4, and demands
lowercase tags in XHTML.
HTML Attributes
Attributes provide additional information about HTML elements.
HTML Attributes
Attribute Example
HTML links are defined with the <a> tag. The link address is specified in the href attribute:
Example
Double style quotes are the most common, but single style quotes are also allowed.
Tip: In some rare situations, when the attribute value itself contains quotes, it is necessary to use single quotes: name='John
"ShotGun" Nelson'
However, the World Wide Web Consortium (W3C) recommends lowercase attributes/attribute values in their HTML 4 recommendation.
A complete list of legal attributes for each HTML element is listed in our:
Below is a list of some attributes that are standard for most HTML elements:
HTML Headings
Headings are important in HTML documents.
HTML Headings
<h1> defines the most important heading. <h6> defines the least important heading.
Example
<h1>This is a heading</h1>
<h2>This is a heading</h2>
<h3>This is a heading</h3>
Try it yourself »
Note: Browsers automatically add some empty space (a margin) before and after each heading.
Use HTML headings for headings only. Don't use headings to make text BIG or bold.
Search engines use your headings to index the structure and content of your web pages.
Since users may skim your pages by its headings, it is important to use headings to show the document structure.
H1 headings should be used as main headings, followed by H2 headings, then the less important H3 headings, and so on.
HTML Lines
Example
<p>This is a paragraph</p>
<hr />
<p>This is a paragraph</p>
<hr />
<p>This is a paragraph</p>
Try it yourself »
HTML Comments
Comments can be inserted into the HTML code to make it more readable and understandable. Comments are ignored by the browser and
are not displayed.
Example
Try it yourself »
Note: There is an exclamation point after the opening bracket, but not before the closing bracket.
Have you ever seen a Web page and wondered "Hey! How did they do that?"
To find out, right-click in the page and select "View Source" (IE) or "View Page Source" (Firefox), or similar for other browsers. This will
open a window containing the HTML code of the page.
Headings
How to display headings in an HTML document.
Hidden comments
How to insert comments in the HTML source code.
Horizontal lines
How to insert a horizontal line.
W3Schools' tag reference contains additional information about these tags and their attributes.
You will learn more about HTML tags and attributes in the next chapters of this tutorial.
Tag Description
<html> Defines an HTML document
<body> Defines the document's body
<h1> to <h6> Defines HTML headings
<hr /> Defines a horizontal line
<!--> Defines a comment
HTML Paragraphs
HTML documents are divided into paragraphs.
HTML Paragraphs
Example
<p>This is a paragraph</p>
<p>This is another paragraph</p>
Try it yourself »
Note: Browsers automatically add an empty line before and after a paragraph.
Most browsers will display HTML correctly even if you forget the end tag:
Example
<p>This is a paragraph
<p>This is another paragraph
Try it yourself »
The example above will work in most browsers, but don't rely on it. Forgetting the end tag can produce unexpected results or errors.
Note: Future version of HTML will not allow you to skip end tags.
Use the <br /> tag if you want a line break (a new line) without starting a new paragraph:
Example
Try it yourself »
The <br /> element is an empty HTML element. It has no end tag.
In XHTML, XML, and future versions of HTML, HTML elements with no end tag (closing tag) are not allowed.
Even if <br> works in all browsers, writing <br /> instead is more future proof.
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 extra spaces and extra lines when the page is displayed. Any number of lines count as one line, and any
number of spaces count as one space.
Try it yourself
W3Schools' tag reference contains additional information about HTML elements and their attributes.
Tag Description
<p> Defines a paragraph
<br /> Inserts a single line break
<html>
<body>
</body>
</html>
HTML uses tags like <b> and <i> for formatting output, like bold or italic text.
These HTML tags are called formatting tags (look at the bottom of this page for a complete reference).
<strong> or <em> means that you want the text to be rendered in a way that the user understands as "important". Today, all
major browsers render strong as bold and em as italics. However, if a browser one day wants to make a text highlighted with
the strong feature, it might be cursive for example and not bold!
Text formatting
How to format text in an HTML document.
Preformatted text
How to control the line breaks and spaces with the pre tag.
Text direction
How to change the text direction.
Quotations
How to handle long and short quotations.
CSS was introduced with HTML 4, to provide a common way to style HTML elements.
It is time consuming and not very practical to style HTML elements using the style attribute.
The preferred way to add CSS to HTML, is to put CSS syntax in separate CSS files.
However, in this HTML tutorial we will introduce you to CSS using the style attribute. This is done to simplify the examples. It also makes
it easier for you to edit the code and try it yourself.
You can learn everything about styles and CSS in our CSS Tutorial.
Example
<html>
<body style="background-color:yellow">
<h2 style="background-color:red">This is a heading</h2>
<p style="background-color:green">This is a paragraph.</p>
</body>
</html>
Try it yourself »
The font-family, color, and font-size properties defines the font, color, and size of the text in an element:
Example
<html>
<body>
<h1 style="font-family:verdana">A heading</h1>
<p style="font-family:arial;color:red;font-size:20px;">A paragraph.</p>
</body>
</html>
Try it yourself »
Example
<html>
<body>
<h1 style="text-align:center">This is a heading</h1>
<p>The heading above is aligned to the center of this page.</p>
</body>
</html>
Try it yourself »
In HTML 4, several tags and attributes are deprecated. Deprecated means that they will not be supported in future versions of HTML and
XHTML.
Tags Description
<center> Deprecated. Defines centered content
<font> and <basefont> Deprecated. Defines HTML fonts
<s> and <strike> Deprecated. Defines strikethrough text
<u> Deprecated. Defines underlined text
Attributes Description
align Deprecated. Defines the alignment of text
bgcolor Deprecated. Defines the background color
color Deprecated. Defines the text color
HTML Links
« Previous Next Chapter »
Links are found in nearly all Web pages. Links allow users to click their way from page to page.
HTML links
How to create links in an HTML document.
A hyperlink (or link) is a word, group of words, or image that you can click on to jump to a new document or a new section within the
current document.
When you move the cursor over a link in a Web page, the arrow will turn into a little hand.
Example
<a href="http://www.w3schools.com/">Visit W3Schools</a>
Tip: The "Link text" doesn't have to be text. You can link from an image or any other HTML element.
The example below will open the linked document in a new browser window:
Example
Try it yourself »
HTML Links - The name Attribute
Note:
The upcoming HTML5 standard suggest using the id attribute instead of the name attribute for specifying the name of an anchor.
Using the id attribute actually works also for HTML4 in all modern browsers.
Bookmarks are not displayed in any special way. They are invisible to the reader.
Example
Create a link to the "Useful Tips Section" inside the same document:
Or, create a link to the "Useful Tips Section" from another page:
<a href="http://www.w3schools.com/html_links.htm#tips">
Visit the Useful Tips Section</a>
Note: Always add a trailing slash to subfolder references. If you link like this: href="http://www.w3schools.com/html", you will generate
two requests to the server, the server will first add a slash to the address, and then create a new request like this:
href="http://www.w3schools.com/html/".
Tip: Named anchors are often used to create "table of contents" at the beginning of a large document. Each chapter within the
document is given a named anchor, and links to each of these anchors are put at the top of the document.
Tip: If a browser does not find the named anchor specified, it goes to the top of the document. No error occurs.
More Examples
An image as a link
How to use an image as a link.
HTML Images
« Previous Next Chapter »
Example
Try it yourself »
<html>
<body>
</body>
</html>
Insert images
How to insert images into an HTML document.
The <img> tag is empty, which means that it contains attributes only, and has no closing tag.
To display an image on a page, you need to use the src attribute. Src stands for "source". The value of the src attribute is the URL of the
image you want to display.
The URL points to the location where the image is stored. An image named "boat.gif", located in the "images" directory on
"www.w3schools.com" has the URL: http://www.w3schools.com/images/boat.gif.
The browser displays the image where the <img> tag occurs in the document. If you put an image tag between two paragraphs, the
browser shows the first paragraph, then the image, and then the second paragraph.
The required alt attribute specifies an alternate text for an image, if the image cannot be displayed.
The alt attribute provides alternative information for an image if a 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).
The height and width attributes are used to specify the height and width of an image.
Tip: It is a good practice to specify both the height and width attributes for an image. If these attributes are set, the space required for
the image is reserved when the page is loaded. However, without these attributes, the browser does not know the size of the image. The
effect will be that the page layout will change during loading (while the images load).
Note: If an HTML file contains ten images - eleven files are required to display the page right. Loading images take time, so my best
advice is: Use images carefully.
Note: When a web page is loaded, it is the browser, at that moment, that actually gets the image from a web server and inserts it into
the page. Therefore, make sure that the images actually stay in the same spot in relation to the web page, otherwise your visitors will
get a broken link icon. The broken link icon is shown if the browser cannot find the image.
More Examples
Aligning images
How to align an image within the text.
HTML Tables
« Previous Next Chapter »
HTML Tables
Apples 44%
Bananas 23%
Oranges 13%
Other 10%
Tables
How to create tables in an HTML document.
Table borders
How to specify different table borders.
HTML Tables
A table is divided into rows (with the <tr> tag), and each row is divided into data cells (with the <td> tag). td stands for "table data,"
and holds the content of a data cell. A <td> tag can contain text, links, images, lists, forms, other tables, etc.
Table Example
<table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>
<table border="1">
<tr>
<td>Row 1, cell 1</td>
<td>Row 1, cell 2</td>
</tr>
</table>
All major browsers will display the text in the <th> element as bold and centered.
<table border="1">
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>
Header 1 Header 2
row 1, cell 1 row 1, cell 2
row 2, cell 1 row 2, cell 2
HTML Lists
« Previous Next Chapter »
The most common HTML lists are ordered and unordered lists:
HTML Lists
An unordered list:
An ordered list:
Unordered list
How to create an unordered list in an HTML document.
Ordered list
How to create an ordered list in an HTML document.
An unordered list starts with the <ul> tag. Each list item starts with the <li> tag.
The list items are marked with bullets (typically small black circles).
<ul>
<li>Coffee</li>
<li>Milk</li>
</ul>
Coffee
Milk
An ordered list starts with the <ol> tag. Each list item starts with the <li> tag.
<ol>
<li>Coffee</li>
<li>Milk</li>
</ol>
1. Coffee
2. Milk
The <dl> tag is used in conjunction with <dt> (defines the item in the list) and <dd> (describes the item in the list):
<dl>
<dt>Coffee</dt>
<dd>- black hot drink</dd>
<dt>Milk</dt>
<dd>- white cold drink</dd>
</dl>
Coffee
Milk
Tip: Inside a list item you can put text, line breaks, images, links, other lists, etc.
More Examples
Nested list
Demonstrates how you can nest lists.
Nested list 2
Demonstrates a more complicated nested list.
Definition list
Demonstrates a definition list.
Try-It-Yourself Examples
HTML Forms
A form can contain input elements like text fields, checkboxes, radio-buttons, submit buttons and more. A form can also contain select
lists, textarea, fieldset, legend, and label elements.
<form>
.
input elements
.
</form>
An input element can vary in many ways, depending on the type attribute. An input element can be of type text field, checkbox,
password, radio button, submit button, and more.
Text Fields
<input type="text" /> defines a one-line input field that a user can enter text into:
<form>
First name: <input type="text" name="firstname" /><br />
Last name: <input type="text" name="lastname" />
</form>
First name:
Last name:
Note: The form itself is not visible. Also note that the default width of a text field is 20 characters.
Password Field
<form>
Password: <input type="password" name="pwd" />
</form>
Password:
Note: The characters in a password field are masked (shown as asterisks or circles).
Radio Buttons
<input type="radio" /> defines a radio button. Radio buttons let a user select ONLY ONE one of a limited number of choices:
<form>
<input type="radio" name="sex" value="male" /> Male<br />
<input type="radio" name="sex" value="female" /> Female
</form>
Male
Female
Checkboxes
<input type="checkbox" /> defines a checkbox. Checkboxes let a user select ONE or MORE options of a limited number of choices.
<form>
<input type="checkbox" name="vehicle" value="Bike" /> I have a bike<br />
<input type="checkbox" name="vehicle" value="Car" /> I have a car
</form>
I have a bike
I have a car
Submit Button
A submit button is used to send form data to a server. The data is sent to the page specified in the form's action attribute. The file
defined in the action attribute usually does something with the received input:
If you type some characters in the text field above, and click the "Submit" button, the browser will send your input to a page called
"html_form_action.asp". The page will show you the received input.
Radio buttons
How to create radio buttons.
Checkboxes
How to create checkboxes. A user can select or unselect a checkbox.
Textarea
How to create a multi-line text input control. In a text-area the user can write an unlimited number of characters.
Create a button
How to create a button.
Form Examples
With frames, several Web pages can be displayed in the same browser window.
Try-It-Yourself Examples
Vertical frameset
How to make a vertical frameset with three different documents.
Horizontal frameset
How to make a horizontal frameset with three different documents.
HTML Frames
With frames, you can display more than one HTML document in the same browser window. Each HTML document is called a frame, and
each frame is independent of the others.
The frameset element holds one or more frame elements. Each frame element can hold a separate document.
The frameset element states HOW MANY columns or rows there will be in the frameset, and HOW MUCH percentage/pixels of space will
occupy each of them.
The <frame> tag defines one particular window (frame) within a frameset.
The first column is set to 25% of the width of the browser window. The second column is set to 75% of the width of the browser window.
The document "frame_a.htm" is put into the first column, and the document "frame_b.htm" is put into the second column:
<frameset cols="25%,75%">
<frame src="frame_a.htm" />
<frame src="frame_b.htm" />
</frameset>
Note: The frameset column size can also be set in pixels (cols="200,500"), and one of the columns can be set to use the remaining
space, with an asterisk (cols="25%,*").
Basic Notes - Useful Tips
Tip: If a frame has visible borders, the user can resize it by dragging the border. To prevent a user from doing this, you can add
noresize="noresize" to the <frame> tag.
Note: Add the <noframes> tag for browsers that do not support frames.
Important: You cannot use the <body></body> tags together with the <frameset></frameset> tags! However, if you add a
<noframes> tag containing some text for browsers that do not support frames, you will have to enclose the text in <body></body>
tags! See how it is done in the first example below.
More Examples
Nested framesets
How to create a frameset with three documents, and how to mix them in rows and columns.
Navigation frame
How to make a navigation frame. The navigation frame contains a list of links with the second frame as the target. The file called
"tryhtml_contents.htm" contains three links. The source code of the links:
<a href ="frame_a.htm" target ="showframe">Frame a</a><br>
<a href ="frame_b.htm" target ="showframe">Frame b</a><br>
<a href ="frame_c.htm" target ="showframe">Frame c</a>
The second frame will show the linked document.
HTML Iframes
« Previous Next Chapter »
The height and width attributes are used to specify the height and width of the iframe.
The attribute values are specified in pixels by default, but they can also be in percent (like "80%").
Example
Try it yourself »
<html>
<body>
</body>
</html>
The frameborder attribute specifies whether or not to display a border around the iframe.
Example
Try it yourself »
<html>
<body>
</body>
</html>
The target attribute of a link must refer to the name attribute of the iframe:
Example
<iframe src="demo_iframe.htm" name="iframe_a"></iframe>
<p><a href="http://www.w3schools.com" target="iframe_a">W3Schools.com</a></p>
Try it yourself »
<html>
<body>
<p><b>Note:</b> Because the target of the link matches the name of the iframe, the link will open in the iframe.</p>
</body>
</html>
HTML Colors
« Previous Next Chapter »
Color Values
HTML colors are defined using a hexadecimal notation (HEX) for the combination of Red, Green, and Blue color values (RGB).
The lowest value that can be given to one of the light sources is 0 (in HEX: 00). The highest value is 255 (in HEX: FF).
HEX values are specified as 3 pairs of two-digit numbers, starting with a # sign.
Color Values
#000000 rgb(0,0,0)
#FF0000 rgb(255,0,0)
#00FF00 rgb(0,255,0)
#0000FF rgb(0,0,255)
#FFFF00 rgb(255,255,0)
#00FFFF rgb(0,255,255)
#FF00FF rgb(255,0,255)
#C0C0C0 rgb(192,192,192)
#FFFFFF rgb(255,255,255)
Try it yourself »
The combination of Red, Green, and Blue values from 0 to 255, gives more than 16 million different colors (256 x 256 x 256).
If you look at the color table below, you will see the result of varying the red light from 0 to 255, while keeping the green and blue light
at zero.
To see the full list of color mixes when RED varies from 0 to 255, click on one of the HEX or RGB values below.
Shades of Gray
Gray colors are created by using an equal amount of power to all of the light sources.
To make it easier for you to select the correct shade, we have created a table of gray shades for you:
Some years ago, when computers supported max 256 different colors, a list of 216 "Web Safe Colors" was suggested as a Web standard,
reserving 40 fixed system colors.
The 216 cross-browser color palette was created to ensure that all computers would display the colors correctly when running a 256
color palette.
This is not important today, since most computers can display millions of different colors. Anyway, here is the list:
147 color names are defined in the HTML and CSS color specification (17 standard colors plus 130 more). The table below lists them all,
along with their hexadecimal values.
Tip: The 17 standard colors are: aqua, black, blue, fuchsia, gray, grey, green, lime, maroon, navy, olive, purple, red, silver, teal,
white, and yellow.
Click on a color name (or a hex value) to view the color as the background-color along with different text colors:
HTML Quick List from W3Schools. Print it, fold it, and put it in your pocket.
<html>
<head>
<title>Title of document goes here</title>
</head>
<body>
Visible text goes here...
</body>
</html>
Heading Elements
<h1>Largest Heading</h1>
<h2> . . . </h2>
<h3> . . . </h3>
<h4> . . . </h4>
<h5> . . . </h5>
<h6>Smallest Heading</h6>
Text Elements
<p>This is a paragraph</p>
<br /> (line break)
<hr /> (horizontal rule)
<pre>This text is preformatted</pre>
Logical Styles
<em>This text is emphasized</em>
<strong>This text is strong</strong>
<code>This is some computer code</code>
Physical Styles
<b>This text is bold</b>
<i>This text is italic</i>
Links
Ordinary link: <a href="http://www.example.com/">Link-text goes here</a>
Image-link: <a href="http://www.example.com/"><img src="URL" alt="Alternate Text" /></a>
Mailto link: <a href="mailto:webmaster@example.com">Send e-mail</a>
A named anchor:
<a name="tips">Tips Section</a>
<a href="#tips">Jump to the Tips Section</a>
Unordered list
<ul>
<li>Item</li>
<li>Item</li>
</ul>
Ordered list
<ol>
<li>First item</li>
<li>Second item</li>
</ol>
Definition list
<dl>
<dt>First term</dt>
<dd>Definition</dd>
<dt>Next term</dt>
<dd>Definition</dd>
</dl>
Tables
<table border="1">
<tr>
<th>Tableheader</th>
<th>Tableheader</th>
</tr>
<tr>
<td>sometext</td>
<td>sometext</td>
</tr>
</table>
Frames
<frameset cols="25%,75%">
<frame src="page1.htm" />
<frame src="page2.htm" />
</frameset>
Forms
<form action="http://www.example.com/test.asp" method="post/get">
<select>
<option>Apples</option>
<option selected="selected">Bananas</option>
<option>Cherries</option>
</select>
</form>
Entities
< is the same as <
> is the same as >
© is the same as ©
Other Elements
<blockquote>
Text quoted from a source.
</blockquote>
<address>
Written by W3Schools.com<br />
<a href="mailto:us@example.org">Email us</a><br />
Address: Box 564, Disneyland<br />
Phone: +12 34 56 78
</address>
HTML Doctypes
« Previous Next Chapter »
A doctype declaration refers to the rules for the markup language, so that the browsers render the content
correctly.
Example
<body>
The content of the document......
</body>
</html>
The doctype declaration is not an HTML tag; it is an instruction to the web browser about what version of the markup language the page
is written in.
The doctype declaration refers to a Document Type Definition (DTD). The DTD specifies the rules for the markup language, so that the
browsers render the content correctly.
The doctype declaration should be the very first thing in an HTML document, before the <html> tag.
Tip: Always add a doctype to your pages. This helps the browsers to render the page correctly!
This DTD contains all HTML elements and attributes, but does NOT INCLUDE presentational or deprecated elements (like font and
center). Framesets are not allowed:
This DTD contains all HTML elements and attributes, INCLUDING presentational and deprecated elements (like font). Framesets are not
allowed:
This DTD is equal to HTML 4.01 Transitional, but allows the use of frameset content:
Look at our table of all HTML/XHTML elements, and which DTD each element appear in.
Use W3C's Validator to check that you have written a valid HTML / XHTML document!
<!DOCTYPE> Defines the document type. This declaration goes before the <html> start tag
HTML Styles
« Previous Next Chapter »
In HTML 4.0, all formatting can be removed from the HTML document, and stored in a style sheet.
Try-It-Yourself Examples
When a browser reads a style sheet, it will format the document according to it.
An external style sheet is ideal when the style is applied to many pages. With an external style sheet, you can change the look of an
entire Web site by changing one file. Each page must link to the style sheet using the <link> tag. The <link> tag goes inside the
<head> section:
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
</head>
An internal style sheet can be used if one single document has a unique style. Internal styles are defined in the <head> section of an
HTML page, by using the <style> tag, like this:
<head>
<style type="text/css">
body {background-color:yellow}
p {color:blue}
</style>
</head>
Inline Styles
An inline style can be used if a unique style is to be applied to one single occurrence of an element.
To use inline styles, use the style attribute in the relevant tag. The style attribute can contain any CSS property. The example below
shows how to change the text color and the left margin of a paragraph:
Try-It-Yourself Examples
The head element is a container for all the head elements. Elements inside <head> can include scripts, instruct the browser where to
find style sheets, provide meta information, and more.
The following tags can be added to the head section: <title>, <base>, <link>, <meta>, <script>, and <style>.
<body>
The content of the document......
</body>
</html>
The <base> tag specifies a default address or a default target for all links on a page:
<head>
<base href="http://www.w3schools.com/images/" />
<base target="_blank" />
</head>
The <link> tag defines the relationship between a document and an external resource.
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
</head>
The <style> tag is used to define style information for an HTML document.
Inside the style element you specify how HTML elements should render in a browser:
<head>
<style type="text/css">
body {background-color:yellow}
p {color:blue}
</style>
</head>
HTML Meta
« Previous Next Chapter »
Try-It-Yourself Examples
Document description
Use the meta element to describe the document.
Document keywords
Use the meta element to define the keywords of a document.
Redirect a user
How to redirect a user to a new web address.
The <meta> tag provides metadata about the HTML document. Metadata will not be displayed on the page, but will be machine
parsable.
Meta elements are typically used to specify page description, keywords, author of the document, last modified, and other metadata.
The metadata can be used by browsers (how to display content or reload page), search engines (keywords), or other web services.
Some search engines will use the name and content attributes of the meta element to index your pages.
The intention of the name and content attributes is to describe the content of a page.
Note: A lot of webmasters have used <meta> tags for spamming, like repeating keywords (or using wrong keywords) for higher
ranking. Therefore, most search engines have stopped using <meta> tags to index/rank pages.
HTML Scripts
« Previous Next Chapter »
Try-It-Yourself Examples
Insert a script
How to insert a script into an HTML document.
The script element either contains scripting statements or it points to an external script file through the src attribute.
The required type attribute specifies the MIME type of the script.
Common uses for JavaScript are image manipulation, form validation, and dynamic changes of content.
Example
<script type="text/javascript">
document.write("Hello World!")
</script>
Try it yourself »
<html>
<body>
<script type="text/javascript">
document.write("Hello World!")
</script>
</body>
</html>
Hello World!
Tip: To learn more about JavaScript, visit our JavaScript tutorial!
The <noscript> tag is used to provide an alternate content for users that have disabled scripts in their browser or have a browser that
doesn’t support client-side scripting.
The noscript element can contain all the elements that you can find inside the body element of a normal HTML page.
The content inside the noscript element will only be displayed if scripts are not supported, or are disabled in the user’s browser:
Example
<script type="text/javascript">
document.write("Hello World!")
</script>
<noscript>Sorry, your browser does not support JavaScript!</noscript>
Try it yourself »
<html>
<body>
<script type="text/javascript">
document.write("Hello World!")
</script>
<noscript>Sorry, your browser does not support JavaScript!</noscript>
<p>A browser without support for JavaScript will show the text in the noscript element.</p>
</body>
</html>
Hello World!
A browser without support for JavaScript will show the text in the noscript element
HTML Entities
« Previous Next Chapter »
HTML Entities
It is not possible to use the less than (<) or greater than (>) signs in your text, because the browser will mix them with tags.
To actually display reserved characters, we must use character entities in the HTML source code.
A character entity looks like this:
&entity_name;
OR
&#entity_number;
Tip: The advantage of using an entity name, instead of a number, is that the name is easier to remember. However, the
disadvantage is that browsers may not support all entity names (the support for entity numbers is very good).
Non-breaking Space
Browsers will always truncate spaces in HTML pages. If you write 10 spaces in your text, the browser will remove 9 of them, before
displaying the page. To add spaces to your text, you can use the character entity.
A URL can be composed of words, such as "w3schools.com", or an Internet Protocol (IP) address:
192.68.20.50. Most people enter the name of the website when surfing, because names are easier to
remember than numbers.
URL - Uniform Resource Locator
When you click on a link in an HTML page, an underlying <a> tag points to an address on the world wide web.
A Uniform Resource Locator (URL) is used to address a document (or other data) on the world wide web.
scheme://host.domain:port/path/filename
Explanation:
scheme - defines the type of Internet service. The most common type is http
host - defines the domain host (the default host for http is www)
domain - defines the Internet domain name, like w3schools.com
:port - defines the port number at the host (the default port number for http is 80)
path - defines a path at the server (If omitted, the document must be stored at the root directory of the web site)
filename - defines the name of a document/resource
Scheme Short for.... Which pages will the scheme be used for...
http HyperText Transfer Protocol Common web pages starts with http://. Not encrypted
https Secure HyperText Transfer Protocol Secure web pages. All information exchanged are encrypted
ftp File Transfer Protocol For downloading or uploading files to a website. Useful for domain
maintenance
file A file on your computer
URL encoding converts characters into a format that can be transmitted over the Internet.
URL Encoding
URLs can only be sent over the Internet using the ASCII character-set.
Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format.
URL encoding replaces non ASCII characters with a "%" followed by two hexadecimal digits.
URLs cannot contain spaces. URL encoding normally replaces a space with a + sign.
Try It Yourself
If you click the "Submit" button below, the browser will URL encode the input before it is sent to the server. A page at the server will
display the received input.
To make your web site visible to the world, you'll have to store it on a web server.
Hosting your web site on your own server is always an option. Here are some points to consider:
Hardware Expenses
To run a "real" web site, you will have to buy some powerful server hardware. Don't expect that a low cost PC will do the job. You will
also need a permanent (24 hours a day ) high-speed connection.
Software Expenses
Remember that server-licenses often are higher than client-licenses. Also note that server-licenses might have limits on number of
users.
Labor Expenses
Don't expect low labor expenses. You have to install your own hardware and software. You also have to deal with bugs and viruses, and
keep your server constantly running in an environment where "everything could happen".
Using an Internet Service Provider
Most small companies store their web site on a server provided by an ISP. Here are some advantages:
Connection Speed
Powerful Hardware
ISPs often have powerful web servers that can be shared by several companies. You can also expect them to have an effective load
balancing, and necessary backup servers.
ISPs are specialists on web hosting. Expect their servers to have more than 99% up time, the latest software patches, and the best virus
protection.
Make sure your ISP offers 24-hours support. Don't put yourself in a situation where you cannot fix critical problems without having to
wait until the next working day. Toll-free phone could be vital if you don't want to pay for long distance calls.
Daily Backup
Make sure your ISP runs a daily backup routine, otherwise you may lose some valuable data.
Traffic Volume
Study the ISP's traffic volume restrictions. Make sure that you don't have to pay a fortune for unexpected high traffic if your web site
becomes popular.
Study the ISP's bandwidth and content restrictions. If you plan to publish pictures or broadcast video or sound, make sure that you can.
E-mail Capabilities
Make sure your ISP supports the e-mail capabilities you need.
Database Access
If you plan to use data from databases on your web site, make sure your ISP supports the database access you need.
HTML Summary
This tutorial has taught you how to use HTML to create your own web site.
HTML is the universal markup language for the Web. HTML lets you format text, add graphics, create links, input forms, frames and
tables, etc., and save it all in a text file that any browser can read and display.
The key to HTML is the tags, which indicates what content is coming up.
XHTML
If you want to learn more about XHTML, please visit our XHTML tutorial.
CSS
CSS is used to control the style and layout of multiple Web pages all at once.
With CSS, all formatting can be removed from the HTML document and stored in a separate file.
CSS gives you total control of the layout, without messing up the document content.
To learn how to create style sheets, please visit our CSS tutorial.
Cell padding (control the white space between cell content and the borders
<html>
<body>
<h4>Without cellpadding:</h4>
<table border="1">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
<h4>With cellpadding:</h4>
<table border="1"
cellpadding="10">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
</body>
</html>
Without cellpadding:
First Row
Second Row
With cellpadding:
First Row
Second Row
Cell spacing (control the distance between cells)
<html>
<body>
<h4>Without cellspacing:</h4>
<table border="1">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
<h4>With cellspacing:</h4>
<table border="1"
cellspacing="10">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
</body>
</html>
Without cellspacing:
First Row
Second Row
With cellspacing:
First Row
Second Row
<html>
<body>
<form action="">
</form>
<p><b>Note:</b> The form itself is not visible. Also note that the default width of a text field is 20
characters.</p>
</body>
</html>
First name:
Last name:
Note: The form itself is not visible. Also note that the default width of a text field is 20
characters.
<html>
<body>
<form action="">
</form>
<p><b>Note:</b> The characters in a password field are masked (shown as asterisks or circles).</p>
</body>
</html>
Username:
Password:
Note: The characters in a password field are masked (shown as asterisks or circles).
<html>
<body>
<form action="">
</form>
</body>
</html>
I have a bike
I have a car
<html>
<body>
<form action="">
</form>
<p><b>Note:</b> When a user clicks on a radio-button, it becomes checked, and all other radio-
buttons with equal name become unchecked.</p>
</body>
</html>
Male
Female
Note: When a user clicks on a radio-button, it becomes checked, and all other radio-buttons
with equal name become unchecked.
<html>
<body>
<form action="">
<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
</form>
</body>
</html>
Volvo
<html>
<body>
<form action="">
<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat" selected="selected">Fiat</option>
<option value="audi">Audi</option>
</select>
</form>
</body>
</html>
Fiat
<html>
<body>
<form action="">
<input type="button" value="Hello world!">
</form>
</body>
</html>
<html>
<body>
<form action="">
<fieldset>
<legend>Personal information:</legend>
Name: <input type="text" size="30" /><br />
E-mail: <input type="text" size="30" /><br />
Date of birth: <input type="text" size="10" />
</fieldset>
</form>
</body>
</html>
Personal information:Name:
E-mail:
Date of birth:
<html>
<body>
<p>If you click the "Submit" button, the form-data will be sent to a page
called "html_form_action.asp".</p>
</body>
</html>
Mickey
First name:
Mouse
Last name:
Submit
If you click the "Submit" button, the form-data will be sent to a page called
"html_form_action.asp".