HTML Semantic Elements
Semantic elements = elements with a meaning.
What are Semantic Elements?
A semantic element clearly describes its meaning to both the
browser and the developer.
Examples of non-semantic elements: <div>
and <span>
-
Tells nothing about its content.
Examples of semantic elements: <form>
, <table>
,
and <article>
- Clearly defines its
content.
Semantic Elements in HTML
Many web sites contain HTML code like: <div
id="nav"> <div class="header"> <div
id="footer"> to indicate navigation, header, and footer.
In HTML there are some semantic elements that can be used to
define different parts of a web page:
- <article>
- <aside>
- <details>
- <figcaption>
- <figure>
- <footer>
- <header>
- <main>
- <mark>
- <nav>
- <section>
- <summary>
- <time>
HTML <section> Element
The <section>
element defines a
section in a document.
Examples of where a <section>
element
can be used:
- Chapters
- Introduction
- News
items
- Contact
information
A web page could normally be split into sections for introduction,
content, and contact information.
Example
Two sections in a document:
<section>
<h1>Heading</h1>
<p>Paragraphs</p>
</section>
<section>
<h1>Heading</h1>
<p>Paragraphs</p></section>
HTML <article> Element
The <article>
element specifies
independent, self-contained content.
An article should make sense on its own, and it should be possible
to distribute it independently from the rest of the web site.
Examples of where the <article>
element
can be used:
- Forum
posts
- Blog
posts
- User
comments
- Product
cards
- Newspaper
articles
Example
Three articles with independent, self-contained content:
<article>
<h1>Heading</h1>
<p>Paragraphs</p>
</article>
<article>
<h1>Heading</h1>
<p>Paragraphs</p>
</article>
<article>
<h1>Heading</h1>
<p>Paragraphs</p>
</article>
Example 2
Use CSS to style the <article> element:
<html>
<head>
<style>
.all-browsers {
margin: 0;
padding: 5px;
background-color: lightgray;
}
.all-browsers > h1, .browser {
margin: 10px;
padding: 5px;
}
.browser {
background: white;
}
.browser > h2, p {
margin: 4px;
font-size: 90%;
}
</style>
</head>
<body>
<article class="all-browsers">
<h1>Heading</h1>
<article class="browser">
<h1>Heading</h1>
<p>Paragraphs</p>
</article>
<article class="browser">
<h1>Heading</h1>
<p>Paragraphs</p>
</article>
<article class="browser">
<h1>Heading</h1>
<p>Paragraphs</p>
</article>
</article>
</body>
</html>
Nesting <article> in <section> or
Vice Versa?
The <article>
element specifies
independent, self-contained content.
The <section>
element defines
section in a document.
Can we use the definitions to decide how to nest those elements?
No, we cannot!
So, you will find HTML pages with <section>
elements
containing <article>
elements, and <article>
elements
containing <section>
elements.
HTML <header> Element
The <header>
element represents
a container for introductory content or a set of navigational links.
A <header>
element typically
contains:
- one
or more heading elements (<h1> - <h6>)
- logo
or icon
- authorship
information
Note: You can have several <header>
elements
in one HTML document. However, <header>
cannot
be placed within a <footer>
, <address>
or
another <header>
element.
Example
A header for an <article>:
<article>
<header>
<h1>Heading</h1>
<p>Paragraphs</p>
</header>
<p>Paragraphs</p>
</article>
HTML <footer> Element
The <footer>
element defines a
footer for a document or section.
A <footer>
element typically
contains:
- authorship
information
- copyright
information
- contact
information
- sitemap
- back
to top links
- related
documents
You can have several <footer>
elements
in one document.
Example
A footer section in a document:
<footer>
<p>Author:
</p>
<p><a href="mailto:mail@example.com">mail@example.com</a></p>
</footer>
HTML <nav> Element
The <nav>
element defines a set of
navigation links.
Notice that NOT all links of a document should be inside a <nav>
element.
The <nav>
element is intended only
for major blocks of navigation links.
Browsers, such as screen readers for disabled users, can use this
element to determine whether to omit the initial rendering of this content.
Example
A set of navigation links:
<nav>
<a href="/html/">HTML</a> |
<a href="/css/">CSS</a> |
<a href="/js/">JavaScript</a> |
<a href="/jquery/">jQuery</a>
</nav>
HTML <aside> Element
The <aside>
element defines some
content aside from the content it is placed in (like a sidebar).
The <aside>
content should be
indirectly related to the surrounding content.
Example
Display some content aside from the content it is placed in:
<h1>Heading</h1>
<p>Paragraphs</p>
<aside>
<h1>Heading</h1>
<p>Paragraphs</p>
</aside>
Example 2
Use CSS to style the <aside> element:
<html>
<head>
<style>
aside {
width: 30%;
padding-left: 15px;
margin-left: 15px;
float: right;
font-style: italic;
background-color: lightgray;
}
</style>
</head>
<body>
<p>Paragraphs!</p>
<aside>
<p>Paragraphs!</p>
</aside>
<p>Paragraphs!</p>
</body>
</html>
HTML <figure> and <figcaption>
Elements
The <figure>
tag specifies
self-contained content, like illustrations, diagrams, photos, code listings,
etc.
The <figcaption>
tag defines a
caption for a <figure>
element. The <figcaption>
element
can be placed as the first or as the last child of a <figure>
element.
The <img>
element defines the
actual image/illustration.
Example
<figure>
<img src="img.jpg" alt="Image">
<figcaption>Image</figcaption>
</figure>
Why Semantic Elements?
According to the W3C: "A semantic Web allows data to be
shared and reused across applications, enterprises, and communities."
Semantic Elements in HTML
Below is a list of some of the semantic elements in HTML.
Tag |
Description |
<article> |
Defines independent,
self-contained content |
<aside> |
Defines content aside from
the page content |
<details> |
Defines additional details
that the user can view or hide |
<figcaption> |
Defines a caption for a
<figure> element |
<figure> |
Specifies self-contained
content, like illustrations, diagrams, photos, code listings, etc. |
<footer> |
Defines a footer for a
document or section |
<header> |
Specifies a header for a
document or section |
<main> |
Specifies the main content of
a document |
<mark> |
Defines marked/highlighted
text |
<nav> |
Defines navigation links |
<section> |
Defines a section in a
document |
<summary> |
Defines a visible heading for
a <details> element |
<time> |
Defines a date/time |
HTML Style Guide
A consistent, clean, and tidy HTML code makes it easier for others
to read and understand your code.
Here are some guidelines and tips for creating good HTML code.
Always Declare Document Type
Always declare the document type as the first line in your
document.
The correct document type for HTML is:
<!DOCTYPE html>
Use Lowercase Element Names
HTML allows mixing uppercase and lowercase letters in element
names.
However, we recommend using lowercase element names, because:
- Mixing
uppercase and lowercase names looks bad
- Developers
normally use lowercase names
- Lowercase
looks cleaner
- Lowercase
is easier to write
Good:
<body>
<p>This
is a paragraph.</p>
</body>
Bad:
<BODY>
<P>This
is a paragraph.</P>
</BODY>
Close All HTML Elements
In HTML, you do not have to close all elements (for example
the <p>
element).
However, we strongly recommend closing all HTML elements, like
this:
Good:
<section>
<p>This
is a paragraph.</p>
<p>This
is a paragraph.</p>
</section>
Bad:
<section>
<p>This
is a paragraph.
<p>This
is a paragraph.
</section>
Use Lowercase Attribute Names
HTML allows mixing uppercase and lowercase letters in attribute
names.
However, we recommend using lowercase attribute names, because:
- Mixing
uppercase and lowercase names looks bad
- Developers
normally use lowercase names
- Lowercase
looks cleaner
- Lowercase
is easier to write
Good:
<a href="https://fixhubblog.blogspot.com/">Visit our HTML tutorial</a>
Bad:
<a HREF="https://fixhubblog.blogspot.com/">Visit our HTML tutorial</a>
Always Quote Attribute Values
HTML allows attribute values without quotes.
However, we recommend quoting attribute values, because:
- Developers
normally quote attribute values
- Quoted
values are easier to read
- You
MUST use quotes if the value contains spaces
Good:
<table class="striped">
Bad:
<table class=striped>
Very bad:
This will not work, because the value contains spaces:
<table class=table striped>
Always Specify alt, width, and height for
Images
Always specify the alt
attribute for
images. This attribute is important if the image for some reason cannot be
displayed.
Also, always define the width
and height
of
images. This reduces flickering, because the browser can reserve space for the
image before loading.
Good:
<img src="html5.gif" alt="HTML5" style="width:128px;height:128px">
Bad:
<img src="html5.gif">
Spaces and Equal Signs
HTML allows spaces around equal signs. But space-less is easier to
read and groups entities better together.
Good:
<link rel="stylesheet" href="styles.css">
Bad:
<link rel = "stylesheet" href = "styles.css">
Avoid Long Code Lines
When using an HTML editor, it is NOT convenient to scroll right
and left to read the HTML code.
Blank Lines and Indentation
Do not add blank lines, spaces, or indentations without a reason.
For readability, add blank lines to separate large or logical code
blocks.
For readability, add two spaces of indentation. Do not use the tab
key.
Good:
<body>
<h1>Heading</h1>
<h2>Fixhub</h2>
<p>FixHub
Blog is the easy free tutorial platform where you get the essential knowledge
what you need to know these at job sector.</p>
<h2>Fixhub</h2>
<p>
FixHub Blog is the easy free tutorial platform where you get the essential
knowledge what you need to know these at job sector.</p>
<h2>Fixhub</h2>
<p>
FixHub Blog is the easy free tutorial platform where you get the essential
knowledge what you need to know these at job sector.</p>
</body>
Bad:
<body>
<h1>Heading</h1>
<h2>Fixhub</h2><p>FixHub Blog is the easy free tutorial
platform where you get the essential knowledge what you need to know these at
job sector.</p><h2>Fixhub</h2><p> FixHub Blog is the easy free tutorial
platform where you get the essential knowledge what you need to know these at
job sector.</p><h2>Fixhub</h2><p> FixHub Blog is the easy free tutorial
platform where you get the essential knowledge what you need to know these at
job sector.</p>
</body>
Good Table
Example:
<table>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<tr>
<td>A</td>
<td>Description
of A</td>
</tr>
<tr>
<td>B</td>
<td>Description
of B</td>
</tr>
</table>
Good List
Example:
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
</ul>
Never Skip the <title> Element
The <title>
element is required in
HTML.
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
So, try to make the title as accurate and meaningful as
possible:
<title>HTML
Style Guide and Coding Conventions</title>
Omitting <html> and <body>?
An HTML page will validate without the <html>
and <body>
tags:
Example
<!DOCTYPE html>
<head>
<title>Page
Title</title>
</head>
<h1>This
is a heading</h1>
<p>This
is a paragraph.</p>
However, we strongly recommend to always add the <html>
and <body>
tags!
Omitting <body>
can
produce errors in older browsers.
Omitting <html>
and <body>
can
also crash DOM and XML software.
Omitting <head>?
The HTML <head> tag can also be omitted.
Browsers will add all elements before <body>
, to a
default <head>
element.
Example
<!DOCTYPE html>
<html>
<title>Page
Title</title>
<body>
<h1>This
is a heading</h1>
<p>This
is a paragraph.</p>
</body>
</html>
However, we recommend using the <head>
tag.
Close Empty HTML Elements?
In HTML, it is optional to close empty elements.
Allowed:
<meta charset="utf-8">
Also
Allowed:
<meta charset="utf-8" />
If you expect XML/XHTML software to access your page, keep the
closing slash (/), because it is required in XML and XHTML.
Add the lang Attribute
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.
Example
<!DOCTYPE html>
<html lang="en-us">
<head>
<title>Page
Title</title>
</head>
<body>
<h1>This
is a heading</h1>
<p>This
is a paragraph.</p>
</body>
</html>
Meta Data
To ensure proper interpretation and correct search engine
indexing, both the language and the character encoding <meta
charset="
charset">
should
be defined as early as possible in an HTML document:
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<title>Page
Title</title>
</head>
Setting The Viewport
The viewport is the user's visible area of a web page. It varies
with the device - it will be smaller on a mobile phone than on a computer
screen.
You should include the following <meta>
element
in all your web pages:
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
This gives the browser instructions on how to control the page's
dimensions and scaling.
The width=device-width
part sets the
width of the page to follow the screen-width of the device (which will vary
depending on the device).
The initial-scale=1.0
part sets the
initial zoom level when the page is first loaded by the browser.
Here is an example of a web page without the viewport meta tag, and the
same web page with the
viewport meta tag:
Tip: If you are browsing this page with a phone or a tablet, you can click on the two links below to see the difference.
HTML Comments
Short comments should be written on one line, like this:
<!--
This is a comment -->
Comments that spans more than one line, should be written like
this:
<!--
This is a long comment example. This is a long
comment example.
This is a long comment example. This is a long
comment example.
-->
Long comments are easier to observe if they are indented with two
spaces.
Using Style Sheets
Use simple syntax for linking to style sheets (the type
attribute
is not necessary):
<link rel="stylesheet" href="styles.css">
Short CSS rules can be written compressed, like this:
p.intro {font-family: Arial Black;font-size:17em;}
Long CSS rules should be written over multiple lines:
body {
background-color: lightgrey;
font-family: "Arial Black",
Helvetica, sans-serif;
font-size: 16em;
color: black;
}
- Place
the opening bracket on the same line as the selector
- Use
one space before the opening bracket
- Use
two spaces of indentation
- Use
semicolon after each property-value pair, including the last
- Only
use quotes around values if the value contains spaces
- Place
the closing bracket on a new line, without leading spaces
Loading JavaScript in HTML
Use simple syntax for loading external scripts (the type
attribute
is not necessary):
<script src="myscript.js">
Accessing HTML Elements with JavaScript
Using "untidy" HTML code can result in JavaScript
errors.
These two JavaScript statements will produce different results:
Example
getElementById("Demo").innerHTML = "Hello";
getElementById("demo").innerHTML = "Hello";
Use Lower Case File Names
Some web servers (Apache, Unix) are case sensitive about file
names: "london.jpg" cannot be accessed as "London.jpg".
Other web servers (Microsoft, IIS) are not case sensitive:
"london.jpg" can be accessed as "London.jpg".
If you use a mix of uppercase and lowercase, you have to be aware
of this.
If you move from a case-insensitive to a case-sensitive server,
even small errors will break your web!
To avoid these problems, always use lowercase file names!
File Extensions
HTML files should have a .html extension (.htm is
allowed).
CSS files should have a .css extension.
JavaScript files should have a .js extension.
Differences Between .htm and .html?
There is no difference between the .htm and .html file extensions!
Both will be treated as HTML by any web browser and web server.
Default Filenames
When a URL does not specify a filename at the end (like
"https://fixhubblog.blogspot.com/"), the server just adds a default
filename, such as "index.html", "index.htm",
"default.html", or "default.htm".
If your server is configured only with "index.html" as
the default filename, your file must be named "index.html", and not
"default.html".
However, servers can be configured with more than one default
filename; usually you can set up as many default filenames as you want.
HTML Entities
Reserved characters in HTML must be replaced with character
entities.
HTML Entities
Some characters are reserved in HTML.
If you use the less than (<) or greater than (>) signs in
your text, the browser might mix them with tags.
Character entities are used to display reserved characters in
HTML.
A character entity looks like this:
&entity_name;
OR
&#entity_number;
To display a less than sign (<) we must write: < or <
Advantage of using an entity name: An
entity name is easy to remember.
Disadvantage of using
an entity name: Browsers may not support all entity names,
but the support for entity numbers is good.
Non-breaking Space
A commonly used entity in HTML is the non-breaking space:
A non-breaking space is a space that will not break into a new
line.
Two words separated by a non-breaking space will stick together
(not break into a new line). This is handy when breaking the words might be
disruptive.
Examples:
- §
10
- 10
km/h
- 10
PM
Another common use of the non-breaking space is to prevent
browsers from truncating spaces in HTML pages.
If you write 10 spaces in your text, the browser will remove 9 of
them. To add real spaces to your text, you can use the character
entity.
Tip: The non-breaking hyphen (‑) is
used to define a hyphen character (‑) that does not break into a new line.
Some Useful HTML Character Entities
Result |
Description |
Entity Name |
Entity Number |
non-breaking space |
|
  |
