HTML Links and Images (with File Paths Explained)
Links join pages together. Images make pages friendly. Both need something called an attribute.
What is an attribute?
An attribute gives extra information to a tag. It is written inside the opening tag like name="value".
name="value".<a href="https://www.wikipedia.org">Go to Wikipedia</a>Here a is the tag (a = anchor, which means link). href is the attribute. Its value is the address to open.
Three kinds of links
| Type | Example href | Use it for |
|---|---|---|
| Absolute | https://example.com/page.html | Other websites |
| Relative | about.html or blog/post.html | Pages on your own website |
| Same page | #contact | Jump to a part of the same page with id="contact" |
<a href="about.html">About me</a>
<a href="blog/first-post.html">My first post</a>
<a href="../index.html">Back to home</a>
<a href="#contact">Jump to contact</a>../ means "go up one folder".
Open a link in a new tab
<a href="https://example.com" target="_blank" rel="noopener">Open in new tab</a>target="_blank" opens a new tab. rel="noopener" keeps your page safe from the other website. Use new tabs only when really needed, because most people like to decide this themselves.
Email, phone and download links
Links can do more than open pages:
<a href="mailto:hello@example.com">Email us</a>
<a href="tel:+919800000000">Call us</a>
<a href="files/price-list.pdf" download>Download the price list</a>mailto:opens the visitor's email app with your address filled in.tel:starts a phone call on mobile. Write the number with the country code and no spaces.downloadtells the browser to save the file instead of opening it.
Make an image clickable
Put the <img> inside an <a>. This is how logos usually link back to the home page.
<a href="index.html">
<img src="images/logo.svg" alt="My Site home page" width="120" height="40">
</a>When an image is the only thing inside a link, the alt text should say where the link goes, not what the picture looks like.
Adding images
Use the <img> tag. It has no closing tag.
alt text. If the image does not load, the alt text shows instead.<img src="images/taj-mahal.jpg" alt="Taj Mahal in the morning sun" width="800" height="533">src: where the image file is.alt: a short description of the image. Blind users hear this text. Google also reads it. Always write alt text.widthandheight: the real size of the image. This stops the page from jumping while the image loads.
Which image format should I use?
| Format | Best for |
|---|---|
| JPG | Photos |
| PNG | Screenshots, images that need a see-through background |
| SVG | Logos and icons (stays sharp at any size) |
| WebP / AVIF | Modern format, much smaller files. Best for speed. |
images folder. Use small letters and dashes in file names: red-car.jpg, not Red Car (1).JPG.Keep image files small
A photo straight from a phone can be 4 to 8 MB. On a slow mobile connection, that one photo can take many seconds to load, and Google ranks slow pages lower. Before you add any photo:
- Resize it. If it will show at 800px wide, there is no need for a 4000px image. Save it at about 1600px wide at most (twice the size, for sharp screens).
- Compress it. Free tools like Squoosh make files much smaller without a visible change. Aim for under 200 KB for most photos.
- Use WebP where you can. It is usually much smaller than JPG for the same quality.
Mini project: a 3-page website
Make this folder structure:
my-website/
index.html
about.html
gallery.html
images/
photo1.jpg
photo2.jpgAdd the same small menu at the top of all three pages, so visitors can move between them:
<nav>
<a href="index.html">Home</a> |
<a href="about.html">About</a> |
<a href="gallery.html">Gallery</a>
</nav>On gallery.html, add two photos with good alt text and width and height. Click every link to check that it works. You now have a real multi-page website.
Why is my image or link broken?
A broken image shows a small icon or the alt text instead. Check these, in this order:
| Check | Example of the problem |
|---|---|
| Is the path right from this file? | The page is inside blog/, so it needs ../images/cat.jpg |
| Do capital letters match exactly? | The file is Cat.JPG but the code says cat.jpg. This often works on Windows but breaks online. |
| Is the file ending right? | .jpeg and .jpg are different names |
| Are there spaces in the name? | Rename my photo.jpg to my-photo.jpg |
Practice time
- Make two pages,
index.htmlandabout.html, and link them to each other. - Add one photo with good alt text.
- Add a link that jumps to the bottom of the page.
- Add an email link and a phone link to your About page.
- Compress one photo with Squoosh and compare the file size before and after.
Quick summary
- Attributes give extra info:
href,src,alt. - Use relative paths for your own pages, full URLs for other sites.
- Always add alt text and width/height to images.