|$ curl https://forge-ai.dev/api/markdown?path=docs/html/buttons
$cat docs/html-button.md
updated Yesterday·15 min read·published

HTML Button

HTMLCSSBeginnerBeginner
Introduction

The <button> element is one of the most fundamental interactive elements in HTML. It represents a clickable control that can trigger actions, submit forms, or reset form data.

Buttons are essential for user interaction. They should always have clear labels, proper styling, and accessible states including hover, focus, and active indicators.

Syntax

The basic syntax of a button element is straightforward:

index.html
HTML
1<button type="button">
2 Click Me
3</button>
Example

Here is a complete example with styling:

1<button class="btn" id="saveBtn">2  Save Changes3</button>
Live Demo

Interactive buttons with different styles:

preview
preview
preview
Code

Complete implementation with all interactive states:

styles.css
CSS
1button {
2 background: #4F46E5;
3 color: white;
4 border: none;
5 padding: 10px 20px;
6 border-radius: 6px;
7 font-size: 14px;
8 cursor: pointer;
9 transition: all 0.2s ease;
10}
11
12button:hover {
13 background: #4338CA;
14 transform: translateY(-1px);
15}
16
17button:focus {
18 outline: 2px solid #4F46E5;
19 outline-offset: 2px;
20}
21
22button:active {
23 background: #3730A3;
24 transform: translateY(0);
25}

info

Buttons should always have a hover state to provide visual feedback. Use transition for smooth animations.
Explanation

The typeattribute defines the button's behavior:

TypeDescriptionDefault
submitSubmits the form dataYes
resetResets all form fields to their initial values
buttonClickable button with no default behavior

warning

Never remove focus outlines without providing an alternative focus indicator. Accessibility is not optional.
Common Mistakes

✗ Incorrect

<div onclick="submitForm()">Submit</div>

Using a div instead of a button breaks accessibility and keyboard navigation.

✓ Correct

<button type="button" onclick="submitForm()">Submit</button>

Always use semantic button elements for interactive controls.

best practice

Use semantic <button> elements instead of <div> or <span> with click handlers. This ensures accessibility, keyboard navigation, and proper form behavior.
Best Practices
Always specify a type attribute (submit, reset, or button)
Provide descriptive labels — never use just 'Click Here'
Include hover, focus, and active states
Use CSS variables for consistent theming
Ensure touch targets are at least 44×44px for mobile
Use disabled styling for inactive buttons
Add aria attributes when button purpose is not clear from text
🔥

pro tip

Use CSS custom properties (variables) for button colors. This makes theming and maintaining consistency across your project significantly easier.
Browser Support
BrowserSupportVersion
ChromeAll
FirefoxAll
SafariAll
EdgeAll
$Blueprint — Engineering Documentation·Section ID: HTML-02·Revision: 1.0