- HTML Links
- HTML Links — Hyperlinks
- HTML Links — Syntax
- Example
- HTML Links — The target Attribute
- Example
- Absolute URLs vs. Relative URLs
- Example
- Absolute URLs
- Relative URLs
- HTML Links — Use an Image as a Link
- Example
- Link to an Email Address
- Example
- Button as a Link
- Example
- Link Titles
- Nested Links
- HTML Links — HTML Internal Link
- Example
- Linking to parts of other documents
- How to create links to sections on the same page in HTML
- Using #top or #bottom
- Examples
- Results
- Using the id selector
- Use the #id selector
- Create a link to the selector
- Result
- Use the #id selector from another page
- Result
- Complete HTML page code example
- Example Internal Links
- Related information
HTML Links
Links are found in nearly all web pages. Links allow users to click their way from page to page.
HTML Links — Hyperlinks
HTML links are hyperlinks.
You can click on a link and jump to another document.
When you move the mouse over a link, the mouse arrow will turn into a little hand.
Note: A link does not have to be text. A link can be an image or any other HTML element!
HTML Links — Syntax
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:
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:
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 «https://www» part):
Example
Absolute URLs
W3C
Relative URLs
HTML Images
CSS Tutorial
HTML Links — Use an Image as a Link
To use an image as a link, just put the tag inside the tag:
Example
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
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:
Example
Tip: Learn more about JavaScript in our JavaScript Tutorial.
Link Titles
The title attribute specifies extra information about an element. The information is most often shown as a tooltip text when the mouse moves over the element.
Nested Links
The other day I posted an image, quite literally as a thought exercise, about how you might accomplish “nested” links. That is, a big container that is linked to one URL that contains a smaller container or text link inside of it that goes to another URL. That’s harder than it might seem at first glance. The main reason being that…
Eric Meyer once called for more flexible linking, but even that doesn’t quite handle a situation where one link is nested inside another. Here’s what happens with that HTML, by the way: My first inclination would be to simply not nest the links in the markup, but make them appear nested visually. Some folks replied to the tweet, including Nathan Smith, who shared that same thought: have a relatively positioned parent element and absolutely position both links. The larger one could fill up the whole area and the smaller one could sit on top of it. See the Pen “Nested” links by Nathan Smith (@nathansmith) on CodePen. It’s finicky, as you’ll need magic numbers to some degree to handle the spacing and variable content. My second inclination would be to deal with it in JavaScript.
I have literally no idea how kosher that is from an accessibility perspective. It looks gross to me so I’m just going to assume it’s bad news. Speaking of accessibility, Heydon Pickering has a whole article about card components which is a popular design pattern where this situation often comes up. His solution is to have a relatively positioned parent element, then a normally-placed and functional main link. That first link has an absolutely positioned pseudo-element on it covering the entire card. Any sub-links are relatively positioned and come after the initial link, so they’d sit on top of the first link by way of z-index . And speaking of stacking pseudos, that’s the approach Sean Curtis uses here: See the Pen Pretend nested links by Sean Curtis (@seancurtis) on CodePen. Other solutions in the “crafty” territory might be:
- Using a element where the action attribute acts as a link.
- Using an element to wrap the inner link.
I had the same requirement a couple of years ago when I was building the front-end foundation for Smashing Magazine. So I thought I’d write my response to Chris’s thread out in the form of a blog post.
Sara has written about this with much more detail and care than I have here, so definitely check that out. It looks like both she and Heydon have landed on nearly the same solution, with the pseudo-element cover that contains sub-links poking above it as needed.
Have you ever done it another way? Plenty of UX and a11y to think abbout!
HTML Links — HTML Internal Link
HTML internal link is linked within the same web page. This link can be an absolute path or relative path.
HTML internal link name is followed by the hash sign(#). You have to assign an id to refer section of your page, which is referred to as an internal link to the same page.
When you click on an internal anchor link, you will scroll automatically to the referred section and display it on your browser.
To understand internal link see the below examples.
- Lession.1 link can be referred as Introduction of Lession.1 automatically.
- Lession.2 link can be referred as Introduction of Lession.2
automatically.
Notes: You can not use name attribute instead of id attribute. Because name attribute not supported in HTML5. Use the id attribute instead of.
Example
Lession.1
Lession.2
Lession.3
Lession.4
This is sub topic.1 This is sub topic.2
This is sub topic.3
This is sub topic.4
This is sub topic.1
This is sub topic.2
This is sub topic.3
This is sub topic.4
This is sub topic.1
This is sub topic.2
This is sub topic.3
This is sub topic.4
This is sub topic.1
This is sub topic.2
This is sub topic.3
This is sub topic.4
Linking to parts of other documents
You can set target to specific sections of the other pages by adding the #id at the end of the href. After adding hash mark is known as a «fragment identifier». This helps a lot for example, you can link to the third section of this tutorial from anywhere else, you have to write
How to create links to sections on the same page in HTML
Hyperlinks are utilized by a web browser to move from one page to another. However, you can also move to a different area on the same page. The following sections show you how to link to the top, bottom, or a specific section on a web page. Choose a method from the following list, or explore both options.
Today, all browsers still recognize «#» as a viable way of moving to the top or bottom of a page. However, this method is deprecated and may not work in future versions of HTML (hypertext markup language). We suggest using the id method instead as it is more current and versatile.
Using #top or #bottom
The following examples use #top and #bottom with the tag and href attribute to link to that section of the page. This method is similar to using «id,» but you don’t have to pick a specific element. Click «Top» or «Bottom» in the Results section to see what happens.
Examples
Results
Using the id selector
In CSS (cascading style sheets), «id» is a selector that is used to designate an area that a link should point to, similar to anchor in HTML. The nice thing about using id is you can create a link to any element on the page, rather than only the top or bottom. In the following sections, we show you how to apply id to an HTML tag, and then how to link to it. The following two examples link to the opening paragraph at the top of this page.
Use the #id selector
id="opening">Hyperlinks are utilized by a web browser to move from one page to another.
Create a link to the selector
Result
Use the #id selector from another page
You can also jump to a specific part of another web page by adding #selector to the page’s URL (uniform resource locator).
Result
Complete HTML page code example
The following code shows how both the #top and #bottom anchors and the id selector can all be used on the same page.
Example Internal Links
href="#bottom">Bottom
id="opening">Here is an introductory paragraph with the selector 'opening' attached to it.
A lot of text that causes scrolling goes here.
href="#opening">Jump to opening
href="#top">Top
Related information
- How to create an HTML link on a web page.
- How to create a link that opens a new web page window or tab.
- See our hyperlink definition for further information and related links to this term.
- HTML and web design help and support.