Use a link when clicking should navigate to a resource or location. Use a button for an action such as opening a dialog, saving a preference, or revealing a hint. The important question is always where the link leads and whether the person reading the page can predict it.
What You Need to Know
Browsers give links and buttons different keyboard and screen-reader behavior. Styling them to look alike does not change their meaning.
Example: See It in Action
Add this example to the appropriate part of a practice HTML page. Read each line before you open the page in your browser.
This is a short HTML fragment. To try it as a webpage, put it inside the body element of a complete HTML document.
<a href="instructions.html">Read the instructions</a>
<button type="button" id="show-hint">Show a hint</button>
<p>The link navigates; the button performs an action.</p>
Let’s Understand the Example
- An a element becomes a working link when it has an href. Its visible text should tell readers where they will go.
- Button is a real keyboard-accessible control. Inside a form, specify type when you do not want the default submit action.
- A p groups a paragraph of related text. Start another paragraph for a new thought.
- Href contains the destination of a link. A value starting with # points to an id on the same page.
- Type changes the kind of input, script, resource or button, depending on the element.
- Id gives one element a unique name within the page, so a link, label, or script can point to it.
The first example link points to instructions.html. Check whether that destination exists relative to the document. A link changes where readers go; it does not perform an action such as saving a form.
What Should You See?
You should see a link named “Read the instructions”. Selecting it leads to instructions.html when that destination exists. Try it with the mouse and the keyboard.
Imagine this link is read out of context. Would its text still explain the destination? Rename unclear links before trying to make them look nicer with CSS.
Try It Yourself
- Decide whether Download lesson or Show hint should be a link or button.
- Save your changes, open or refresh the page, and compare the result with the original example.
- Tell a friend what your change did. If you are not sure, undo it, refresh the page, and compare.
Common Beginner Mistakes
- Making a fake link with href="#" and JavaScript when a real button is needed.
- Avoid dead links, empty href values and clickable-looking text without a real destination.
Key Takeaways
- Use a link when clicking should navigate to a resource or location.
- An a element becomes a working link when it has an href. Its visible text should tell readers where they will go.
- Browsers give links and buttons different keyboard and screen-reader behavior. Styling them to look alike does not change their meaning.