|
< |
less than |
< |
< |
> |
greater than |
> |
> |
& |
ampersand |
& |
& |
" |
double quotation mark |
" |
" |
' |
single quotation mark
(apostrophe) |
' |
' |
¢ |
cent |
¢ |
¢ |
£ |
pound |
£ |
£ |
¥ |
yen |
¥ |
¥ |
€ |
euro |
€ |
€ |
© |
copyright |
© |
© |
® |
registered trademark |
® |
® |
Note: Entity names are case sensitive.
Combining Diacritical Marks
A diacritical mark is a "glyph" added to a letter.
Some diacritical marks, like grave ( ̀) and acute ( ́)
are called accents.
Diacritical marks can appear both above and below a letter, inside
a letter, and between two letters.
Diacritical marks can be used in combination with alphanumeric
characters to produce a character that is not present in the character set
(encoding) used in the page.
Here are some examples:
Mark |
Character |
Construct |
Result |
̀ |
a |
à |
à |
́ |
a |
á |
á |
̂ |
a |
â |
â |
̃ |
a |
ã |
ã |
̀ |
O |
Ò |
Ò |
́ |
O |
Ó |
Ó |
̂ |
O |
Ô |
Ô |
̃ |
O |
Õ |
Õ |
You will see more HTML symbols in the next chapter of this
tutorial.
Using
Emojis in HTML
Emojis are characters from the UTF-8 character set: 😄 😍 💗
What are Emojis?
Emojis look like images, or icons, but they are not.
They are letters (characters) from the UTF-8 (Unicode) character
set.
UTF-8 covers almost all of the characters and symbols in the
world.
The HTML charset Attribute
To display an HTML page correctly, a web browser must know the
character set used in the page.
This is specified in the <meta>
tag:
<meta charset="UTF-8">
If not specified, UTF-8 is the default character set in HTML.
UTF-8 Characters
Many UTF-8 characters cannot be typed on a keyboard, but they can
always be displayed using numbers (called entity numbers):
- A
is 65
- B
is 66
- C
is 67
Example
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<p>I
will display A B C</p>
<p>I
will display A B C</p>
</body>
</html>
Example
Explained
The <meta
charset="UTF-8">
element defines the
character set.
The characters A, B, and C, are displayed by the numbers 65, 66,
and 67.
To let the browser understand that you are displaying a character,
you must start the entity number with &# and end it with ; (semicolon).
Emoji Characters
Emojis are also characters from the UTF-8 alphabet:
- 😄 is 128516
- 😍 is 128525
- 💗 is 128151
Example
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>My
First Emoji</h1>
<p>😀</p>
</body>
</html>
Since Emojis are characters, they can be copied, displayed, and
sized just like any other character in HTML.
Example
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>Sized
Emojis</h1>
<p style="font-size:48px">
😀 😄 😍 💗
</p>
</body>
</html>
Using
Emojis in HTML
Emojis are characters from the UTF-8 character set: 😄 😍 💗
What are Emojis?
Emojis look like images, or icons, but they are not.
They are letters (characters) from the UTF-8 (Unicode) character
set.
UTF-8 covers almost all of the characters and symbols in the
world.
The HTML charset Attribute
To display an HTML page correctly, a web browser must know the
character set used in the page.
This is specified in the <meta>
tag:
<meta charset="UTF-8">
If not specified, UTF-8 is the default character set in HTML.
UTF-8 Characters
Many UTF-8 characters cannot be typed on a keyboard, but they can
always be displayed using numbers (called entity numbers):
- A
is 65
- B
is 66
- C
is 67
Example
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<p>I
will display A B C</p>
<p>I
will display A B C</p>
</body>
</html>
Example
Explained
The <meta
charset="UTF-8">
element defines the
character set.
The characters A, B, and C, are displayed by the numbers 65, 66,
and 67.
To let the browser understand that you are displaying a character,
you must start the entity number with &# and end it with ; (semicolon).
Emoji Characters
Emojis are also characters from the UTF-8 alphabet:
- 😄 is 128516
- 😍 is 128525
- 💗 is 128151
Example
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>My
First Emoji</h1>
<p>😀</p>
</body>
</html>
Since Emojis are characters, they can be copied, displayed, and
sized just like any other character in HTML.
Example
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>Sized
Emojis</h1>
<p style="font-size:48px">
😀 😄 😍 💗
</p>
</body>
</html>
HTML Uniform Resource Locators
A URL is another word for a web address.
A URL can be composed of words (e.g. w3schools.com), or an
Internet Protocol (IP) address (e.g. 192.68.20.50).
Most people enter the name when surfing, because names are easier
to remember than numbers.
URL - Uniform Resource Locator
Web browsers request pages from web servers by using a URL.
A Uniform Resource Locator (URL) is used to address a document (or
other data) on the web.
A web address like https://fixhubblog.blogspot.com follows these syntax rules:
scheme://prefix.domain:port/path/filename
Explanation:
- scheme -
defines the type of Internet service (most common
is http or https)
- prefix -
defines a domain prefix (default for http is www)
- domain -
defines the Internet domain name (like w3schools.com)
- port -
defines the port number at the host (default for http
is 80)
- path -
defines a path at the server (If omitted: the root
directory of the site)
- filename -
defines the name of a document or resource
Common URL Schemes
The table below lists some common schemes:
Scheme |
Short for |
Used for |
http |
HyperText Transfer Protocol |
Common web pages. Not
encrypted |
https |
Secure HyperText Transfer
Protocol |
Secure web pages. Encrypted |
ftp |
File Transfer Protocol |
Downloading or uploading
files |
file |
|
A file on your computer |
URL Encoding
URLs can only be sent over the Internet using the ASCII character-set. If a URL contains characters outside the ASCII set, the
URL has to be converted.
URL encoding converts non-ASCII characters into a format that can
be transmitted over the Internet.
URL encoding replaces non-ASCII characters with a "%"
followed by hexadecimal digits.
URLs cannot contain spaces. URL encoding normally replaces a space
with a plus (+) sign, or %20.
If you click "Submit", the browser will URL encode the
input before it is sent to the server.
A page at the server will display the received input.
Try some other input and click Submit again.
ASCII Encoding Examples
Your browser will encode input, according to the character-set
used in your page.
The default character-set in HTML5 is UTF-8.
Character |
From Windows-1252 |
From UTF-8 |
€ |
%80 |
%E2%82%AC |
£ |
%A3 |
%C2%A3 |
© |
%A9 |
%C2%A9 |
® |
%AE |
%C2%AE |
À |
%C0 |
%C3%80 |
Á |
%C1 |
%C3%81 |
 |
