Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

HTML Introduction to HTML and CSS (2016) HTML: The Structural Foundation of Web Pages and Applications Review: Common HTML tags

gene c
gene c
13,630 Points

how does putting an <img> tag inside of an <a> tag make it a link?

Does this mean that when you click the image on the website, you'll be redirected to the source of the image?

Think of an a tag as a box that wraps the element. you can think of it as an invisible border around and including everything within.

if you wrap an image in an a tag the image is clickable, it will direct you to the href of the a tag. So if you want the image to click to the source of the image. You have to use the same url to the image in the href.

img tag says RENDER this href attribute says GO here

2 Answers

The <a></a> anchor element creates a link around most elements that it encapsulates. Where that link goes to is based upon the src source attribute. So for example if we consider this:

<a href="http://www.google.com">
    <img src="./kitten.jpg" />
</a>

The kitten image would now be a clickable image because it is wrapped in the anchor element. Clicking on the image would take you to Google. You don't have to put any elements within an anchor, you can simply put a string of text like this:

<a href="http://www.google.com">Go to Google</a>

And it would still work the same way. Whatever is encapsulated within the anchor tags becomes a link, with the exception of certain elements like buttons.

Andrey Misikhin
Andrey Misikhin
16,529 Points
<a href="https://www.google.ru/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"><img src="https://www.google.ru/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"></a>