Thursday, July 7, 2011

Lesson 8: Links

What do I need to make a link?

To make links, you use what you always use when coding HTML: an element. A simple element with one attribute and you will be able to link to anything and everything. Here is an example of what a link to HTML.net could look like:

Example 1:

        
        <a href="http://www.html.net/">Here is a link to HTML.net</a>
        
        
Would look like this in the browser:
The element a stands for "anchor". And the attribute href is short for "hypertext reference", which specifies where the link leads to - typically an address on the internet or a file name.
In the above example the attribute href has the value "http://www.html.net", which is the full address of HTML.net and is called a URL (Uniform Resource Locator). Note that "http://" must always be included in URLs. The sentence "Here is a link to HTML.net" is the text that is shown in the browser as the link. Remember to close the element with an </a>.

What about links between my own pages?

If you want to make a link between pages on the same website, you do not need to spell out the entire address (URL) for the document. For example, if you have made two pages (let us call them page1.htm and page2.htm) and saved them in the same folder you can make a link from one page to the other by only typing the name of the file in the link. Under such circumstances a link from page1.htm to page2.htm could look like this:

Example 2:

        
        <a href="page2.htm">Click here to go to page 2</a>
        
        
If page 2 were placed in a subfolder (named "subfolder"), the link could look like this:

Example 3:

        
        <a href="subfolder/page2.htm">Click here to go to page 2</a>
        
        
The other way around, a link from page 2 (in the subfolder) to page 1 would look like this:

Example 4:

        
        <a href="../page1.htm">A link to page 1</a>
        
        
"../" points to the folder one level up from position of the file from which the link is made. Following the same system, you can also point two (or more) folders up by writing "../../".
Did you understand the system? Alternatively, you can always type the complete address for the file (URL).

What about internal links within a page?

You can also create internal links within a page - for example a table of contents at the top with links to each chapter below. All you need to use is a very useful attribute called id (identification) and the symbol "#".
Use the id attribute to mark the element to which you want to link. For example:
        
        <h1 id="heading1">heading 1</h1>
        
        
You can now create a link to that element by using "#" in the link attribute. The "#" must be followed by the id of the tag you want to link to. For example:
        
        <a href="#heading1">Link to heading 1</a>
        
        
All will become clear with an example:

Example 5:

        
        <html>
          
          <head>
          </head>
 
          <body>
 
               <p><a href="#heading1">Link to heading 1</a></p>
               <p><a href="#heading2">Link to heading 2</a></p>
 
               <h1 id="heading1">heading 1</h1>
               <p>Text text text text</p>
 
               <h1 id="heading2">heading 2</h1>
               <p>Text text text text</p>
          
          </body>
 
        </html>
        
        
will look like this in the browser (click on the two links):

Heading 1

Text text text text

Heading 2

Text text text text
(Note: An id attribut must start with a letter)

Can I link to anything else?

You can also make a link to an e-mail address. It is done in almost the same way as when you link to a document:

Example 6:

        
        <a href="mailto:nobody@html.net">Send an e-mail to nobody at HTML.net</a>
        
        
will look like this in the browser:
The only difference between a link to an e-mail and a link to a file is that instead of typing the address of a document, you type mailto: followed by an e-mail address. When the link is clicked, the default e-mail program opens with a new blank message addressed to the specified e-mail address. Please note that this function will only work if there is an e-mail program installed on your computer. Give it a try!

Are there any other attributes I should know of?

To create a link, you always have to use the href attribute. In addition, you can also put a titleon your link:

Example 7:

        
        <a href="http://www.html.net/" title="Visit HTML.net and learn HTML">HTML.net</a>
        
        
Would look like this in the browser:
The title attribute is used to type a short description of the link. If you - without clicking - place the cursor over the link, you will see the text "Visit HTML.net and learn HTML" appears.

Sunday, July 3, 2011

Lesson 7: Attributes

You can add attributes to a number of elements.

What is an attribute?

As you probably remember, elements give structure to a HTML document and tells the browser how you want your website to be presented (for example, <br/> informs the browser to make a line break). In some elements you can add more information. Such additional information is called an attribute.
Example 1:
        
        <h2 style="background-color:#ff0000;">My friendship with HTML</h2>
        
        
Attributes are always written within a start tag and are followed by an equals sign and the attribute details written between inverted commas. The semicolon after the attribute is for separating different style commands. We will get back to that later.

What is the catch?

There are many different attributes. The first one you will learn is style. With the style attribute you can add layout to your website. For instance a background colour:
Example 2:
        
        <html>
          
          <head>
          </head>
 
          <body style="background-color:#ff0000;">
          </body>
 
        </html>
        
        
