HTML - Hyperlinks

HTML Hyperlinks

1. Introduction to Hyperlinks

Hyperlinks are one of the fundamental features of the web. A hyperlink allows users to navigate from one page to another or to a specific part of the same page. In HTML, hyperlinks are created using the <a> (anchor) tag. This tag links an element to a destination such as a web page, an email address, or an external resource.

2. Syntax of a Hyperlink

The basic syntax for a hyperlink is as follows:

    <a href="URL">Link Text</a>
    

Here, the href attribute defines the target URL, and the content inside the <a> tag (known as link text) is the clickable part of the hyperlink.

Example of a Simple Hyperlink:

    <a href="https://www.example.com">Click Here to Visit Example Website</a>
    

Click Here to Visit Example Website

3. Types of Hyperlinks

There are several types of hyperlinks in HTML. These links can either navigate within the same page, to other pages, or to external websites.

3.1. External Links

External links are hyperlinks that take the user to an external website or resource, such as another website.

    <a href="https://www.google.com">Visit Google</a>
    

Visit Google

3.2. Internal Links

Internal links point to a different page within the same website or to a specific section of a page.

    <a href="about.html">About Us</a>
    

About Us

3.3. Anchor Links (Jump Links)

Anchor links are used to navigate to a specific section of a webpage. These are particularly useful for long pages where users want to jump to a particular content section.

First, we define an anchor with a unique ID:

    <h2 id="contact">Contact Us</h2>
    

Then, we create a link that points to that section using the ID:

    <a href="#contact">Go to Contact Us</a>
    

Go to Contact Us

3.4. Email Links

Email links allow users to send an email by clicking the link. To create an email link, you use the mailto: scheme followed by the email address.

    <a href="mailto:info@example.com">Send an Email</a>
    

Send an Email

4. Link Target Attribute

The target attribute specifies where to open the linked document. By default, hyperlinks open in the same browser window. You can modify this behavior using the target attribute:

4.1. Open Link in Same Window (default behavior)

    <a href="https://www.example.com">Visit Example</a>
    

Visit Example

4.2. Open Link in New Tab or Window

To open a link in a new tab or window, set the target attribute to _blank.

    <a href="https://www.example.com" target="_blank">Open in New Tab</a>
    

Open in New Tab

4.3. Open Link in Parent Frame

If you are using frames, you can set the link to open in the parent frame using _parent:

    <a href="https://www.example.com" target="_parent">Open in Parent Frame</a>
    

4.4. Open Link in a Specific Frame

If you're using multiple frames, you can target a specific frame by setting the target attribute to the name of the frame.

5. Link Titles and Tooltips

Using the title attribute in a hyperlink provides additional information about the link. This text appears as a tooltip when the user hovers over the link.

    <a href="https://www.example.com" title="Visit Example Website">Visit Example</a>
    

Visit Example

6. Styling Hyperlinks

CSS can be used to style hyperlinks. You can modify the color, underline, and other properties of links based on their state (e.g., visited, hover, active).

6.1. Default Link Styles

- Links are typically blue and underlined by default.

6.2. Styling Links with CSS

You can style links using the following pseudo-classes: - :link — styles links that haven’t been clicked yet. - :visited — styles links that have been clicked. - :hover — styles links when the user hovers over them. - :active — styles links when they are clicked.
    <style>
    a:link {
        color: blue;
    }
    a:visited {
        color: purple;
    }
    a:hover {
        color: red;
        text-decoration: underline;
    }
    a:active {
        color: orange;
    }
    </style>
    

Hyperlinks are one of the most important features of the web. They enable navigation between different web pages, sections, and external resources. Understanding how to use the <a> tag with the href attribute, the target attribute, and how to style links using CSS is essential for creating interactive and accessible websites.

logo

HTML

Beginner 5 Hours

HTML Hyperlinks

1. Introduction to Hyperlinks

Hyperlinks are one of the fundamental features of the web. A hyperlink allows users to navigate from one page to another or to a specific part of the same page. In HTML, hyperlinks are created using the

<a> (anchor) tag. This tag links an element to a destination such as a web page, an email address, or an external resource.

2. Syntax of a Hyperlink

The basic syntax for a hyperlink is as follows:

    <a href="URL">Link Text</a>
    

Here, the href attribute defines the target URL, and the content inside the <a> tag (known as link text) is the clickable part of the hyperlink.

Example of a Simple Hyperlink:

    <a href="https://www.example.com">Click Here to Visit Example Website</a>
    

