← Back to HTML Course | Chapter 3: Page Structure | Lesson 12 of 12

HTML Buttons

Imagine walking up to an elevator. It has a panel of metal buttons: you press 4 to go to the fourth floor, or press the Alarm button in an emergency. Each button has a specific job, and clicking it triggers an action. HTML buttons are those elevator controls. They define clickable interaction elements on your page. You can use them to submit form data, trigger custom JavaScript actions, or reset input fields. By setting the correct button type and styles, you make interacting with your page easy for all users.

The button Tag

The button tag defines an interactive, clickable button on your webpage. Inside this container element, you can place text, icons, or images to customize how your button looks.

Note: Always specify an explicit type attribute on every button to prevent unexpected browser behaviors.

Warning: The button tag is not self-closing; you must write a closing tag, even if your button contains only an icon.

Example: The button Tag

markup
<button>Click me</button>

Defining Button Types

You can define three distinct button types using the type attribute: submit (which packages and sends form data), reset (which clears all fields), and button (which triggers custom JavaScript actions).

Note: Use type="button" for buttons that trigger custom scripts to prevent the page from reloading.

Warning: If you do not specify a type attribute, browsers will treat the button as a submit button by default inside forms.

Example: Defining Button Types

markup
<button type="submit">Submit</button>
<button type="reset">Reset</button>
<button type="button">Run Script</button>

Disabling Buttons

You can temporarily lock or disable a button by adding the disabled attribute to its opening tag. This grays out the button, prevents user clicks, and blocks any associated script triggers.

Note: Use the disabled attribute to prevent users from clicking a submit button multiple times while their file is uploading.

Warning: Disabled buttons are completely ignored during keyboard navigation, so users cannot focus on them using the Tab key.

Example: Disabling Buttons

markup
<button disabled>Uploading...</button>

Styling Buttons with CSS

Browsers render buttons with boxy, outdated default styles. You can easily customize their borders, background colors, padding, and hover states using standard CSS properties.

Note: Add custom hover and active states to your button styles to make them feel highly responsive on click.

Warning: Never use CSS outline: none to hide browser focus outlines without replacing them with custom focus styles.

Example: Styling Buttons with CSS

markup
<button style="background-color: blue; color: white; padding: 10px; border: none;">
  Styled Button
</button>

Accessible Button Labels

If your button contains only an icon (like a trash can or a pencil symbol) without any visible text, screen readers will find it difficult to interpret. You must include a descriptive aria-label attribute to tell visually impaired users what the button does.

Note: Always add an aria-label to describe the action of any icon-only button.

Warning: Failing to add accessibility labels can make your interactive menus completely unusable for screen-reader users.

Example: Accessible Button Labels

markup
<button aria-label="Delete item">🗑</button>
Common Mistakes
  1. Omitting explicit type attributes on button tags, causing unexpected form submissions.
  2. Attempting to write closing button tags as self-closing tags.
  3. Hiding browser focus outlines using CSS (outline: none) without replacing them with custom focus styles.
Chapter Summary
  • The button tag defines an interactive, clickable button on your webpage.
  • Specify button types using the type attribute: submit for form submissions, reset to clear fields, or button for custom JavaScript actions.
  • Style buttons with CSS for a responsive design, and always include descriptive aria-labels on icon-only buttons for accessibility.
Browser Support

Standard button tags and core attributes are natively supported by all web browsers.

🔒

Chapter Quiz — Complete all 12 topics to unlock

0/12 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.