For a button onclick href comparison, choose the element by its action, not its appearance. Use an anchor with an href when activation takes the user to another URL, document, download, or in-page location. Style that anchor to look like a button. Use a real button when activation performs an in-page action or requires JavaScript to decide what happens.
This distinction preserves browser behavior. Anchors support link interactions such as opening a destination in a new tab, copying a link address, and following a URL without JavaScript. Buttons communicate an action. A button can trigger navigation with JavaScript, but that does not make it a semantic substitute for an anchor.
Button onclick versus href: use an anchor for navigation
An anchor is the correct element when the result is navigation to a known destination. Its href should contain the URL, so browsers, assistive technologies, search engines, and users can identify the destination before activating it.
Use an anchor for paths such as /about, full URLs, document downloads, email links, and fragment links such as #features. If an HTML button onclick link sends the user to a known URL, replace that pattern with an anchor and style the anchor instead.
Do not add an href attribute to a button element. The two elements have different semantic roles, even when they share the same visual design.
Style an anchor with href to look like a button
Apply button-like CSS to the anchor while keeping its link behavior. The anchor remains discoverable as a destination and can still receive normal browser focus and link commands.
HTML: <a class=”button-link” href=”/pricing”>View plans</a>
CSS: .button-link { display: inline-block; padding: .75rem 1rem; border-radius: .35rem; background: #1769aa; color: #fff; text-decoration: none; font-weight: 600; } .button-link:focus-visible { outline: 3px solid #111; outline-offset: 2px; }
Use sufficient color contrast and preserve a clear focus indicator. A class changes the presentation, not the meaning: this remains a link because it takes the user to another location.
Use HTML button navigation with onclick for real actions
Use a button when activation performs an action in the current interface, such as opening a dialog, toggling a menu, applying a filter, or deleting an item. HTML button onclick navigation is appropriate only when the action genuinely needs JavaScript, such as computing a destination after validation or saving state before redirecting.
Example: <button type=”button” onclick=”location.assign(‘/account’)”>Open account</button>
location.assign() navigates the current document to the supplied URL and normally adds that destination to the browser history, allowing the user to return with the Back button. The equivalent property form is location.href = ‘/account’; it also navigates the current page and normally creates a history entry. Neither method opens a new tab.
Set type=”button” when the control sits inside a form and is not meant to submit it. Without that type, a button inside a form defaults to submission. If JavaScript is unavailable and the destination is known in advance, prefer an anchor so navigation still works.
Choose by action, not appearance
- Navigate to a URL, document, download, or page section: use an anchor with href.
- Open, close, toggle, filter, select, or delete within the page: use a button.
- Send form data through the form’s normal process: use <button type=”submit”> or the form’s submit control.
- Navigate only after JavaScript performs a required action: use a button with JavaScript, such as location.assign().
Choose semantics first, then make the selected element match the interface design. A button-shaped anchor is still navigation; a button with onclick is still an action control.









