The most important attribute of the element is the href attribute, which indicates the link's destination.
The link text is the part that will be visible to the reader.
Clicking on the link text, will send the reader to the specified URL address.
Example
This example shows how to create a link to W3Schools.com:
Visit W3Schools.com!
Try it Yourself »By default, links will appear as follows in all browsers:
- An unvisited link is underlined and blue
- A visited link is underlined and purple
- An active link is underlined and red
Tip: Links can of course be styled with CSS, to get another look!
HTML Links - The target Attribute
By default, the linked page will be displayed in the current browser window. To change this, you must specify another target for the link.
The target attribute specifies where to open the linked document.
The target attribute can have one of the following values:
- _self - Default. Opens the document in the same window/tab as it was clicked
- _blank - Opens the document in a new window or tab
- _parent - Opens the document in the parent frame
- _top - Opens the document in the full body of the window
Example
Use target="_blank" to open the linked document in a new browser window or tab:
Visit W3Schools!
Try it Yourself »Absolute URLs vs. Relative URLs
Both examples above are using an absolute URL (a full web address) in the href attribute.
A local link (a link to a page within the same website) is specified with a relative URL (without the "//www" part):
Example
Absolute URLs
W3C
Relative URLs
HTML Images
CSS Tutorial
Try it Yourself »HTML Links - Use an Image as a Link
To use an image as a link, just put the 0 tag inside the tag:
Example
To point to a specific part of a web page, you add a pound sign followed by the id name at the end of the url ie. href="/#assign-id" or href="devpractical.com/same-page-internal-link/#add-link"
If you click on the navigation items at the top, you will be able to jump to specific parts of the web page.
Create a link to an element on a different web page.
For you to add a link to a different page, you will need the url of that page and the id value of that section you want to link to. You will then use the full url of the web page and then add the id value at the end of the url. Example:
I want to link to the books section a different page on this website. The url is //devpractical.com/resources/ and the heading is books. I can create a link as follows:
Code:
Result: HTML and CSS books
Create a link to a section of another website.
If you are linking to a different website, you will have to find out if the website has assigned ids to the section you want to link to. You can do this by:
- Inspect Q element.
- Use the select tool.
- Check the html code for id value.
If the element does not have an id value, you cannot link to that section. If it does then you can just add the pound sign and id to the url of the website and you are good to go.
Code:
Which insects do people eat
Result: Which insects do people eat
Create a link that jumps to the top and bottom of a web page.
Link to top of your website
- Choose the top most part of your website. Which is the logo.
- Get it id value.
- For me its the website’s logo with the id “logo”.
So the link to go to the top will become:
Code: Top
Result: Top
Link to bottom of your website
- Choose the bottom most part of your website.
- Get it id value.
- For me its the footer with the id “footer-style”
- So the link to go to the bottom will become:
Code: Go to bottom
Result: Go to bottom
Hi there! I am Avic Ndugu.
I have published 100+ blog posts on HTML, CSS, Javascript, React and other related topics. When I am not writing, I enjoy reading, hiking and listening to podcasts.