<button>
<button>
Tag
The <button>
element is used to define a clickable button that can perform various functions. It is commonly used to submit forms, execute JavaScript code, or enhance user interaction on a webpage. Unlike the <input>
element with type="button"
, the <button>
tag allows the inclusion of text, images, and other HTML elements inside it, providing greater flexibility.
Syntax
Below is the basic syntax for creating a button using the <button>
tag:
<button type="button">Button Text</button>
Button Demo
<button name="button">Click me</button>
Definition and Usage
The <button>
tag is primarily used to create a clickable button. It can contain text, images, or a combination of both, making it more versatile than the <input>
button type. Buttons created with the <button>
tag can be customized using CSS to enhance their appearance and functionality.
<button>
, such as <i>
, <b>
, <strong>
, <br>
, and <img>
. This flexibility is not available when using the <input>
element for buttons.<button>
Element Attributes of the
The <button>
tag supports several attributes that define its behavior and appearance:
type
– Specifies the button type. Common values include:"button"
– A standard clickable button that does not submit a form."submit"
– A button that submits a form when clicked."reset"
– A button that resets all form fields to their default values.
name
– Assigns a name to the button, which can be used when sending form data.value
– Defines the value associated with the button, which is sent to the server when the form is submitted.disabled
– Disables the button, preventing user interaction.autofocus
– Automatically focuses on the button when the page loads.
Button with Multiple Elements
Below is an example demonstrating how text and an image can be combined within a <button>
:
<button type="button">
<img src="icon.png" alt="Button Icon" width="20"> Click Me
</button>
type
attribute in the <button>
element to prevent unexpected behavior. If omitted, the default behavior within a <form>
is to submit the form (type="submit"
).Conclusion
The <button>
tag in HTML is a versatile element used to create interactive buttons on webpages. Unlike the <input>
button, it allows the inclusion of text, images, and other elements within the button, offering greater flexibility. It supports various attributes like type
, name
, and disabled
, which control its behavior. Always specifying the type
attribute ensures the correct functionality, especially in forms.
Forms
HTML forms gather user input and send it to a server. They include elements like text fields, checkboxes, and buttons, with attributes for validation and submission. Combined with CSS and JavaScript, forms become interactive and user-friendly.
<datalist>
The HTML <datalist> element offers a set of predefined options for an <input> element, providing suggestions as users type to improve form usability and validation.