In part 1 of this series, I started the basics of what HTML is and how html tags work. In this second part, I will go into more detail to show how to use tags with attributes on them to make links and include images in your html pages.
What is an attribute?
HTML tags can have attributes on them, which are simply named values that add additional details to the tag. An attribute on an html tag can specify a page to link to, an image to display, the placeholder text in a textbox, etc. They are included as part of the tag using the attribute name and value, as shown below:
[html light=”true”]
[raw]
<img alt="Blue Honda Accord" src="newcar.jpg">
<a href="http://www.microsoft.com">Microsoft</a>
[/raw]
[/html]
You have the name of the attribute, an equal sign, then the value in single or double quotes. If there is more than one attribute, they are separated with a space.
In the examples above, alt and src are attributes of the <img> tag, and href is an attribute of the <a> tag. Tags share some attribute names in common, but each tag often has some attributes that are specific to that tag. The individual attributes will be explained in more detail below.
Like the tag itself, the attribute names are not case sensitive, and they can appear in any order on the tag. The same attribute cannot be duplicated on the same tag.
Creating Links
While you would think the obvious tag to create a link would be the link tag, the tag used for links is actually the <a> tag. There is a <link> tag, but it serves a completely different purpose (it is used to include an external style sheet into a page).
Instead the <a> tag (for anchor) is used to create links to pages within your site, to external sites, and to places within the current page or another page.
The <a> tag’s main attribute is called href (which stands for hypertext reference). You use href to define the page or site that you are linking to. You put the label for the link (what the link says on the page) in between the opening and closing a tags. Here are some examples – note that all of these would go inside the <body> tag in your page:
[html]
[raw]
<a href="http://www.google.com">Google</a>
<a href="page2.html">Next page</a>
<a href="#top">Return to top</a>
[/raw]
[/html]
The first example links to Google.com. Since this is a link to an external site, you need to include the full URL including the http:// or https:// – you can’t just put www.google.com. Any time you link to something outside of the site your page is on, you have to use the full URL. Note: you can always use a full URL even for pages within your own site.
The second example links to another page in the same site with a name of page2.html. This is known as a relative path or link, which is explained in more detail here. Again, you can also use the full URL to the page instead (ie.. http://www.mysite.com/page2.html).
Finally, the third example links to a place in the current page called “top”. The # sign in front of the name indicates that it is a link to a place within the document. For more information on how to define places within a page to link to, see this.
Note: there is an attribute you can use to indicate that a link should open a new page instead of replacing the existing one. It is the target attribute. You use the “_blank” value to specify that a link opens up a new tab or window. For more information on target, see this.
Here is a sample page with some different links in it. It includes a tag you’ve not seen yet, which is <br>. It is used to put in a line break since HTML ignores line breaks in your html file. One of the links also uses target=”_blank” to open in a new window.
Note: <br> It is one of the few HTML tags that does not have a closing tag. It is used by itself to indicate a line break. You will sometimes see <br/> (which is a combined opening and closing tag), but <br> is perfectly valid and most common.
Adding Images
To add an image to a web page, you use the <img> tag. Like <br>, the <img> tag has no closing tag. Its main attributes are src (source), which specifies the name and location of the image, and alt (alternative) which provides alternate descriptive text for the picture that is important for accessibility purposes and as a placeholder while the image loads or if it fails to load).
Don’t confuse alt text with a caption – the alt text is not usually displayed. Unless the image is simply decorative, you should always provide alt text describing the meaning of the image.
Examples:
[html][raw]
<img src="http://www.commonchaos.com/wp-content/uploads/2011/07/happy-cat.jpg" alt="Happy cat picture">
<img src="cat.jpg" alt="another cat picture">
[/raw]
[/html]
Like links using the <a> tag, if the image is not on the current website, you must use the full URL to the image to include it, including the http:// or https://. This is shown in example # 1 above. It uses the full URL to the image on an external site.
In example number 2, only the file name was specified. The browser would look for the image in the same folder as the web page referencing it. The same rules about relative paths with links applies to images. More details on using relative paths can be found here. It’s simply a way to refer to a file that is in a different location on your site than the current page.
You can also combine a link and an image so that a link is opened when you click on an image. This is often used to show a smaller picture on a page, and link to a more detailed version. To do this, you simply put the <img> tag in between the opening and closing <a> tag with the link specified on it. Example:
[html light=”true”]
[raw]
<a href="largepicture.jpg"><img src="smallpicture.jpg" alt="Sunset at the beach"></a>
[/raw][/html]
I’ve created a sample page that shows this in action. It has a picture from an external site, a local image, and an image that is linked to another image. You can view the live sample here
The sample page uses the <h3> tag, which will be explained in an upcoming post on text formatting. It’s a heading tag that indicates the text in between is a heading. As a reminder, you can right click on the sample page in your browser and select View Source to see the HTML I used to create the page.
Summary
In this post, you built on what you learned in the first post about html tags and their use. You learned that attributes are settings on an HTML tag that give details like what picture to show, what page to link to, etc. You learned that the <a> tag, along with its href attribute, is used to create links to other pages, sites, and sections of pages. Finally, you learned how to show an image in a web page using the <img> tag along with its src attribute to specify the image to use and the alt attribute to label it. Combining both techniques, you also learned to add a link to an image.
As an exercise, try creating a page to display some of your favorite pictures. You can either use the full URL to them online, or save them locally and put them in the same folder with your html file and use their file name. Link one of the pictures to a site or a larger version of the same image.
Also try creating a list of your favorite web page links. Add a link for each site and give it a name (put the link text between the opening and closing <a> tags). Use <br> to separate each line. You can also try creating a second page in the same folder and linking to it using the file name of the new file you create (ie..page2.html).
In the next part we will cover some of the tags that help to format your text and give your page structure, such as headers, footers, etc.
Resources:
Attribute Reference (list of attributes for each HTML tag)
What are relative paths/links? (understanding how to use a relative link to a page or image)
W3 Attribute Specification (explains attributes and when to use single or double quotes)
Target Attribute of the Anchor Tag (explains the values of target on a link)
Alternative Text (understanding the importance of the ALT attribute for accessibility)
.


