HTML Tutorial
HTML Tutorial
❮ HomeNext ❯
Example
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
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.
A Simple HTML Document
Example
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
</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
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 »
ADVERTISEMENT
HTML Paragraphs
HTML paragraphs are defined with the <p> tag:
Example
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
Try it Yourself »
HTML Links
HTML links are defined with the <a> tag:
Example
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 »
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:
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>:
HTML Attributes
All HTML elements can have attributes
Attributes provide additional information about elements
Attributes are always specified in the start tag
Attributes usually come in name/value pairs like: name="value"
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:
<img src="img_typo.jpg" alt="Girl with a jacket">
Try it Yourself »
You will learn more about images in our HTML Images chapter.
ADVERTISEMENT
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 title Attribute
The title attribute defines some extra information about an element.
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:
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
Exercise?
Which of the following is a correct syntax for using an HTML attribute?
窗体顶端
<img src='img_girl.jpg'>
<img src('img_girl.jpg')>
<img src:'img_girl.jpg'>
Submit Answer »
窗体底端
HTML contains several elements for defining text with a special meaning.
Example
Try it Yourself »
Example
Try it Yourself »
The HTML <strong> element defines text with strong importance. The content inside is
typically displayed in bold.
Example
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
Example
Try it Yourself »
Example
Example
Example
Example
Example
Try it Yourself »
Exercise?
Two of the following HTML elements makes the text bold, which two?
窗体顶端
Submit Answer »
窗体底端
For a complete list of all available HTML tags, visit our HTML Tag Reference.
Example
Example
Example
<p>WWF's goal is to: <q>Build a future where people live in harmony with
nature.</q></p>
Marking abbreviations can give useful information to browsers, translation systems and
search-engines.
Tip: Use the global title attribute to show the description for the abbreviation/acronym
when you mouse over the element.
Example
The text in the <address> element usually renders in italic, and browsers will always add a
line break before and after the <address> element.
Example
<address>
Written by John Doe.<br>
Visit us at:<br>
Example.com<br>
Box 564, Disneyland<br>
USA
</address>
Example
The HTML <bdo> tag is used to override the current text direction:
Example
HTML Comments
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>
Hide Content
Comments can be used to hide content.
Example
<p>This is a paragraph.</p>
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>
<!--
<p>Look at this cool image:</p>
<img border="0" src="pic_trulli.jpg" alt="Trulli">
-->
<p>This is a paragraph too.</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
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
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
Hello World
Hello World
Hello World
Example
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
Example