No, really, WAY LONG
Table of Contents
Introduction to Attributes
The Anchor Element and the HREF Attribute
The Target Attribute
The Name Attribute
HTML Lesson #2
So, I know that Francine requested Tables for the next HTML lesson, but personally, I think that's a little bit too advanced right now. So, I'm going to give you an element that uses attributes, so that you are ready for tables in the near future.
Last time, I went over the parts of your code, tags and elements. If you do not remember this information, now would be a good time to go back and review. No matter what code you are using, the structure of tags and elements will not change. To review this information, go here.
Today we are going to add another part to your elements: attributes. Attributes break down a code so that you can customize it even further. Not all elements use attributes. For example, the elements from last time do not. Then again, there is not much to customize when making your text bold, now is there?
Here is what W3Schools says about attributes:
HTML Attributes
HTML tags can have attributes. Attributes provide additional information about the HTML element.
Attributes always come in name/value pairs like this: name="value".
Attributes are always specified in the start tag of an HTML element.
When defining attributes, there are three main things to remember. First, your attribute will always be added to the opening tag of your element. This means that you will add all attributes for an element between the first set of < and >, but after the original designation.
Second, attributes always need both a name and value to work. You must first define the attribute of the element you want to change, and then define in what way you want to modify that attribute. Without a name or value an attribute will not do anything.
Third, all values must be enclosed in quotes. It does not matter if you use single quotes ('value') or double quotes ("value"), unless there are quotes in the value. For example, to define the name attribute as Swanky, you are free to use single or double quotes. However, to define the name attribute as Heather "Swanky" Swank, you must use single quotes because there are already double quotes in the value.
The Anchor Element and the HREF Attribute
One element that uses attributes is anchor, the link element. The tag for anchor is <a>. There are three main attributes you will use with this element- href, target, and name.
HREF is used to define your destination. You will use this attribute in nearly every single anchor element you create. HREF is where you type the address you are trying to link to. Let's walk through a very simple anchor element using the HREF attribute:
For this example, we will link to my blog: tales-of-swanky.blogspot.com
- Begin your element with the opening tag with appropriate designation, but do not close the tag: <a
- Next, add your attribute name: <a href=
- Define the attribute value, the address to link to- do not add the ending / to any link when defining an attribute value- and close the tag: <a href="http://tales-of-swanky.blogspot.com">
- Add the text you wish to be displayed as a link: <a href="http://tales-of-swanky.blogspot.com">Tales.of.Swanky
- Close the element with the appropriate ending tag: <a href="http://tales-of-swanky.blogspot.com">Tales.of.Swanky</a>
If you've completed all the steps correctly, your links should display as follows:
Tales.of.Swanky
If your link does not work properly, go over the steps again and make sure you did not miss any of the aspects of you element. Pay close attention to the spelling of your address, and make certain that you have both added the http:// before the address and removed any forward slashes (/) that may appear at the end of your address. The forward slash at the end of an address will prevent your link from working as it confuses the HTML, so always be sure that you have removed it.
The Target Attribute
Now, you will notice, if you click on that link, that it redirects this page whereas most of my links open in a new window or tab, depending on your web browser. This is because there is no target attribute defined in the above example.
The target attribute is used to define where your link will open- the same window/tab or a new window/tab. If you do not include the target attribute the default setting of "same window/tab" will be used. To change this, you will have to add the target attribute and define the value as "_blank". This tells HTML to open the link in a blank window/tab. Let's add the target attribute to the previous example.
- First, open your element: <a
- Next, add your href attribute: <a href="http://tales-of-swanky.blogspot.com"
- Add your next attribute name: <a href="http://tales-of-swanky.blogspot.com" target=
- Define your attribute value and close the tag: <a href="http://tales-of-swanky.blogspot.com" target="_blank">
- Define your hyper-link text: <a href="http://tales-of-swanky.blogspot.com" target="_blank">Tales.of.Swanky
- Close the element with the appropriate ending tag: <a href="http://tales-of-swanky.blogspot.com" target="_blank">Tales.of.Swanky</a>
You're link should now display as follows, and open in a new window/tab:
Tales.of.Swanky
If you are having problems, make sure that you have the quotations around all of your attribute values, check your address, and check that you have added the underscore (_) at the beginning of your target attribute value.
The Name Attribute
The last attribute you will use with the anchor element is name. The name attribute, when used with anchor creates a *gasp* Named Anchor, which allows you to link to a specific section of a page, rather than to the beginning of the page. You can see examples of this in use at the very beginning of this entry with the table of contents. If you missed them, click here to see them again.
Yes, that was another example. Named Anchors are most usefully when creating tables of contents at the beginning of large pages. I do not use this attribute often, to be honest, though it is very handy to have in your repertoire. It makes organizing and navigating your page a breeze. However, it does make your anchor elements a bit more involved.
When using named anchors you will not only define the hyper-text (as we did above) but you will define the target-text as well. As with most of HTML, this is not as difficult as it sounds.
First, you should decide where you want your named anchors to appear and where you want them to link to- this is defining your target-text. For our examples, I will walk you through creating the Table of Contents at the beginning of this lesson.
There are four logical places to create 'chapters' to this lessons: the introduction of attributes and the three examples of attributes using the anchor element.
The first thing you have to do when creating named anchors is define your values and select your target-text. This part of the process will happen in your page or entry. You will write up your actual table of contents later.
- First, open your anchor element: <a
- Next, add your name attribute: <a name=
- Add what ever text you wish, but remember what you use where, and close the tag: <a name="intro">
- Next, type to text you wish to link to: <a name="intro">HTML Lesson #2
- Lastly, close the tag: <a name="intro">HTML Lesson #2</a>
If you have defined the value correctly, the text will not display any differently.
Repeat this process for each occurrence of text on the page you want to link to. Remember to make note of what name you gave to each section of text. Through out my entry, I used "HTML Lesson #2", "The Anchor Element and the HREF Attribute", "The Target Attribute", and "The Name Attribute", defining them as "intro", "href", "target", and "name" respectively.
The second step in creating named anchors is to create the hyper-text links, or your table of contents. If you are linking from with in the same page, such as from one part of this entry to another, your addresses will be much simpler, but you can link to a certain section of a different page, as well. For the following examples, we will be linking to a section of the same page.
- First, go to where you want to insert your links, or table of contents.
- Open your anchor element: <a
- Add your href attribute to create the link: <a href=
- Add the value you defined for the first link, proceeded by a number sign (#) and close the tag: <a href="#intro">
- Add the text you want to use as your actual link: <a name="#intro">HTML Lesson #2
- Close the element with the appropriate tag: <a name="#intro">HTML Lesson #2</a>
If you have created your link correctly it should display like any other link. Repeat this process for each named anchor you have created. Remember to change the value for each link and do not forget to add the number sign to each value. Adding the number sign is what distinguishes the target-text from the actual link, or hyper-text. Using the name attribute with out the number sign defines where to link to- the target-text. Using the name attribute with the number sign defines the text to be used as a link- the hyper-text.
If you have created your table of contents correctly, the HTML code should look like this:
<a href="#intro">HTML Lesson #2</a>
<a href="#href">The Anchor Element and the HREF Attribute</a>
<a href="#target">The Target Attribute</a>
<a href="#name">The Name Attribute</a>
And display and function like this:
HTML Lesson #2
The Anchor Element and the HREF Attribute
The Target Attribute
The Name Attribute
Now, if you were attempting to link to a named anchor on a different page you had written, the actual address in the above example would be a little different. You would have to add the full address of the page the named anchor is on before the number sign in your link address. For an example, I have added a named anchor to the line "First, let me give you a basic understand of what HTML is and how it is used:" from my first HTML lesson that I will now show you how to link to from this lesson. I have already used this same named anchor in this entry, which you can see at the end of this paragraph.
First, you must either have already defined the target-text on your other page or go back and do so now. Again, just follow the steps here to do this.
Next you must create your hyper-text. We will follow the same process as before, with two alterations- adding the address of the external page to our href attribute value, and adding the target attribute. I always suggest using the target attribute when linking to an external page. I know I hate it when I click on a link and it doesn't open in a new tab because I loose what ever I was looking a to begin with. Because of this, I like to make sure all my external links open to a new window/tab.
For this example, the address of the entry we will be linking to is tales-of-swanky.blogspot.com/2008/10/cultural-expansion-through-language.html, and the value name defined on that page is "lesson1".
- First, go to where you want to insert your link.
- Open your anchor element: <a
- Add your href attribute to create the link: <a href=
- Add the value you defined for the first link, proceeded by a number sign (#)and the address of the external page you are linking to: <a href="http://tales-of-swanky.blogspot.com/2008/10/cultural-expansion-through-language.html#lesson1"
- Add the target attribute and value, and close the tag: <a href="http://tales-of-swanky.blogspot.com/2008/10/cultural-expansion-through-language.html#lesson1" target="_blank">
- Add the text you want to use as your actual link: <a href="http://tales-of-swanky.blogspot.com/2008/10/cultural-expansion-through-language.html#lesson1" target="_blank">HTML Lesson #1
- Close the element with the appropriate tag: <a href="http://tales-of-swanky.blogspot.com/2008/10/cultural-expansion-through-language.html#lesson1" target="_blank">HTML Lesson #1</a>
Remember to add the number sign and value to the end of your address. If your link is not working, go back and make sure that your values are the same on both pages. In this example, the code for the target-text in Lesson 1 should look like this:
<a name="lesson1">First, let me give you a basic understand of <b>what HTML is and how it is used</b>:</a>
And should display as normal text:
First, Let me give you a basic understand of what HTML is and how it is used:
While the hyper-text in this lesson looks like this:
<a href="http://tales-of-swanky.blogspot.com/2008/10/cultural-expansion-through-language.html#lesson1" target="_blank">HTML Lesson #1</a>
And displays and functions like this:
HTML Lesson 1
Well, that is all I have for you this week. I hope this wasn't too much for you to digest in one go. I want you to be ready when I give you tables, and this is the bes introduction of attributes that I could think of. Also, it is possibly the most useful. Links are a wonderful thing, and as you've seen, can do much more than you would have thought!
Next time, I'll give you one more set of examples using attributes, and there will even be some crossover with this lesson. Oh yes, we are getting fancy now. ^_^
And now I have to go to bed as it is WAY to late for good little Swankies to be awake. Getting up for work tomorrow is going to be so hard...
Read More...
