HTML Links
HTML links, also known as Hyperlinks, are the cornerstone of web pages. Links allow users to navigate from one page to another, connecting the entire World Wide Web.
HTML uses the <a> tag (a is short for anchor) to create links.
hrefAttribute: This is the most important attribute, specifying the target address (URL) of the link.- Link Text: This is the part visible to users on the page and can be clicked.
Example:
The above code produces the following effect: Visit Google
The target Attribute
The target attribute specifies where to open the linked document. It has several common values:
-
_self: Default. Opens the link in the current browser window or tab. -
_blank: Opens the link in a new browser window or tab. This is commonly used for external links to prevent users from leaving your site. -
_parent: Opens the link in the parent frame (mainly used with<iframe>frame layouts). -
_top: Opens the link in the full browser window, breaking out of all frames.
Absolute URL vs. Relative URL
The address used in the href attribute can be absolute or relative.
-
Absolute URL: A complete web address pointing to another website.
-
Relative URL: Points to a file within the current website. It doesn't include the domain name.
- Link to a file in the same directory:
- Link to a file in a subdirectory:
- Link to a file in a parent directory:
- Link to a file in the same directory:
Best Practice: Always use relative URLs within your website. This way, even if your domain name changes, internal links will still work.
Using an Image as a Link
You can place an <img> inside an <a> tag to create a clickable image link.
Linking to a Specific Part of a Page (Bookmarks)
You can create links that jump directly to a specific section of the current page or another page. This requires two steps:
-
Create the target anchor: Use the
idattribute to create a unique identifier for the element you want to jump to. -
Create the link to the anchor: In the
hrefattribute, use#followed by the target'sid.- Jump to an anchor on the current page:
- Jump to an anchor on another page:
- Jump to an anchor on the current page:
mailto Links
You can create a special link that, when clicked, opens the user's default email client with the recipient address pre-filled.