Skip to main content

HTML Links and Images

Images can be used to display photos, logos and graphics on a webpage.


Use the a element to create a hyperlink.

<a href="page.html">Go to Page</a>

Result:

Go to Page

When clicked, the browser will open the specified page.


Linking to Another Website

Use a full web address when linking to an external website.

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

Result:

Visit Google


Add the target="_blank" attribute.

<a href="https://www.google.com"
   target="_blank"
   rel="noopener noreferrer">

    Visit Google

</a>

Result:

Visit Google

The rel attribute improves security when opening new tabs.


Linking to an Email Address

Use the mailto: protocol.

<a href="mailto:teacher@school.com">
    Email Teacher
</a>

Result:

Email Teacher

When clicked, the user's email program will open.


Creating an Image

Use the img element.

<img src="images/dog.jpg"
     alt="Photo of a dog">

The src attribute specifies the image location.

The alt attribute provides alternative text if the image cannot be displayed.

Photo of a Dog


Setting Image Width

Use the width attribute.

<img src="images/dog.jpg"
     width="100"
     alt="Photo of a dog">

The image will be displayed at 100 pixels wide.

Photo of a Dog

Responsive Images

A responsive image automatically scales to fit different screen sizes.

<img src="images/dog.jpg"
     style="max-width:100%;"
     alt="Photo of a dog">

This is useful for:

  • Phones
  • Tablets
  • Laptops
  • Desktop computers

Using an Image as a Link

Images can also be used as buttons.

<a href="dogs.html">

    <img src="images/dog.jpg"
         alt="Dog">

</a>

When the image is clicked, the browser will open the specified page.

Dog

A common use of images is displaying a website logo.

<img src="images/logo.png"
     width="200"
     alt="Company Logo">

Creating an Image Gallery

Multiple images can be displayed together.

<img src="images/dog1.jpg"
     width="200"
     alt="Dog 1">

<img src="images/dog2.jpg"
     width="200"
     alt="Dog 2">

<img src="images/dog3.jpg"
     width="200"
     alt="Dog 3">

Complete Example

<!DOCTYPE html>

<html>

<head>
    <title>Links and Images</title>
</head>

<body>

    <h1>Links and Images</h1>

    <a href="page.html">
        Internal Page
    </a>

    <br><br>

    <a href="https://www.google.com"
       target="_blank"
       rel="noopener noreferrer">

        Google

    </a>

    <br><br>

    <a href="mailto:teacher@school.com">
        Email Teacher
    </a>

    <br><br>

    <img src="images/dog.jpg"
         width="300"
         alt="Photo of a dog">

</body>

</html>

Quick Reference

<a href="page.html">
<a href="https://website.com">

New Tab

target="_blank"
<a href="mailto:someone@email.com">

Image

<img src="image.jpg"
     alt="Description">

Image Width

width="300"

Responsive Image

style="max-width:100%;"
<a href="page.html">

    <img src="image.jpg">

</a>

You now know how to create hyperlinks and display images in HTML.

Next tutorial: HTML Lists and Tables