Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
HTML Example
<! DOCTYPE html>
<html>
<body>
</body>
</html>
Example Explained
What is HTML?
HTML Tags
HTML tags are keywords (tag names) surrounded by angle brackets like
<html>
HTML tags normally come in pairs like <b> and </b>
The first tag in a pair is the start tag, the second tag is the end tag
The end tag is written like the start tag, with a forward slash before the tag
name
Start and end tags are also called opening tags and closing tags
<tagname>content</tagname>
HTML Elements
"HTML tags" and "HTML elements" are often used to describe the same thing.
But strictly speaking, an HTML element is everything between the start tag and the end tag,
including the tags:
HTML Element:
<p>This is a paragraph.</p>
Web Browsers
The purpose of a web browser (such as Google Chrome, Internet Explorer, Firefox, Safari) is to
read HTML documents and display them as web pages.
The browser does not display the HTML tags, but uses the tags to determine how the content of
the HTML page is to be presented/displayed to the user:
<html>
<body>
<h1>This a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
HTML Versions
Since the early days of the web, there have been many versions of HTML:
Version Year
HTML 1991
HTML+ 1993
XHTML 2000
HTML5 2012
The <!DOCTYPE> declaration helps the browser to display a web page correctly.
There are many different documents on the web, and a browser can only display an HTML page
100% correctly if it knows the HTML type and version used.
HTML Editors
Adobe Dreamweaver
Microsoft Expression Web
CoffeeCup HTML Editor
However, for learning HTML we recommend a text editor like Notepad (PC) or TextEdit (Mac).
We believe using a simple text editor is a good way to learn HTML.
Follow the 4 steps below to create your first web page with Notepad.
Step 1: Start Notepad
Start
All Programs
Accessories
Notepad
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.
Start your web browser and open your html file from the File, Open menu, or just browse the
folder and double-click your HTML file.
Don't worry if the examples use tags you have not learned.
Example
<h1>This is a heading</h1>
<h2>This is a heading</h2>
<h3>This is a heading</h3>
HTML Paragraphs
Example
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
HTML Links
Try it yourself
Example
<img src="w3schools.jpg" alt="W3Schools.com" width="104" height="142">
Note: The filename and the size of the image are provided as attributes.
HTML Elements
HTML documents are defined by HTML elements.
HTML Elements
An HTML element is everything from the start tag to the end tag:
<br>
* 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>
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
HTML Attributes
Attribute Example
HTML links are defined with the <a> tag. The link address is specified in the href attribute:
Example
<a href="http://www.w3schools.com">This is a link</a>
Always Quote Attribute Values
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: HTML
Tag Reference.
Below is a list of some attributes that can be used on any HTML element:
Attribute Description
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
Try it yourself
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 Paragraphs
HTML documents are divided into paragraphs.
HTML Paragraphs
Example
<p>This is a paragraph</p>
<p>This is another paragraph</p>
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
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
<p>This is<br>a para<br>graph with line breaks</p>
The <br> element is an empty HTML element. It has no end tag.
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.
W3Schools' tag reference contains additional information about HTML elements and their
attributes.
Tag Description
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!
HTML Comments
Comment tags <!-- and --> are used to insert comments in HTML.
You can add comments to your HTML source by using the following syntax:
Comments are not displayed by the browser, but they can help document your HTML.
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 at the moment
<img border="0" src="/images/pulpit.jpg" alt="Pulpit rock"
width="304" height="228">
-->
HTML comments tags can also be generated by various HTML software programs.
For example the <!--webbot bot--> tags which are wrapped inside HTML comments by
FrontPage.
Conditional Comments
You can add conditional comments to your HTML document by using the following syntax:
Example
<!--[if IE 5]>This is IE 5<br><![endif]-->
<!--[if IE 6]>This is IE 6<br><![endif]-->
<!--[if IE 7]>This is IE 7<br><![endif]-->
<!--[if IE 8]>This is IE 8<br><![endif]-->
<!--[if IE 9]>This is IE 9<br><![endif]-->
HTML Links
Links are found in nearly all Web pages. Links allow users to click their way from page to page.
A hyperlink (or link) is a word, group of words, or image that you can click on to jump to
another document.
When you move the cursor over a link in a Web page, the arrow will turn into a little hand.
The most important attribute of the <a> element is the href attribute, which indicates the link's
destination.
Example
<a href="http://www.w3schools.com/">Visit W3Schools</a>
Tip: The "Link text" doesn't have to be text. It can be an image or any other HTML element.
The example below will open the linked document in a new browser window or a new tab:
Example
<a href="http://www.w3schools.com/" target="_blank">Visit W3Schools!</a>
Tip: 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/".
More Examples
An image as a link
How to use an image as a link.
HTML Head
<title> - Define a title for an HTML document
Use the <title> tag to define a title for a document.
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
The content of the document......
</body>
</html>
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>
<base> Defines a default address or a default target for all links on a page
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 CSS in our CSS Tutorial.
Inline Styles
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:
<p style="color:blue;margin-left:20px;">This is a
paragraph.</p>
HTML Style Example - Background Color
Example
<!DOCTYPE html>
<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>
The font-family, color, and font-size properties defines the font, color, and size of the text in an
element:
Example
<!DOCTYPE html>
<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
The font-family, color, and font-size properties make the old <font> tag obsolete.
HTML Style Example - Text Alignment
Example
<!DOCTYPE html>
<html>
<body>
<h1 style="text-align:center;">Center-aligned heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Try it yourself
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>
body {background-color:yellow;}
p {color:blue;}
</style>
</head>
.inquiry_form textarea{ padding:3px; margin:3px; color:#959595; font-
family:Arial, Helvetica, sans-serif; font-size:11px;}
In HTML 4, several tags and attributes were used to style documents. These tags are not
supported in newer versions of HTML.
Avoid using the elements: <font>, <center>, and <strike>, and the attributes: color and bgcolor.
HTML Images
HTML Images - The <img> Tag and the Src Attribute
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.
The attribute values are specified in pixels by default:
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 takes 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
<script type="text/javascript">
function showmenu(elmnt)
{
document.getElementById(elmnt).style.visibility="visible
";
}
function hidemenu(elmnt)
{
document.getElementById(elmnt).style.visibility="hidden
";
}
Html Form
<html>
<head>
</head>
<body>
<form method="post" action="">
Enter Name:<input type="text" name="txt"><br>
Enter Password:<input type="password"
name="pwd"><br>
Select Gender:<input type="radio" name="gender"
value="male">male
<input type="radio" name="gender"
value="female">female<br>
Select Qualification:<input type="checkbox"
name="qual" value="MBA">MBA
<input type="checkbox" name="qual"
value="MCA">MCA
<input type="checkbox" name="qual" value="BBA">BBA
<input type="checkbox" name="qual"
value="MBA">MBA<br>
Select City:<select name="city">
<option value="delhi">delhi</option>
<option value="noida">noida</option>
<option value="delhi">delhi</option>
<option value="delhi">delhi</option>
</option>
</select><br>
Select City:<select name="city1" size="2" multiple>
<option value="delhi">delhi</option>
<option value="noida">noida</option>
<option value="delhi">delhi</option>
<option value="delhi">delhi</option>
</option>
</select><br>
Enter Address<textarea rows="3"
cols="15"></textarea><br>
Upload Resume:<input type="file" name="resume"><br>
<input type="submit">
</body>
</html>
Html Table
<html>
<body>
<table border="1" cellpadding="10" cellspacing="0"
width="300">
<tr>
<th>Name</th>
<th>Contact</th>
<th>Mobile</th>
</tr>
<tr>
<td rowspan="2">xyz</td>
<td>1234</td>
<td>22222</td>
</tr>
<tr>
<td colspan="2">1234</td>
</tr>
</table>
</body>
</html>
Next
Example
Preformatted text:
<pre>
Text in a pre element
is displayed in a fixed-width
font, and it preserves
both spaces and
line breaks
</pre>
Try it Yourself
Browser Support
Text in a <pre> element is displayed in a fixed-width font (usually Courier), and it preserves
both spaces and line breaks.
Tip: Use the <pre> element when displaying text with unusual formatting, or some sort of
computer code.
<form name="contact_form"
action="http://www.bvcorphr4u.com/postr
esume_action.php"
enctype="multipart/form-data"
method="post" onsubmit="return
ValidateForm(this);">
Next Chapter
color
date
datetime
datetime-local
email
month
number
range
search
tel
time
url
week
Any input types, not supported by your browser, will behave as regular text types.
Input Type: number
The number type is used for input fields that should contain a numeric value.
Depending on browser support, the restrictions can apply to the input field.
Example
<form>
Quantity (between 1 and 5):
<input type="number" name="quantity" min="1" max="5">
</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.
Example
<form>
Quantity:
<input type="number" name="points" min="0" max="100" step="10" value="30">
</form>
Try it Yourself
Depending on browser support, a date picker can show up in the input field.
Example
<form>
Birthday:
<input type="date" name="bday">
</form>
Try it Yourself
Try it Yourself
Depending on browser support, a color picker can show up in the input field.
Example
<form>
Select your favorite color:
<input type="color" name="favcolor">
</form>
Try it Yourself
Depending on browser support, the input field can be displayed as a slider control.
Example
<form>
<input type="range" name="points" min="0" max="10">
</form>
Try it Yourself
You can use the following attributes to specify restrictions: min, max, step, value.
Depending on browser support, a date picker can show up in the input field.
Example
<form>
Birthday (month and year):
<input type="month" name="bdaymonth">
</form>
Try it Yourself
Depending on browser support, a date picker can show up in the input field.
Example
<form>
Select a week:
<input type="week" name="week_year">
</form>
Try it Yourself
Depending on browser support, a time picker can show up in the input field.
Example
<form>
Select a time:
<input type="time" name="usr_time">
</form>
Try it Yourself
Depending on browser support, a date picker can show up in the input field.
Example
<form>
Birthday (date and time):
<input type="datetime" name="bdaytime">
</form>
Try it Yourself
Input Type: datetime-local
The datetime-local type allows the user to select a date and time (no time zone).
Depending on browser support, a date picker can show up in the input field.
Example
<form>
Birthday (date and time):
<input type="datetime-local" name="bdaytime">
</form>
Try it Yourself
Depending on browser support, the e-mail address can be automatically validated when
submitted.
Some smartphones recognize the email type, and adds ".com" to the keyboard to match email
input.
Example
<form>
E-mail:
<input type="email" name="email">
</form>
Try it Yourself
Input Type: search
The search type is used for search fields (a search field behaves like a regular text field).
Example
<form>
Search Google:
<input type="search" name="googlesearch">
</form>
Try it Yourself
There is currently no validation support for the tel type in any browser.
Example
<form>
Telephone:
<input type="tel" name="usrtel">
</form>
Try it Yourself
Depending on browser support, the url field can be automatically validated when submitted
Some smartphones recognize the url type, and adds ".com" to the keyboard to match url input.
Example
<form>
Add your homepage:
<input type="url" name="homepage">
</form>
Next Chapter
autocomplete
novalidate
autocomplete
autofocus
form
formaction
formenctype
formmethod
formnovalidate
formtarget
height and width
list
min and max
multiple
pattern (regexp)
placeholder
required
step
<form> / <input> autocomplete Attribute
The autocomplete attribute specifies whether a form or input field should have autocomplete on
or off.
When autocomplete is on, the browser automatically complete values based on values that the
user has entered before.
Tip: It is possible to have autocomplete "on" for the form, and "off" for specific input fields, or
vice versa.
Note: The autocomplete attribute works with <form> and the following <input> types: text,
search, url, tel, email, password, datepickers, range, and color.
Example
An HTML form with autocomplete on (and off for one input field):
Try it Yourself
Tip: In some browsers you may need to activate the autocomplete function for this to work.
When present, it specifies that the form-data (input) should not be validated when submitted.
Example
Try it Yourself
When present, it specifies that an <input> element should automatically get focus when the
page loads.
Example
Let the "First name" input field automatically get focus when the page loads:
Try it Yourself
Tip: To refer to more than one form, use a space-separated list of form ids.
Example
An input field located outside the HTML form (but still a part of the form):
Try it Yourself
The formaction attribute overrides the action attribute of the <form> element.
Example
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="Submit"><br>
<input type="submit" formaction="demo_admin.asp"
value="Submit as admin">
</form>
Try it Yourself
The formenctype attribute overrides the enctype attribute of the <form> element.
Send form-data that is default encoded (the first submit button), and encoded as
"multipart/form-data" (the second submit button):
Try it Yourself
The formmethod attribute overrides the method attribute of the <form> element.
Note: The formmethod attribute can be used with type="submit" and type="image".
Example
The second submit button overrides the HTTP method of the form:
Try it Yourself
<input> formnovalidate Attribute
The novalidate attribute is a boolean attribute.
When present, it specifies that the <input> element should not be validated when submitted.
The formnovalidate attribute overrides the novalidate attribute of the <form> element.
Example
<form action="demo_form.asp">
E-mail: <input type="email" name="userid"><br>
<input type="submit" value="Submit"><br>
<input type="submit" formnovalidate value="Submit without validation">
</form>
Try it Yourself
The formtarget attribute overrides the target attribute of the <form> element.
Note: The formtarget attribute can be used with type="submit" and type="image".
Example
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="Submit as normal">
<input type="submit" formtarget="_blank"
value="Submit to a new window">
</form>
Try it Yourself
Note: The height and width attributes are only used with <input type="image">.
Tip: Always specify both the height and width attributes for images. If height and width 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, and cannot reserve the appropriate
space to it. The effect will be that the page layout will change during loading (while the images
load).
Example
Define an image as the submit button, with height and width attributes:
Try it Yourself
Example
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
Try it Yourself
Note: The min and max attributes works with the following input types: number, range, date,
datetime, datetime-local, month, time and week.
Example
Try it Yourself
Note: The multiple attribute works with the following input types: email, and file.
Example
Try it Yourself
Note: The pattern attribute works with the following input types: text, 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):
Try it Yourself
<input> placeholder Attribute
The placeholder attribute specifies a short hint that describes the expected value of an input field
(e.g. a sample value or a short description of the expected format).
The short hint is displayed in the input field before the user enters a value.
Note: The placeholder attribute works with the following input types: text, search, url, tel, email,
and password.
Example
Try it Yourself
When present, it specifies that an input field must be filled out before submitting the form.
Note: The required attribute works with the following input types: text, search, url, tel, email,
password, date pickers, number, checkbox, radio, and file.
Example
Try it Yourself
<input> step Attribute
The step attribute specifies the legal number intervals for an <input> element.
Tip: The step attribute can be used together with the max and min attributes to create a range
of legal values.
Note: The step attribute works with the following input types: number, range, date, datetime,
datetime-local, month, time and week.
Example
Try it Yourself