Creating Links
On this card you’ll learn how to make a link that takes you to another page when it’s clicked.
- Add the following code to the body section of
index.html
:
<a href="">Click here</a>
The <a> </a>
tags turn whatever is in between them into a link.
- Try clicking your link to see what happens. It does nothing, right?
That’s because the href
attribute is empty at the moment. It needs to contain the URL (web address) of the page that you want to link to.
-
Go to Wikipedia and find a page about something on your website. I’m going to use the page about bird conservation.
-
Click in the address bar and select all of the text in i5. That’s the complete URL of the page you’re on. Press the
Ctrl (orcmd ) andC keys at the same time to copy it. -
In your trinket, click in between the quotation marks after
href=
and press theCtrl (orcmd ) andV keys at the same time to paste in the URL you just copied. Your code should look something like this now:
<a href="https://en.wikipedia.org/wiki/Bird_conservation">Click here</a>
You just created your first link! Click on it to see if it works now.
Links to other websites
Trinket has trouble with some web addresses. You can try URLs of websites other than Wikipedia if you like, but they may not work in your trinket. However, if you were to download your project and view the files in a web browser, you would see the links working.
- Try putting a picture in between the
<a> </a>
tags instead of the wordsClick here
, like this:
<a href="https://en.wikipedia.org/wiki/Bird_conservation">
<img src="barn-owl.jpg" alt="A barn owl" width="200px" />
</a>
- Click on your picture. Do you see that it was turned into a link?
You can put a link into other elements of your webpage too, such as in a paragraph or even in a list. Here is an example of a sentence with a link in it:
<p>
<a href="https://en.wikipedia.org/wiki/Bird_conservation">Click here</a> to read about bird conservation on Wikipedia.
</p>
Challenge: put a link into a list
- See if you can make a list that contains a link inside one of the list items.