HTML Links
In HTML, the <a> (anchor) element is used to create links or hyperlinks.
A hyperlink is a reference to another document, resource, or location on the web.
When a user clicks on a hyperlink, it typically navigates them to the linked destination.
Here's the basic syntax of an HTML link:
- href attribute: Specifies the URL (Uniform Resource Locator) or the destination of the link. It can point to another webpage, a file, an email address, or various other resources.
- Link Text: The text between the opening tag and the closing tag is the clickable text that appears on the webpage.
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
<a href="https://www.w3schools.com/"> target="_blank">Visit CodeLines!You can click on above box to edit the code and run again.
Output
Link to an Email Address
Use mailto:
inside the href
attribute to create a link that opens the user's email program (to let them send a new email):
Example
<a href="mailto:someone@example.com">Send email</a>
You can click on above box to edit the code and run again.Output
Linking with an Image:
To use an image as a link, just put the <img> tag inside the <a> tag:
Button as a Link
To use an HTML button as a link, you have to add some JavaScript code.
JavaScript allows you to specify what happens at certain events, such as a click of a button: