HTMLLesson 4 of 10Jump to practice
Links and images
Connect pages with the a element and show pictures with img, using attributes to add the details.
So far every element has been a tag plus content. Links and images also need extra information: where the link goes, which image to show. That extra information goes in attributes.
An attribute sits inside the start tag and is written as name="value".
Links
A link uses the <a> element. The href attribute holds the address.
<p>
Read more on
<a href="https://developer.mozilla.org/">MDN Web Docs</a>.
</p>To open a link in a new tab, add target="_blank":
<a href="https://example.com" target="_blank">Opens in a new tab</a>
Images
An image uses <img>. It is an empty element, so it has no end tag.
srcis the address of the image file.altdescribes the image in words. Screen readers read it aloud, and it appears if the image fails to load.
<img
src="https://picsum.photos/id/1018/400/200"
alt="A mountain valley with a river"
width="400"
>
<p>Change the width, or break the src address to see the alt text.</p>Linking an image
Elements nest, so you can put an image inside a link to make it clickable:
<a href="/gallery/">
<img src="photo.jpg" alt="Open the photo gallery">
</a>Practice
Check what you just learned. Fill in the missing code, then answer the quiz.
Exercise 1Solved
Complete the link so it goes to https://example.com.
<a ="https://example.com">Visit Example</a>The attribute name is short for hypertext reference.
Exercise 2Solved
Fill in the two attributes every image needs: the file address and a text description.
<img ="cat.jpg" ="A gray cat asleep on a sofa">One is short for source, the other for alternative text.
Exercise 3Solved
Make this link open in a new tab.
<a href="https://example.com" target="">Example</a>The value starts with an underscore.
Finish the practice to complete this lesson.
Sign in to keep your progress on any device.
