Wednesday, 15 April 2015

Web Design - Part 1: HTML (Lesson 2)


In lesson one I talked about what HTML means, the basic step required and how to save and open html document. Before proceeding to lesson two, let get over some steps again.

HTML Tags

HTML tags are keywords (tag names) surrounded by angle brackets:
<tagname>content</tagname>
  • HTML tags normally come in pairs like <p> and </p>
  • 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, but with a slash before the tag name
Note The start tag is often called the opening tag. The end tag is often called the closing tag.

Web Browsers

The purpose of a web browser (Chrome, IE, Firefox, Safari) is to read HTML documents and display them.
The browser does not display the HTML tags, but uses them to determine how to display the document:
View in Browser

HTML Page Structure

Below is a visualization of an HTML page structure:
<html>
<head>
<title>Page title</title>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
</body>
</html>
Note Only the <body> area (the white area) is displayed by the browser.



In lesson two I will show you the next step needed.

LESSON 2

The <!DOCTYPE> Declaration

The <!DOCTYPE> declaration helps the browser to display a web page correctly.
There are different document types on the web.
To display a document correctly, the browser must know both type and version.
The doctype declaration is not case sensitive. All cases are acceptable:
<!DOCTYPE html>

<!DOCTYPE HTML>

<!doctype html>

<!Doctype Html>

Common Declarations

HTML5

<!DOCTYPE html>

HTML 4.01

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

XHTML 1.0

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Note All tutorials and examples at W3Schools use HTML5.

HTML Versions

Since the early days of the web, there have been many versions of HTML:
Version Year
HTML 1991
HTML 2.0 1995
HTML 3.2 1997
HTML 4.01 1999
XHTML 2000
HTML5 2014

HTML Basic Examples


Don't worry if these examples use tags you have not learned.
You will learn them as we proceed.

HTML Documents

All HTML documents must start with a 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>

<h1>My First Heading</h1>

<p>My first paragraph.</p>

</body>
</html>

HTML Headings

HTML headings are defined with the <h1> to <h6> tags:

Example

<h1>This is a heading</h1>
<h2>This is a heading</h2>
<h3>This is a heading</h3>


HTML Paragraphs

HTML paragraphs are defined with the <p> tag:

Example

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>


HTML Links

HTML links are defined with the <a> tag:

Example

<a href="http://joelmone.blogspot.com">This is a link</a>
The link address is specified in the href attribute.
Attributes are used to provide additional information about HTML elements.

HTML Images

HTML images are defined with the <img> tag.
The source file (src), alternative text (alt), and size (width and height) are provided as attributes:

Example

<img src="FileName.jpg" alt="JoelMone" width="104" height="142">


HTML documents are made up by HTML elements.

HTML Elements

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 HTML paragraph.</p>
Start tag Element content End tag
           <h1> My First Heading            </h1>
            <p> My first            </p>
           <br>
Note Some HTML elements do not have an end tag.

Nested HTML Elements

HTML elements can be nested (elements can contain elements).
All HTML documents consist of nested HTML elements.
This example contains 4 HTML elements:

Example

<!DOCTYPE html>
<html>

<body>
  <h1>My First Heading</h1>
  <p>My first paragraph.</p>
</body>

</html>


HTML Example Explained

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>

Don't Forget the End Tag

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>

The example above works in all browsers, because the closing tag is considered optional.
Never rely on this. It might produce unexpected results and/or errors if you forget the end tag.

Empty HTML Elements

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 elements can be "closed" in the opening tag like this: <br />.
HTML5 does not require empty elements to be closed. But if you want stricter validation, or you need to make your document readable by XML parsers, you should close all HTML elements.

HTML Tip: Use Lowercase Tags

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 demands lowercase for stricter document types like XHTML.




Attributes provide additional information about HTML elements.

HTML Attributes

  • HTML elements can have attributes
  • Attributes provide additional information about an element
  • Attributes are always specified in the start tag
  • Attributes come in name/value pairs like: name="value"

The lang Attribute

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:

Example

<!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).

The title Attribute

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":

Example

<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>
Note When you move the mouse over the element, the title will be displayed as a tooltip.

The href Attribute

HTML links are defined with the <a> tag. The link address is specified in the href attribute:

Example

<a href="http://joelmone.blogspot.com">This is a link</a>

Size Attributes

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:

Example

<img src="FileName.jpg" width="104" height="142">
The image size is specified in pixels: width="104" means 104 screen pixels wide.

The alt Attribute

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.

Example

<img src="FileName.jpg" alt="http://joelmone.blogspot.com" width="104" height="142">


We Suggest: Always Use Lowercase Attributes

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.
Note Lower case is the most common. Lower case is easier to type.
At W3Schools we always use lower case attribute names.

We Suggest: Always Quote Attribute Values

The HTML5 standard does not require quotes around attribute values.
The href attribute, demonstrated above, can be written as:

Example

<a href=http://joelmone.blogspot.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:

Example

<p title=About W3Schools>
 
Note Using quotes are the most common. Omitting quotes can produce errors. 

Single or Double Quotes?

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:

Example

<p title='John "ShotGun" Nelson'>
Or vice versa:

Example

<p title="John 'ShotGun' Nelson">

Lesson Summary

  • All HTML elements can have attributes
  • The HTML title attribute provides additional "tool-tip" information
  • The HTML href attribute provides address information for links
  • The HTML width and height attributes provide size information for images
  • The HTML alt attribute provides text for screen readers
  • At W3Schools we always use lowercase HTML attribute names
  • At W3Schools we always quote attributes with double quotes




Previous Lesson

No comments:

Post a Comment

DISCLAIMER: Comments are opinions of those that posted it and are subject to moderation.

Disqus for Joel Mone's Blog