The anchor element is the mechanism that turns a collection of documents into a web. It is also more configurable than most people realise, and a few of its behaviours catch beginners out in ways that affect both usability and accessibility.
The basic form
An anchor wraps the text that becomes clickable, and the href attribute holds the destination.
<a href="/css/selectors/">Learn about CSS selectors</a> The content between the tags is what the user sees and clicks. It can be more than text: an image, a heading, or in modern HTML an entire block of content such as a card. Wrapping a whole card in an anchor is valid and is how clickable panels are usually built.
href accepts more than page addresses
The destination can take several forms, and each behaves differently.
- A path to another page, either relative to the current document or relative to the site root.
- A full address including the scheme and domain, for external destinations.
- A fragment beginning with a hash, which jumps to an element on the current page.
- A mail address using the mailto scheme, which opens the user's mail application.
- A telephone number using the tel scheme, which offers to dial on a phone.
Two more exist and should be treated with caution. A javascript pseudo address executes code and is a legacy pattern that breaks with content security policies. A data address embeds content directly and is blocked by browsers for top level navigation.
An anchor without href is not a link
This is the behaviour that surprises people. Remove the href and the element stops being a link entirely. It is no longer focusable by keyboard, it is not announced as a link, it does not appear in the list of links a screen reader user can pull up, and it loses the pointer cursor.
<a class="btn">Not a link</a> The specification calls this a placeholder link, intended for cases such as the current page in a navigation menu where a link would be pointless. It is not intended as a hook for a click handler, and using it that way produces something that looks interactive to sighted mouse users and is invisible to everyone else.
Links go places, buttons do things
The choice between an anchor and a button is a genuine decision rather than a stylistic one. An anchor navigates to a destination. A button performs an action on the current page.
The distinction has practical consequences. A link can be opened in a new tab, copied, bookmarked and shared, and it appears in the browser history. A button can do none of those and is not expected to. Beyond that, the keyboard behaviour differs: a link activates on the enter key alone, while a button responds to both enter and space, and users expect exactly that.
Something that submits a form, opens a dialogue or toggles a panel is a button even if you style it to look like text. Something that takes the user elsewhere is a link even if you style it to look like a button.
The empty fragment
An href containing only a hash points at the top of the current page, which is why clicking one scrolls to the top and adds a hash to the address. It appears constantly as a placeholder in templates and tutorials.
Left in production it is a small bug with real consequences, since it changes the address, adds a history entry and moves the user's position for no reason. If a link genuinely has no destination yet, it is better to leave it as plain text until it does.
Attributes worth knowing
The title attribute adds a tooltip, and as covered elsewhere it is unreliable and should never carry essential information. The hreflang attribute declares the language of the destination, which is useful when linking to content in another language. The type attribute hints at the format of the destination, which some browsers use when deciding how to handle a click.
Styling the states
Links have five states worth styling: normal, visited, hover, focus and active. The order matters in CSS because of specificity, and the conventional sequence puts link and visited first, then focus, hover and active.
The one that gets neglected is focus. It is what a keyboard user sees to know where they are on the page, and removing the outline without providing a replacement makes a site effectively unusable without a mouse. If the default ring does not suit your design, replace it with something clearly visible rather than removing it.