Quackit Logo
HTML
CSS
Scripting
Database
Hosting
Design
XML

HTML Links

Links, otherwise known as hyperlinks, are defined using the <a> tag - otherwise known as the anchor element.

To create a hyperlink, you use the <a> tag in conjunction with the href attribute (href stands for Hypertext Reference). The value of the href attribute is the URL, or, location of where the link is pointing to.

Example:

CodeResult

Create a website with this Website Builder

Hypertext references can use absolute URLS, relative URLs, or root relative URLs.

absolute
This refers to a URL where the full path is provided. For example, http://www.quackit.com/html/tutorial/
relative
This refers to a URL where only the path, relative to the current location, is provided. For example, if we want to reference the http://www.quackit.com/html/tutorial/ URL, and our current location is http://www.quackit.com/html/, we would use tutorial/
root relative
This refers to a URL where only the path, relative to the domain's root, is provided. For example, if we want to reference the http://www.quackit.com/html/tutorial/ URL, and the current location is http://www.quackit.com/html/, we would use /html/tutorial/. The forward slash indicates the domain's root. This way, no matter where your file is located, you can always use this method to determine the path, even if you don't know what the domain name will eventually be.

Link Targets

You can nominate whether to open the URL in a new window or the current window. You do this with the target attribute. For example, target="_blank" opens the URL in a new window.

The target attribute can have the following possible values:

_blankOpens the URL in a new browser window.
_selfLoads the URL in the current browser window.
_parentLoads the URL into the parent frame (still within the current browser window). This is only applicable when using frames.
_topLoads the URL in the current browser window, but cancelling out any frames. Therefore, if frames were being used, they aren't any longer.

Example:

CodeResult

Check out this stir-fried chicken and basil recipe.

Named Anchors

You can make your links "jump" to other sections within the same page. You do this with named anchors.

To use named anchors, you need to create two pieces of code - one for the hyperlink (this is what the user will click on), and one for the named anchor (this is where they will end up).

This page uses a named anchor. I did this by performing the steps below:

  1. I created the named anchor first (where the user will end up)

    Example HTML Code:

    Code

  2. I then created the hyperlink (what the user will click on). This is done by linking to the name of the named anchor. You need to preceed the name with a hash (#) symbol.

    Example HTML Code:

    Code

This results in:

CodeResult

Link Targets
When you click on the above link, this page should jump up to the "Link Targets" section (above). You can either use your back button, or scroll down the page to get back here.

You're back? Good, now lets move on to email links.

Email Links

You can create a hyperlink to an email address. To do this, use the mailto attribute in your anchor tag.

Example:

CodeResult

Email King Kong
Clicking on this link should result in your default email client opening up with the email address already filled out.

You can go a step further than this. You can auto-complete the subject line for your users, and even the body of the email. You do this appending subject and body parameters to the email address.

CodeResult

Email King Kong

Base href

You can specify a default URL for all links on the page to start with. You do this by placing the base tag (in conjunction with the href attribute) in the document's <head>.

Example HTML Code:

Code

Enjoy this page?

  1. Link to this page (copy/paste into your own website or blog):
  2. Link to Quackit using one of these banner ads.

Thanks for supporting Quackit!

Sponsored Link: Design CODE-FREE Websites