DEV Community

Cover image for Why <label> and <button> Are Better Than <div> for Forms
Pawar Shivam
Pawar Shivam

Posted on

Why <label> and <button> Are Better Than <div> for Forms

Small HTML Choices That Affect Accessibility

Many developers focus on JavaScript frameworks and UI styling, but forget that HTML semantics already provide powerful built-in functionality.

Two common mistakes are:

  • ignoring the <label> tag in forms
  • using <div> instead of <button> for interactions

These decisions affect accessibility, usability, and maintainability.


1. Why <label> Improves Form Accessibility

Many forms are written like this:

<div>
  Name
  <input type="text">
</div>
Enter fullscreen mode Exit fullscreen mode

This works visually, but it creates problems for screen readers.

The correct version is:

<label for="name">Name</label>
<input id="name" type="text">
Enter fullscreen mode Exit fullscreen mode

Benefits of Using <label>

Using <label> provides several improvements:

  • Screen readers correctly associate the label with the input
  • Clicking the label focuses the input
  • Improves form usability for all users

Example:

<label for="email">Email</label>
<input id="email" type="email">
Enter fullscreen mode Exit fullscreen mode

Now users can click either the text or the input.


2. Why <button> Is Better Than <div> for Click Events

Developers sometimes build buttons like this:

<div class="btn" onclick="submitForm()">
  Submit
</div>
Enter fullscreen mode Exit fullscreen mode

This looks like a button but is not accessible.

Instead, use:

<button type="submit">
  Submit
</button>
Enter fullscreen mode Exit fullscreen mode

Why <button> Is Better

The <button> element automatically provides:

  • keyboard support
  • focus states
  • accessibility semantics
  • proper form submission behavior

Users can press:

Enter
Space
Enter fullscreen mode Exit fullscreen mode

And the button works automatically.

With a <div>, you would have to implement all of this manually.


Real Developer Rule

If something behaves like a button, use:

<button>
Enter fullscreen mode Exit fullscreen mode

If something labels a form input, use:

<label>
Enter fullscreen mode Exit fullscreen mode

HTML already solves many problems if we use the correct elements.


Example of a Clean Accessible Form

<form>
  <label for="username">Username</label>
  <input id="username" type="text">

  <label for="password">Password</label>
  <input id="password" type="password">

  <button type="submit">
    Login
  </button>
</form>
Enter fullscreen mode Exit fullscreen mode

This structure is:

  • accessible
  • semantic
  • easier to maintain

Final Thought

Many frontend developers focus on complex tools like React, but good frontend engineering starts with correct HTML.

Small elements like:

  • <label>
  • <button>

can make a big difference in accessibility and usability.

Sometimes the best solutions are already built into HTML.

Top comments (0)