will show a completely red page in the browser - go ahead and see for yourself. We will explain in greater detail how the colour system works in a few moments.
Note that some tags and attributes use US spelling i.e. color instead of colour. It is important that you are careful to use the same spelling as we use in the examples in this tutorial - otherwise, browsers will not be able to understand your codes. Also, don't forget to always close the inverted commas (quotation marks) after an attribute.

How did the page become red?

In the above example, we asked for the background colour with the code "#ff0000". This is the colour code for red using so called hexadecimal numbers (HEX). Each colour has its own hexadecimal number. Here are some examples:
White: #ffffff
Black: #000000 (zeros)
Red: #ff0000
Blue: #0000ff
Green: #00ff00
Yellow: #ffff00
A hexadecimal colour code consists of # and six digits or letters. There are more than 1000 HEX codes and it is not easy to figure out which HEX code is tied to a specific colour. To make it easier we have made a chart of the 216 most commonly used colours: 216 Web Safe Colour Chart.
You can also use the English name for the most common colours (white, black, red, blue, green and yellow).
Example 3:
        
        <body style="background-color: red;">
        
        
Enough about colours. Let's get back to the attributes.

Which elements can use attributes?

Different attributes can be applied to most elements.
You will often use attributes in tags such as the body tag while you will rarely use attributes in, for example, a br tag since a line break normally is a line break without any parameters to adjust.
Just as there are many different elements, so there are many different attributes. Some attributes are tailor made for one particular element while others can be used for many different element. And vice versa: some elements can only contain one kind of attribute while others can contain many.
It may sound a bit confusing but once you get acquainted with the different attributes it is actually very logical and you will soon see how easy they are to use and how many possibilities they provide.
This tutorial will introduce you to the most important attributes.

Exactly what parts does an element consist of?

Generally an elements consist of a start tag with or without one or more attributes, some content and an end tag. Simple as that. See the illustration below.

Saturday, July 2, 2011

Lesson 6: A few more elements

Did you manage to make a few pages on your own? If not, here is an example:
        
        <html>
 
          <head>
          <title>My website</title>
          </head>
 
          <body>
          <h1>A Heading</h1>
          <p>text, text text, text</p>
          <h2>Subhead</h2>
          <p>text, text text, text</p>
          </body>
          
        </html>
        
        

Now what?

Now it is time to learn seven new elements.
In the same way you emphasise the text by putting it between the openning tag <em> and the closing tag </em>, you can give stronger emphasis by using theopenning tag <strong> and the closing tag </strong>.
Example 1:
        
        <strong>Stronger emphasis.</strong>
        
        
Will look like this in the browser:
Stronger emphasis.
Likewise, you can make your text smaller using small:
Example 2:
        
        <small>This should be in small.</small>
        
        
Will look like this in the browser:
This should be in small.

Can I use several elements at the same time?

You can easily use several elements at the same time as long as you avoid overlapping elements. This is best illustrated by an example:
Example 3:
If you want to emphasise small text, it must be done like this:
        
        <em><small>Emphasised small text</small></em>
        
        
And NOT like this:
        
        <em><small>Emphasise small text</em></small>
        
        
The difference is that in the first example, we closed the tag we first opened last. This way we avoid confusing both ourselves and the browser.

More elements!

As mentioned in Lesson 3 there are elements which are opened and closed in the same tag. These so-called empty elements are not connected to a specific passage in the text but rather are isolated labels. An example of such a tag is <br /> which creates a forced line break:
Example 4:
        
        Some text<br /> and some more text in a new line 
        
        
Will look like this in the browser:
Some text
and some more text in a new line
Notice that the tag is written as a contraction of an opening and closing tag with an empty space and a forward slash at the end: <br />.
Another element that is opened and closed in the same tag is <hr /> which is used to draw a horizontal line ("hr" stands for "horizontal rule"):
Example 5:
        
        <hr />
        
        
Will look like this in the browser:


Examples of elements that needs both an opening tag and a closing tag - as most elements do - isul, ol and li. These elements are used when you want to make lists.
ul is short for "unordered list" and inserts bullets for each list item. ol is short for "ordered list" and numbers each list item. To make items in the list use the li tag ("list item"). Confused? See the examples:
Example 7:
        
        <ul>
          <li>A list item</li>
          <li>Another list item</li>
        </ul>
        
        
will look like this in the browser:
  • A list item
  • Another list item
Example 8:
        
        <ol>
          <li>First list item</li>
          <li>Second list item</li>
        </ol>
        
        
will look like this in the browser:
  1. First list item
  2. Second list item

Phew! Is that all?

That is all for now. Again, experiment and make your own pages using some of the seven new elements you learned in this lesson:
        
        <strong>Stronger emphasis</strong>
        <small>Small text</small>
        <br /> Line shift
        <hr /> Horizontal line
        <ul>List</ul>
        <ol>Ordered list</ol>
        <li>List item</li>
        
        
Related Posts Plugin for WordPress, Blogger...