HTML is a markup language for describing web documents (web pages).
<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
Using the description, a web browser can display a document with a heading and a paragraph.
HTML tags are keywords (tag names) surrounded by angle brackets:
<tagname>content</tagname>
The end tag is written like the start tag, but with a slash before the tag name
Below is a visualization of an HTML page structure:
<html>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
</body>
</html>
Since the early days of the web, there have been many versions of HTML:
Version |
Year |
HTML |
1991 |
HTML+ |
1993 |
HTML 2.0 |
1995 |
HTML 3.2 |
1997 |
HTML 4.01 |
1999 |
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 correctly if it knows the HTML version and type.
<!DOCTYPE html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
All tutorials and examples at W3Schools use HTML5. |
HTML can be edited by using a professional HTML editor like:
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.
Write or copy a simple HTML in notepad.
<html>
<body>
<h1>Hello World</h1>
<p>My First HTML page</p>
</body>
</html>
Save the file on your computer.
Select File -> Save as in the Notepad menu.
When saving an HTML file, use either the .htm or the .html file extension. There is no difference, it is entirely up to you.
To view HTML Page in Your Browser double-click your saved HTML file.
All HTML documents must start with a type declaration: <!DOCTYPE html>.
The HTML document itself begins with <html> and ends with </html>.
HTML headings are defined with the <h1> to <h6> tags:
<h1>This is a heading</h1>
<h2>This is a heading</h2>
<h3>This is a heading</h3>
HTML paragraphs are defined with the <p> tag:
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
HTML links are defined with the <a> tag:
Example
<a href="http://www.google.com">This is a link</a>
The link address is specified in the href attribute.
HTML images are defined with the <img> tag.
The source file (src), alternative text (alt), and size (width and height) are provided as attributes:
<img src="google.jpg" alt="google.com" width="104" height="142">
HTML elements are written with a start tag, with an end tag, with the content in between:
<tagname>content</tagname>
The HTML element is everything from the start tag to the end tag:
<p>My first paragraph.</p>
Start tag |
Element content |
End tag |
<h1> |
My First Heading |
</h1> |
<p> |
My first paragraph. |
</p> |
<br> |
|
|
Some HTML elements do not have an end tag. |
HTML elements can be nested (elements can contain elements).
All HTML documents consist of nested HTML elements.
This example contains 4 HTML elements:
<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
The <html> element defines the whole document.
It has a start tag <html> and an end tag </html>.
The element content is another HTML element (the <body> element).
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
The <body> element defines the document body.
It has a start tag <body> and an end tag </body>.
The element content is two other HTML elements (<h1> and <p>).
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
The <h1> element defines a heading.
It has a start tag <h1> and an end tag </h1>.
The element content is: My First Heading.
<h1>My First Heading</h1>
The <p> element defines a paragraph.
It has a start tag <p> and an end tag </p>.
The element content is: My first paragraph.
<p>My first paragraph.</p>
Some HTML elements will display correctly, even if you forget the end tag:
Example
<html>
<body>
<p>This is a paragraph
<p>This is a paragraph
</body>
</html>
HTML elements with no content are called empty elements.
<br> is an empty element without a closing tag (the <br> tag defines a line break).
Empty element can be "closed" in the opening tag like this: <br />.
HTML5 does not require empty elements to be closed. But if you need stricter validation, and make your document readable by XML parsers, please close all HTML elements.
HTML tags are not case sensitive: <P> means the same as <p>.
The HTML5 standard does not require lowercase tags, but W3C recommends lowercase in HTML4, and demandslowercase for stricter document types like XHTML.
At W3Schools we always use lowercase tags. |
Headings are defined with the <h1> to <h6> tags.
<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>
Headings Are Important
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.
Users skim your pages by its headings. It is important to use headings to show the document structure.
h1 headings should be main headings, followed by h2 headings, then the less important h3, and so on.
The <hr> tag creates a horizontal line in an HTML page.
The hr element can be used to separate content:
<p>This is a paragraph.</p>
<hr>
<p>This is a paragraph.</p>
<hr>
<p>This is a paragraph.</p>
The HTML <head> element has nothing to do with HTML headings.
The HTML <head> element only contains meta data.
The HTML <head> element is placed between the <html> tag and the <body> tag:
<!DOCTYPE html>
<html>
<head>
<title>My First HTML</title>
<meta charset="UTF-8">
</head>
<body>
.
.
.
Meta data: Data about data. HTML meta data: Data about the HTML document. |
The HTML <title> element is meta data.
It defines the HTML document's title. It will not be displayed in the document.
However, might be displayed in one of the browser tabs.
The HTML <meta> element is meta data.
It defines the character set used in the HTML document.
In the following chapter you will learn more about meta elements:
The HTML <style> element defines internal CSS style sheets.
The HTML <link> element can define external CSS style sheets.
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 Page Source" (in Chrome) or "View Source" (in IE), or similar in another browser. This will open a window containing the HTML code of the page.
HTML Headings
How to display headings in an HTML document.
HTML horizontal rules
How to insert horizontal lines.
The HTML <head> element
How to add meta information.
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 |
Defines an HTML document |
|
Defines the document's body |
|
Defines the document's head Element |
|
Defines HTML headings |
|
Defines a horizontal line |
Paragraphs are defined with the <p> tag.
<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:
<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:
<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.
(The example demonstrates some HTML formatting problems)
HTML paragraphs
How HTML paragraphs are displayed in a browser.
Line breaks
The use of line breaks in an HTML document.
Poem problems
Some problems with HTML formatting.
More paragraphs
The default behaviour of paragraphs.
W3Schools' tag reference contains additional information about HTML elements and their attributes.
Tag |
Description |
Defines a paragraph |
|
Inserts a single line break |
This text is bold
This text is italic
This is computer output
This is subscript and superscript
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).
Often <strong> renders as <b>, and <em> renders as <i>. |
Text formatting
How to format text in an HTML document.
Preformatted text
How to control the line breaks and spaces with the <pre> tag.
"Computer output" tags
How different "computer output" tags will be displayed.
Address
How to define contact information for the author/owner of an HTML document.
Abbreviations and acronyms
How to handle abbreviations and acronyms.
Text direction
How to change the text direction.
Quotations
How to handle long and short quotations.
Deleted and inserted text
How to mark deleted and inserted text.
Marked/Highlighted text
How to mark/highlight text.
Tag |
Description |
Defines bold text |
|
Defines emphasized text |
|
Defines a part of text in an alternate voice or mood |
|
Defines smaller text |
|
Defines important text |
|
Defines subscripted text |
|
Defines superscripted text |
|
Defines inserted text |
|
Defines deleted text |
|
Defines marked/highlighted text |
Tag |
Description |
Defines computer code text |
|
Defines keyboard text |
|
Defines sample computer code |
|
Defines a variable |
|
Defines preformatted text |
Tag |
Description |
Defines an abbreviation or acronym |
|
Defines contact information for the author/owner of a document |
|
Defines the text direction |
|
Defines a section that is quoted from another source |
|
Defines an inline (short) quotation |
|
Defines the title of a work |
|
Defines a definition term |
You can add comments to your HTML source by using the following syntax:
<!-- Write your comments here -->
Note: There is an exclamation point (!) in the opening tag, but not in the closing tag. |
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:
<!-- This is a comment -->
<p>This is a paragraph.</p>
<!-- Remember to add more information here -->
Comments are also great for debugging HTML, because you can comment out HTML lines of code, one at a time, to search for errors:
<!-- Do not display this at the moment
<img border="0" src="pic_mountain.jpg" alt="Mountain">
-->
You might stumble upon conditional comments in HTML:
<!--[if IE 8]>
.... some HTML here ....
<![endif]-->
Conditional comments defines HTML tags to be executed by IE only.
HTML comments tags can also be generated by various HTML software programs.
For example <!--webbot bot--> tags wrapped inside HTML comments by FrontPage and Expression Web.
As a rule, let these tags stay, to help support the software that created them.
The document language can be declared in the <html> tag.
The language is declared in the lang attribute.
Declaring a language is important for accessibility applications (screen readers) and search engines:
<!DOCTYPE html>
<html lang="en-US">
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
The first two letters specify the language (en). If there is a dialect, use two more letters (US).
HTML paragraphs are defined with the <p> tag.
In this example, the <p> element has a title attribute. The value of the attribute is "About W3Schools":
<p title="About W3Schools">
W3Schools is a web developer's site.
It provides tutorials and references covering
many aspects of web programming,
including HTML, CSS, JavaScript, XML, SQL, PHP, ASP, etc.
</p>
When you move the mouse over the element, the title will be displayed as a tooltip. |
HTML links are defined with the <a> tag. The link address is specified in the href attribute:
<a href="http://www.w3schools.com">This is a link</a>
You will learn more about links and the <a> tag later in this tutorial.
HTML images are defined with the <img> tag.
The filename of the source (src), and the size of the image (width and height) are all provided as attributes:
<img src="w3schools.jpg" width="104" height="142">
The image size is specified in pixels: width="104" means 104 screen pixels wide.
You will learn more about images and the <img> tag later in this tutorial.
The alt attribute specifies an alternative text to be used, when an HTML element cannot be displayed.
The value of the attribute can be read by "screen readers". This way, someone "listening" to the webpage, i.e. a blind person, can "hear" the element.
<img src="w3schools.jpg" alt="W3Schools.com" width="104" height="142">
The HTML5 standard does not require lower case attribute names.
The title attribute can be written with upper or lower case like Title and/or TITLE.
W3C recommends lowercase in HTML4, and demands lowercase for stricter document types like XHTML.
Lower case is the most common. Lower case is easier to type. |
The HTML5 standard does not require quotes around attribute values.
The href attribute, demonstrated above, can be written as:
<a href=http://www.w3schools.com>
W3C recommends quotes in HTML4, and demands quotes for stricter document types like XHTML.
Sometimes it is necessary to use quotes. This will not display correctly, because it contains a space:
<p title=About W3Schools>
Using quotes are the most common. Omitting quotes can produce errors. |
Double style quotes are the most common in HTML, but single style can also be used.
In some situations, when the attribute value itself contains double quotes, it is necessary to use single quotes:
<p title='John "ShotGun" Nelson'>
Or vice versa:
<p title="John 'ShotGun' Nelson">
The HTML width and height attributes
An HTML attribute without quotes
An HTML attribute without quotes that does not work
Exercise 1 » Exercise 2 » Exercise 3 »
Below is an alphabetical list of some attributes often used in HTML:
Attribute |
Description |
alt |
Specifies an alternative text for an image |
disabled |
Specifies that an input element should be disabled |
href |
Specifies the URL (web address) for a link |
id |
Specifies a unique id for an element |
src |
Specifies the URL (web address) for an image |
style |
Specifies an inline CSS style for an element |
title |
Specifies extra information about an element (displayed as a tool tip) |
value |
Specifies the value (text content) for an input element. |
A complete list, of all legal attributes for each HTML element, is listed in our: HTML Tag Reference.