Reading history

HTML Links and Images: href, target, alt and Paths

5 min read Updated 2 views

Learn HTML links and images: absolute and relative URLs, new tabs, email, phone and page links, img with alt, width, height and figure.

Links are what make the web a web: the <a> element connects your page to other pages, to sections of the same page, to email addresses and phone numbers. Images bring pages to life, and the <img> element, used well, also helps accessibility, speed and search rankings. This lesson covers both, including absolute and relative paths and why alt text matters.

In this lesson you will learn

  • How to create links with <a href>
  • Absolute vs relative URLs
  • Opening links in a new tab safely
  • Email, phone and same-page links
  • Adding images with alt, width, height and captions
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Links</title>
</head>
<body>
  <p><a href="https://coreteched.com/">Absolute link to Core Tech ED</a></p>
  <p><a href="/learn/python/">Relative link to the Python course</a></p>
  <p><a href="https://example.com/" target="_blank" rel="noopener">Opens in a new tab</a></p>
  <p><a href="mailto:hello@example.com">Send an email</a></p>
  <p><a href="tel:+8801700000000">Call us</a></p>
  <p><a href="#contact">Jump to the contact section</a></p>
  <h3 id="contact">Contact</h3>
</body>
</html>

Result

  • An absolute URL includes the full address (https://...) and is used for other websites.
  • A relative URL such as /learn/python/ points to a page on the same site and keeps working if the domain changes.
  • target="_blank" opens a new tab. Add rel="noopener" so the new page cannot control yours.
  • mailto: and tel: links open the email app or phone dialler.
  • href="#contact" jumps to the element with id="contact" on the same page.

Write descriptive link text

“Read the Python course” tells users and search engines where a link goes. “Click here” does not.

Images

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Images</title>
  <style> img { border-radius: 8px; } </style>
</head>
<body>
  <figure>
    <img src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='240' height='120'%3E%3Crect width='240' height='120' fill='%23006a4e'/%3E%3Ccircle cx='105' cy='60' r='40' fill='%23f42a41'/%3E%3C/svg%3E"
         alt="A green rectangle with a red circle, like the flag of Bangladesh"
         width="240" height="120">
    <figcaption>An image with alt text, a size and a caption.</figcaption>
  </figure>
  <img src="missing-photo.jpg" alt="Photo of the Dhaka skyline" width="240" height="60">
</body>
</html>

Result

  • src is the image address. Here it is a small SVG written directly in the page; usually it is a file path such as images/flag.png.
  • alt describes the image for screen readers and is shown when the image fails to load. The second image’s file does not exist, so the browser shows its alt text instead.
  • width and height let the browser reserve space before the image loads, which stops the page from jumping around.
  • <figure> and <figcaption> attach a visible caption.

Choosing an Image Format

FormatBest for
JPEGPhotos
PNGScreenshots and images that need transparency
WebP / AVIFModern formats, much smaller files for photos and graphics
SVGLogos and icons; sharp at any size

Add loading="lazy" to images further down the page so they load only when the visitor scrolls near them.

Common Mistakes

  1. Missing alt text. Use an empty alt="" only for purely decorative images.
  2. Uploading huge photos and shrinking them with HTML. Resize and compress the file itself.
  3. Wrong relative paths, such as images/a.png when the file is one folder up (../images/a.png).
  4. Nesting a link inside another link, which is not allowed.

Practice Exercises

Exercise 1. Make an image that links to the home page.

Show answer
<a href="/"><img src="logo.png" alt="Core Tech ED home" width="120" height="40"></a>

Exercise 2. Add a “Back to top” link at the bottom of a page.

Show answer

Give the first heading id="top", then add <a href="#top">Back to top</a>.

Frequently Asked Questions

How do I make a link download a file?

Add the download attribute: <a href="notes.pdf" download>Download notes</a>. It works for files on the same site.

Does alt text help SEO?

Yes. Search engines use alt text to understand images, and it is required for accessibility.

Summary

  • <a href> links to pages, page sections, email and phone.
  • Use relative URLs within your site and rel="noopener" with new tabs.
  • Every meaningful image needs alt text, plus width and height.
  • Pick the right format and lazy-load images further down the page.

Next: HTML Lists and Tables.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *