Choosing Between <button> and <a> in HTML
Both <button> and <a> are interactive elements, but they serve different purposes. Choosing the correct element ensures proper semantics, accessibility, and user experience.
<a> – Represents a link to another page, resource, or section within the page. Primary purpose: navigation.
<button> – Represents an action or command on the current page, such as submitting a form, opening a modal, or toggling content.
Use <a> when navigating to a new URL or location. Example: <a href="/about">About Us</a>
Use <button> when performing an action on the current page. Example: <button type="submit">Submit</button>
Correct use helps screen readers and other assistive technologies convey the proper role. Misusing these elements can confuse users.
Do not use <a> for actions that do not navigate; use <button> instead.
Do not use <button> for navigation; use <a> with a valid href.
Provide descriptive text or aria-label for buttons, especially icon-only buttons.
If styling a link as a button, ensure it remains accessible with a valid href.
You need to add a 'Delete' control to each row in a table of items. Would you use <button> or <a>, and why?
For a navigation menu where each item goes to a different page, which element should you choose and what attributes are required?
Your team built a component that uses <a> tags for form submissions, and now some users report that the page doesn't work when JavaScript is disabled. Explain why this is happening and how you'd fix it.
During a code review you notice a mix of <button> and <a> elements for actions like 'Add to cart' and 'View details'. How would you decide which to keep, considering accessibility and SEO?
In a large e‑commerce platform you need to implement a reusable UI library. How would you design the API for a component that sometimes acts as a navigation link and sometimes as an action button, while keeping semantics correct?
Your performance team flags that a page with thousands of <a> elements used for JavaScript actions is causing a high CLS score. What are the underlying reasons and how would you refactor the markup?
Your organization is migrating a legacy monolith to a micro‑frontend architecture. The legacy code uses <a> tags for many interactive controls. What migration strategy would you propose to replace them with proper <button> semantics without breaking existing deep‑linking and analytics?
When defining a design system shared across multiple product teams, how do you establish guidelines for when to use <button> vs <a>, and how do you enforce them in CI/CD pipelines?