Click Here to Visit Example Website

3. Types of Hyperlinks

There are several types of hyperlinks in HTML. These links can either navigate within the same page, to other pages, or to external websites.

3.1. External Links

External links are hyperlinks that take the user to an external website or resource, such as another website.

    <a href="https://www.google.com">Visit Google</a>
    

Visit Google

3.2. Internal Links

Internal links point to a different page within the same website or to a specific section of a page.

    <a href="about.html">About Us</a>
    

About Us

3.3. Anchor Links (Jump Links)

Anchor links are used to navigate to a specific section of a webpage. These are particularly useful for long pages where users want to jump to a particular content section.

First, we define an anchor with a unique ID:

    <h2 id="contact">Contact Us</h2>
    

Then, we create a link that points to that section using the ID:

    <a href="#contact">Go to Contact Us</a>
    

Go to Contact Us

3.4. Email Links

Email links allow users to send an email by clicking the link. To create an email link, you use the mailto: scheme followed by the email address.

    <a href="mailto:info@example.com">Send an Email</a>
    

Send an Email

4. Link Target Attribute

The target attribute specifies where to open the linked document. By default, hyperlinks open in the same browser window. You can modify this behavior using the target attribute:

4.1. Open Link in Same Window (default behavior)

    <a href="https://www.example.com">Visit Example</a>
    

Visit Example

4.2. Open Link in New Tab or Window

To open a link in a new tab or window, set the target attribute to _blank.

    <a href="https://www.example.com" target="_blank">Open in New Tab</a>
    

Open in New Tab

4.3. Open Link in Parent Frame

If you are using frames, you can set the link to open in the parent frame using _parent:

    <a href="https://www.example.com" target="_parent">Open in Parent Frame</a>
    

4.4. Open Link in a Specific Frame

If you're using multiple frames, you can target a specific frame by setting the target attribute to the name of the frame.

5. Link Titles and Tooltips

Using the title attribute in a hyperlink provides additional information about the link. This text appears as a tooltip when the user hovers over the link.

    <a href="https://www.example.com" title="Visit Example Website">Visit Example</a>
    

Visit Example

6. Styling Hyperlinks

CSS can be used to style hyperlinks. You can modify the color, underline, and other properties of links based on their state (e.g., visited, hover, active).

6.1. Default Link Styles

- Links are typically blue and underlined by default.

6.2. Styling Links with CSS

You can style links using the following pseudo-classes: - :link — styles links that haven’t been clicked yet. - :visited — styles links that have been clicked. - :hover — styles links when the user hovers over them. - :active — styles links when they are clicked.
    <style>
    a:link {
        color: blue;
    }
    a:visited {
        color: purple;
    }
    a:hover {
        color: red;
        text-decoration: underline;
    }
    a:active {
        color: orange;
    }
    </style>
    

Hyperlinks are one of the most important features of the web. They enable navigation between different web pages, sections, and external resources. Understanding how to use the <a> tag with the href attribute, the target attribute, and how to style links using CSS is essential for creating interactive and accessible websites.

Frequently Asked Questions for HTML

  • HTML stands for HyperText Markup Language.
  • It is used to create the structure of web pages and web applications.
  • HTML defines elements such as headings, paragraphs, links, images, and other content.

  • Block-level elements (like <div>, <p>, <h1>) start on a new line and take full width.
  • Inline elements (like <span>, <a>, <strong>) stay within the flow of the text.
  • Understanding this helps with layout and styling.

  • A basic HTML page includes a <!DOCTYPE html> declaration, followed by <html>, <head>, and <body>.
  • The <head> section contains metadata like the title and links to stylesheets.
  • The <body> section contains all the visible content of the webpage.

  • The <meta> tag provides metadata such as page description, keywords, and author.
  • It helps browsers and search engines understand the content of the page.
  • One common use is specifying the character encoding: <meta charset="UTF-8">.

  • Forms collect user input using the <form> tag.
  • Inside a form, use <input>, <textarea>, <select>, and <button>.
  • The action attribute specifies where to send the form data.

  • The <label> tag defines a label for an input element.
  • It improves accessibility and allows users to click the label to focus the input.
    Example: <label for="email">Email:</label><input id="email">.

Comments in HTML are written between <!-- and -->.

Example:
<!-- This is a comment -->.
Comments are not displayed on the webpage and are used for documentation.

HTML entities are used to display reserved or special characters.

For example, &lt; displays < and &amp; displays &.
Use them to avoid confusion with actual HTML syntax.