Basic HTML Structure
Tag | Description |
---|---|
<!DOCTYPE> | Defines the document type |
<html> | Defines an HTML document |
<head> | Contains metadata/information for the document |
<title> | Defines a title for the document |
<body> | Defines the document's body |
<h1> to <h6> | Defines HTML headings |
<p> | Defines a paragraph |
<br> | Inserts a single line break |
<hr> | Defines a thematic change in the content |
<!--...--> | Defines a comment |
HTML <body> Tag
Example
<head>
<title>Title of the document</title>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Definition and Usage
The <body>
tag defines the document's body.
The <body>
element contains all the contents of an HTML document, such as headings, paragraphs, images, hyperlinks, tables, lists, etc.
Note: There can only be one <body>
element in an HTML document.
More Examples
Background Image Example:
<head>
<style>
body {
background-image: url(w3s.png);
}
</style>
</head>
<body>
<h1>Hello world!</h1>
<p><a href="https://www.w3schools.com">Visit W3Schools.com!</a></p>
</body>
Background Color Example:
<head>
<style>
body {
background-color: #E6E6FA;
}
</style>
</head>
<body>
<h1>Hello world!</h1>
<p><a href="https://www.w3schools.com">Visit W3Schools.com!</a></p>
</body>
Link Styling Examples:
Unvisited Links:
a:link { color: #0000FF; }Active Links:
a:active { color: #00FF00; }Visited Links:
a:visited { color: #FF0000; }HTML <!DOCTYPE> Declaration
Definition and Usage
All HTML documents must start with a <!DOCTYPE>
declaration.
The declaration is not an HTML tag. It is an "information" to the browser about what document type to expect.
In HTML 5, the declaration is simple:
Older HTML Documents
In older documents (HTML 4 or XHTML), the declaration is more complicated because the declaration must refer to a DTD (Document Type Definition).
HTML 4.01:
XHTML 1.1:
Tips and Notes
Tip: The <!DOCTYPE>
declaration is NOT case sensitive.
Examples
HTML <html> Tag
A simple HTML document:
<html lang="en">
<head>
<title>Title of the document</title>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Definition and Usage
The <html>
tag represents the root of an HTML document.
The <html>
tag is the container for all other HTML elements (except for the <!DOCTYPE> tag).
Note: You should always include the lang attribute inside
the <html>
tag, to declare the language of the Web page. This is meant to assist
search engines and browsers.
Attributes
Attribute | Value | Description |
---|---|---|
xmlns | http://www.w3.org/1999/xhtml | Specifies the XML namespace attribute (If you need your content to conform to XHTML) |
HTML <head> Tag
Example
<html lang="en">
<head>
<title>Title of the document</title>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Definition and Usage
The <head>
element is a container for metadata (data about data) and is placed between the <html> tag and the <body> tag.
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.
Elements inside <head>:
More Examples
Base Tag Example:
<head>
<base href="https://www.w3schools.com/" target="_blank">
</head>
<body>
<img src="images/stickman.gif" width="24" height="39" alt="Stickman">
<a href="tags/tag_base.asp">HTML base Tag</a>
</body>
</html>
Style Tag Example:
<head>
<style>
h1 {color:red;}
p {color:blue;}
</style>
</head>
<body>
<h1>A heading</h1>
<p>A paragraph.</p>
</body>
</html>
Link Tag Example:
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<h1>I am formatted with a linked style sheet</h1>
<p>Me too!</p>
</body>
</html>
Note: Most browsers will display the <head>
element with: display: none;
HTML <title> Tag
Example
<html>
<head>
<title>HTML Elements Reference</title>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Definition and Usage
The <title>
tag defines the title of the document. The title must be text-only, and it is shown in the browser's title bar or in the page's tab.
<title>
tag is required in HTML documents!
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.
The <title> element:
- defines a title in the browser toolbar
- provides a title for the page when it is added to favorites
- displays a title for the page in search-engine results
Tips for creating good titles:
- Go for a longer, descriptive title (avoid one- or two-word titles)
- Search engines will display about 50-60 characters of the title, so try not to have titles longer than that
- Do not use just a list of words as the title (this may reduce the page's position in search results)
So, try to make the title as accurate and meaningful as possible!
Note: You can NOT have more than one <title>
element in an HTML document.
HTML <h1> to <h6> Tags
Example
The six different HTML headings:
<h2>This is heading 2</h2>
<h3>This is heading 3</h3>
<h4>This is heading 4</h4>
<h5>This is heading 5</h5>
<h6>This is heading 6</h6>
Definition and Usage
The <h1>
to <h6>
tags are used to define HTML headings.
<h1>
defines the most important heading. <h6>
defines the least important heading.
Note: Only use one <h1>
per page - this should represent the main heading/subject for the whole page. Also, do not skip heading levels - start with <h1>
, then use <h2>
, and so on.
Styling Examples
Background and Text Color:
<h2 style="color:Tomato;">Hello World</h2>
Text Alignment:
<h2 style="text-align:left">This is heading 2</h2>
<h3 style="text-align:right">This is heading 3</h3>
<h4 style="text-align:justify">This is heading 4</h4>
Default CSS Settings
h1 Default Style:
h2 Default Style:
h3 Default Style:
h4-h6 Default Styles:
HTML <p> Tag
Example
A paragraph is marked up as follows:
Definition and Usage
The <p>
tag defines a paragraph.
Browsers automatically add a single blank line before and after each <p>
element.
Styling Examples
Text Alignment:
CSS Styling:
Line Breaks in Source Code:
Poem Example:
My Bonnie lies over the sea.
My Bonnie lies over the ocean.
Oh, bring back my Bonnie to me. </p>
Default CSS Settings
HTML <br> Tag
Example
Insert single line breaks in a text:
Definition and Usage
The <br>
tag inserts a single line break.
The <br>
tag is useful for writing addresses or poems.
The <br>
tag is an empty tag which means that it has no end tag.
Note: Use the <br>
tag to enter line breaks, not to add space between paragraphs.
Example
Using <br> in a poem:
HTML <hr> Tag
Example
Use the <hr> tag to define thematic changes in the content:
Definition and Usage
The <hr>
tag defines a thematic break in an HTML page (e.g. a shift of topic).
The <hr>
element is most often displayed as a horizontal rule that is used to separate content (or define a change) in an HTML page.
Styling Examples
Align a <hr> element:
A noshaded <hr>:
Set the height:
Set the width:
Default CSS Settings
HTML Comments
Example
Here is how you add comments in HTML:
Definition and Usage
HTML comments are not displayed in the browser, but they can help document your HTML source code.
Note: Comments are used to explain the code and can help when you edit the source code later.
Examples
Hide Content:
Hide Inline Content:
Multi-line Comments:
Warning: You cannot nest HTML comments (put one comment inside another).
HTML Formatting Tags
Tag | Description |
---|---|
<acronym> | Not supported in HTML5. Use <abbr> instead. Defines an acronym |
<abbr> | Defines an abbreviation or an acronym |
<b> | Defines bold text |
<bdi> | Isolates a part of text that might be formatted differently from the rest of the document |
<bdo> | Overrides the current text direction |
<big> | Not supported in HTML5. Use CSS instead. Defines big text |
<i> | Defines italic text |
<ksup> | Not supported in HTML5. Use CSS instead. Defines text that should be displayed as superscripted |
<ksub> | Not supported in HTML5. Use CSS instead. Defines text that should be displayed as subscripted |
<mark> | Defines marked/highlighted text |
<small> | Defines smaller text |
<strong> | Defines important text |
<sub> | Defines subscripted text |
<sup> | Defines superscripted text |
<time> | Defines a specific time (or datetime) |
<u> | Defines text that should be underlined |
<wbr> | Defines a possible line-break |
Note: Some tags like <font>, <center>, and <strike> are not supported in HTML5. Use CSS for styling instead.
HTML <abbr> Tag
Example
An abbreviation is marked up as follows:
Definition and Usage
The <abbr>
tag defines an abbreviation or an acronym, like "HTML", "CSS", "Mr.", "Dr.", "ASAP", "ATM".
Tip: Use the global title
attribute to show the description for the abbreviation/acronym when you mouse over the element.
More Examples
HTML <address> Tag
Example
Contact information for Example.com:
Definition and Usage
The <address>
tag defines the contact information for the author/owner of a document or an article.
The contact information can be an email address, URL, physical address, phone number, social media handle, etc.
Note: The text in the <address>
element usually renders in italic, and browsers will always add a line break before and after the element.
HTML <b> Tag
Example
Make some text bold (without marking it as important):
Definition and Usage
The <b>
tag specifies bold text without any extra importance.
Tips and Notes
Note: According to the HTML5 specification, the <b>
tag should be used as a LAST resort when no other tag is more appropriate.
The specification states that:
- Headings should use
<h1>
to<h6>
tags - Emphasized text should use
<em>
tag - Important text should use
<strong>
tag - Marked/highlighted text should use
<mark>
tag
Tip: You can also use CSS to set bold text: font-weight: bold;
HTML <bdi> Tag
Example
Isolate the usernames from the surrounding text-direction settings:
<li>User <bdi>hrefs</bdi>: 60 points</li>
<li>User <bdi>jdoe</bdi>: 80 points</li>
<li>User <bdi>إيان</bdi>: 90 points</li>
</ul>
Definition and Usage
BDI stands for Bi-Directional Isolation.
The <bdi>
tag isolates a part of text that might be formatted in a different direction from other text outside it.
This element is useful when embedding user-generated content with an unknown text direction.
HTML <bdo> Tag
Example
Specify the text direction:
This text will go right-to-left.
</bdo>
HTML <blockquote> Tag
Example
A section that is quoted from another source:
For 50 years, WWF has been protecting the future of nature.
The world's leading conservation organization, WWF works in 100 countries
and is supported by 1.2 million members in the United States and close to 5 million globally.
</blockquote>
Definition and Usage
The <blockquote>
tag specifies a section that is quoted from another source.
Browsers usually indent <blockquote>
elements.
Tips and Notes
Tip: Use <q>
for inline (short) quotations.
HTML <cite> Tag
Example
Define the title of a work with the <cite> tag:
Definition and Usage
The <cite>
tag defines the title of a creative work (e.g., a book, a poem, a song, a movie, a painting, etc.).
Note: A person's name is not the title of a work.
The text in the <cite>
element usually renders in italic.
HTML <code> Tag
Example
Define some text as computer code in a document:
<p>The CSS <code>background-color</code> property defines the background color of an element.</p>
Definition and Usage
The <code>
tag is used to define a piece of computer code. The content inside is displayed in the browser's default monospace font.
Related Tags
HTML <del> Tag
Example
A text with a deleted part, and a new, inserted part:
Definition and Usage
The <del>
tag defines text that has been deleted from a document. Browsers will usually strike a line through deleted text.
Attributes
HTML <dfn> Tag
Example
Mark up a term with <dfn>:
Definition and Usage
The <dfn>
tag stands for "definition element", and it specifies a term that is going to be defined within the content.
The nearest parent of the <dfn>
tag must also contain the definition/explanation for the term.
Usage Examples
1. Basic Usage:
2. With title attribute:
3. With abbr tag:
4. With id reference:
HTML <em> Tag
Example
Mark up emphasized text in a document:
HTML <i> Tag
Example
Mark up text that is set off from the normal prose in a document:
Definition and Usage
The <i>
tag defines a part of text in an alternate voice or mood. The content inside is typically displayed in italic.
The <i>
tag is often used to indicate a technical term, a phrase from another language, a thought, a ship name, etc.
Note: Use the <i>
element only when there is not a more appropriate semantic element.
Alternative Tags
<em>
- for emphasized text<strong>
- for important text<mark>
- for marked/highlighted text<cite>
- for titles of works<dfn>
- for definition terms
HTML <ins> Tag
Example
A text with a deleted part, and a new, inserted part:
Definition and Usage
The <ins>
tag defines text that has been inserted into a document. Browsers will usually underline inserted text.
Tip: Use with the <del>
tag to show both deleted and inserted text.
Attributes
HTML <kbd> Tag
Example
Define some text as keyboard input in a document:
Definition and Usage
The <kbd>
tag is used to define keyboard input. The content inside is displayed in the browser's default monospace font.
Related Tags
HTML <mark> Tag
Example
Highlight parts of a text:
Definition and Usage
The <mark>
tag defines text that should be marked or highlighted.
HTML <meter> Tag
Example
Use the meter element to display a scalar value within a given range (a gauge):
Definition and Usage
The <meter>
tag defines a scalar measurement within a known range, or a fractional value. This is also known as a gauge.
Examples: Disk usage, the relevance of a query result, etc.
Note: The <meter>
tag should not be used to indicate progress (as in a progress bar). For progress bars, use the <progress>
tag.
Tip: Always add the <label>
tag for best accessibility practices!
Attributes
Attribute | Value | Description |
---|---|---|
form | form_id | Specifies which form the <meter> element belongs to |
high | number | Specifies the range that is considered to be a high value |
low | number | Specifies the range that is considered to be a low value |
max | number | Specifies the maximum value of the range |
min | number | Specifies the minimum value of the range. Default value is 0 |
optimum | number | Specifies what value is the optimal value for the gauge |
value | number | Required. Specifies the current value of the gauge |
HTML <pre> Tag
Example
Preformatted text:
Definition and Usage
The <pre>
tag defines preformatted text.
Text in a <pre>
element is displayed in a fixed-width font, and the text preserves both spaces and line breaks. The text will be displayed exactly as written in the HTML source code.