HTML buttons are interactive elements that allow users to trigger actions on a web page. They are essential components in HTML forms and can be used independently to create clickable elements.
The simplest way to create a button is using the <button>
element:
<button>Click me</button>
This creates a standard button with the text "Click me" displayed on it.
HTML provides three main types of buttons, each serving a different purpose:
Specify the button type using the type
attribute:
<button type="submit">Submit</button>
<button type="reset">Reset</button>
<button type="button">Click Me</button>
Enhance button functionality with these common attributes:
disabled
: Prevents user interactionname
: Assigns a name to the buttonvalue
: Sets a value for the buttonform
: Associates the button with a formButtons can be styled using CSS to improve their appearance and user experience. Here's a simple example:
<button style="background-color: #4CAF50; color: white; padding: 10px 20px; border: none; cursor: pointer;">
Styled Button
</button>
Buttons can trigger JavaScript events, allowing for dynamic interactions. The most common event is onclick
:
<button onclick="alert('Button clicked!')">Click me</button>
aria-label
attributes when necessaryHTML buttons are versatile elements crucial for creating interactive web pages. By understanding their types, attributes, and best practices, you can enhance user experience and functionality in your web projects.
For more information on related topics, explore HTML Forms and HTML Input Types.