HTML
Buttons and Actions
Create interactive buttons that respond to clicks and perform actions on your webpage
Buttons are the workhorses of web interaction. Every time you click "Buy Now" on Amazon, "Tweet" on Twitter, or "Sign Up" on GitHub, you're clicking a button that triggers an action. These aren't just pretty rectangles — they're the bridge between what users want and what your website does. Think of buttons like the controls on a TV remote. Each one has a specific job. Some submit forms, others open menus, and some just make things happen when clicked. The difference between a boring website and an engaging one often comes down to well-designed, functional buttons.The Button Element
The<button> tag creates clickable buttons. Unlike input elements that we learned about in form validation, button elements can contain any HTML content — text, images, icons, or combinations.
<!-- Download button for Alex's portfolio -->
<button type="button">Download My Resume</button>
<!-- Button with an icon and text -->
<button type="button">
📄 View Projects
</button>
<!-- Multiple action buttons -->
<button type="button">Contact Me</button>
<button type="button">Hire Me</button>What just happened?
The browser created three clickable buttons with default styling. Each button responds to hover and click events automatically. Try this: Click the buttons and notice how they provide visual feedback when pressed.
Button Types and Behavior
Thetype attribute tells the browser what the button should do when clicked. Think of it like setting the mode on a camera — each type has a different purpose.
type="button"
General purpose button that does nothing by default. Perfect for JavaScript actions.
type="submit"
Submits the form it's inside. Like clicking "Send" on an email.
type="reset"
Clears all form fields back to their default values.
No type
Defaults to "submit" if inside a form, "button" if not.
<!-- Contact form with different button types -->
<form>
<h3>Contact Alex</h3>
<label>Your Name:</label>
<input type="text" name="name" value="John Doe">
<br><br>
<label>Message:</label>
<textarea name="message">Hello Alex!</textarea>
<br><br>
<!-- Submit button - sends the form -->
<button type="submit">Send Message</button>
<!-- Reset button - clears all fields -->
<button type="reset">Clear Form</button>
<!-- Regular button - does nothing yet -->
<button type="button">Save Draft</button>
</form>What just happened?
Three different button behaviors: "Send Message" would submit the form, "Clear Form" resets all fields to their defaults, and "Save Draft" does nothing (yet). Try this: Click "Clear Form" to see the fields reset instantly.
Button States and Accessibility
Buttons can be disabled, which prevents users from clicking them. This is useful when a form isn't ready to submit or an action shouldn't be available yet. Disabled buttons appear grayed out and don't respond to clicks. Thedisabled attribute makes buttons unclickable. Think of it like putting tape over a light switch — it's still there, but it won't work until you remove the tape.
<!-- Buttons showing different states -->
<h3>Alex's Portfolio Actions</h3>
<!-- Normal active button -->
<button type="button">Download Resume</button>
<!-- Disabled button -->
<button type="button" disabled>Portfolio Loading...</button>
<!-- Button with descriptive text for screen readers -->
<button type="button" aria-label="Download Alex's resume as PDF file">
📄 Resume
</button>
<!-- Button that explains what will happen -->
<button type="button" title="Opens Alex's LinkedIn profile in new tab">
Connect on LinkedIn
</button>Accessibility Tip: The aria-label attribute provides extra description for screen readers, while title shows a tooltip on hover. Both help users understand what your buttons actually do.
Styling Buttons with CSS
Raw HTML buttons look pretty boring. But with CSS, you can make them match your design perfectly. Most websites completely override the default button appearance to create custom designs. Alex wants professional-looking buttons for their portfolio. Here's how to style them:<!-- Custom styled buttons for Alex's portfolio -->
<style>
.primary-btn {
background: #3b82f6;
color: white;
border: none;
padding: 12px 24px;
border-radius: 8px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
}
.primary-btn:hover {
background: #2563eb;
}
.secondary-btn {
background: transparent;
color: #3b82f6;
border: 2px solid #3b82f6;
padding: 10px 22px;
border-radius: 8px;
font-size: 16px;
cursor: pointer;
}
</style>
<h3>Styled Portfolio Buttons</h3>
<!-- Primary action button -->
<button type="button" class="primary-btn">Hire Me</button>
<!-- Secondary action button -->
<button type="button" class="secondary-btn">View Resume</button>What just happened?
CSS completely transformed the default button appearance. The primary button has a solid blue background that darkens on hover, while the secondary button has a transparent background with a blue border. Try this: Hover over the buttons to see the color transitions.
Common Button Design Patterns
Professional websites use consistent button hierarchies. Primary buttons grab attention for main actions, while secondary buttons handle less important tasks.Primary Buttons
Bold, high-contrast design for main actions
- Submit forms
- Make purchases
- Start processes
Secondary Buttons
Subtle design for supporting actions
- Cancel operations
- View details
- Alternative paths
Buttons vs Links
This trips up many developers. When do you use a button versus a link? The rule is simple: buttons perform actions, links navigate to places.Common Mistake
Using <button onclick="location.href='page.html'"> for navigation. Use <a href="page.html"> instead. Screen readers and keyboards expect links for navigation.
<!-- Correct usage: buttons vs links -->
<h3>Alex's Portfolio Navigation</h3>
<!-- Links - navigate to other pages -->
<a href="#" style="color: #3b82f6; text-decoration: none; margin-right: 20px;">About Me</a>
<a href="#" style="color: #3b82f6; text-decoration: none; margin-right: 20px;">My Projects</a>
<a href="#" style="color: #3b82f6; text-decoration: none;">Contact</a>
<br><br>
<!-- Buttons - perform actions -->
<button type="button">Toggle Dark Mode</button>
<button type="button">Show More Projects</button>
<button type="submit">Send Message</button>What just happened?
Links handle navigation between pages, while buttons trigger actions on the current page. This semantic difference helps screen readers, search engines, and keyboard users understand your interface better. Try this: Notice how links show the typical underline styling while buttons have the standard button appearance.
Quiz
Alex wants to create a contact form button that sends the form data when clicked. Which button type should they use and why?
Alex is confused about when to use buttons versus links. What's the correct rule?
Alex wants to prevent users from clicking a "Submit Application" button until they complete all required fields. What HTML attribute should they use?
Next: Semantic HTML
Learn how to structure your HTML with meaning and purpose using semantic elements that search engines and screen readers understand.