%C2 |
%C3%82 |
à |
%C3 |
%C3%83 |
Ä |
%C4 |
%C3%84 |
Å |
%C5 |
%C3%85 |
.
HTML Versus XHTML
XHTML is a stricter, more XML-based version of HTML.
What is XHTML?
- XHTML
stands for EXtensible HyperText Markup Language
- XHTML
is a stricter, more XML-based version of HTML
- XHTML
is HTML defined as an XML application
- XHTML
is supported by all major browsers
Why XHTML?
XML is a markup language where all documents must be marked up
correctly (be "well-formed").
XHTML was developed to make HTML more extensible and flexible to
work with other data formats (such as XML). In addition, browsers ignore errors
in HTML pages, and try to display the website even if it has some errors in the
markup. So XHTML comes with a much stricter error handling.
The Most Important Differences from HTML
- <!DOCTYPE>
is mandatory
- The
xmlns attribute in <html> is mandatory
- <html>,
<head>, <title>, and <body> are mandatory
- Elements
must always be properly nested
- Elements
must always be closed
- Elements
must always be in lowercase
- Attribute
names must always be in lowercase
- Attribute
values must always be quoted
- Attribute
minimization is forbidden
XHTML - <!DOCTYPE ....> Is Mandatory
An XHTML document must have an XHTML <!DOCTYPE> declaration.
The <html>, <head>, <title>, and <body>
elements must also be present, and the xmlns attribute in <html> must
specify the xml namespace for the document.
Example
Here is an XHTML document with a minimum of required tags:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Title
of document</title>
</head>
<body>
some content here...
</body>
</html>
XHTML Elements Must be Properly Nested
In XHTML, elements must always be properly nested within each
other, like this:
Correct:
<b><i>Some
text</i></b>
Wrong:
<b><i>Some
text</b></i>
XHTML Elements Must Always be Closed
In XHTML, elements must always be closed, like this:
Correct:
<p>This
is a paragraph</p>
<p>This
is another paragraph</p>
Wrong:
<p>This
is a paragraph
<p>This
is another paragraph
XHTML Empty Elements Must Always be Closed
In XHTML, empty elements must always be closed, like this:
Correct:
A break: <br />
A horizontal rule: <hr />
An image: <img src="happy.gif" alt="Happy
face" />
Wrong:
A break: <br>
A horizontal rule: <hr>
An image: <img src="happy.gif" alt="Happy
face">
XHTML Elements Must be in Lowercase
In XHTML, element names must always be in lowercase, like this:
Correct:
<body>
<p>This
is a paragraph</p>
</body>
Wrong:
<BODY>
<P>This
is a paragraph</P>
</BODY>
XHTML Attribute Names Must be in Lowercase
In XHTML, attribute names must always be in lowercase, like this:
Correct:
<a href="https://fixhubblog.blogspot.com/html/">Visit our HTML tutorial</a>
Wrong:
<a HREF=" https://fixhubblog.blogspot.com/html/">Visit our HTML tutorial</a>
XHTML Attribute Values Must be Quoted
In XHTML, attribute values must always be quoted, like this:
Correct:
<a href=" https://fixhubblog.blogspot.com/html/">Visit our HTML tutorial</a>
Wrong:
<a href= https://fixhubblog.blogspot.com/html/>Visit our HTML tutorial</a>
XHTML Attribute Minimization is Forbidden
In XHTML, attribute minimization is forbidden:
Correct:
<input type="checkbox" name="vehicle" value="car" checked="checked" />
<input type="text" name="lastname" disabled="disabled" />
Wrong:
<input type="checkbox" name="vehicle" value="car" checked />
<input type="text" name="lastname" disabled />