The <button> tag in HTML is used to create clickable buttons on a webpage. These buttons can trigger various actions, such as submitting a form, running JavaScript functions, or simply interacting with users.
- Versatile Content: The <button> tag can contain text, images, or other HTML elements.
- Form Integration: Can be used inside forms to submit or reset data.
- Event Handling: Supports event attributes like onclick, onmouseover, and onfocus for interactive actions.
- Customizable Styles: Easily styled with CSS, including properties like border-radius, box-shadow, and animations.
Syntax
<button>Click Me!</button>
- The opening <button> tag marks the start of the button element.
- The closing </button> tag indicates the end of the button element.
- The text Click Me! will be displayed on the button.
Now let's understand this with the help of example
HTML
<html>
<head>
<title>Centered Button Example</title>
<style>
body,
html {
height: 100%;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
font-family: Arial, sans-serif;
}
button {
padding: 15px 30px;
font-size: 16px;
background-color: blue;
color: white;
border: none;
border-radius: 8px;
cursor: pointer;
}
button:hover {
background-color: darkblue;
}
</style>
</head>
<body>
<button>Click Me!</button>
</body>
</html>
Output
HTML button TagIn this example
- The HTML creates a simple button with the text "Click Me!".
- The button changes its background color when hovered over.
For more details follow this article => How to add Button Tag in HTML
Attributes
The various attributes that can be used with the "button" tag are listed below:
Attributes | Descriptions |
---|
autofocus | Specifies whether the button should automatically get focus or not when the page loads |
disabled | Indicate whether the element is disabled or not. If this attribute is set, the element is disabled. |
form | Create a form for user input. There are many elements that> are used within the >form tag. |
formaction | Specifies where to send the data of the form. |
formnovalidate | Specifies that the Input Element should not be validated when submitting the form. |
formenctype | Specifies that the form data should be encoded when submitted to the server. |
formmethod | Specifies the HTTP method used to send data while submitting the form. |
formtarget | Specifies the name or a keyword that indicates where to display the response after submitting the form. |
type | Specifies the type of button for button elements. It is also used in <input> element to Specifies the type of input to display. |
value | Specifies the value of the element with which it is used. It has different meanings for different HTML elements. |
Note=> It is important to specify the type attribute for a button element to inform the browser of the button's function.
A reset button resets all the form fields to their default values. It's created by setting the type attribute to reset inside the <button> tag.
HTML
<html>
<head>
<title>Centered Reset Button Example</title>
<style>
/* Center the form using Flexbox */
body,
html {
height: 100%;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
font-family: Arial, sans-serif;
}
form {
text-align: center;
padding: 20px;
border: 1px solid #ccc;
border-radius: 10px;
}
button {
padding: 10px 20px;
background-color: blue;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: darkblue;
}
</style>
</head>
<body>
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username" placeholder="Enter your username"><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" placeholder="Enter your email"><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" placeholder="Enter your password"><br><br>
<!-- Centered Reset Button -->
<button type="reset">Reset</button>
</form>
</body>
</html>
Output
In this example
- The HTML code creates a form with three fields: username, email, and password.
- A Reset button is added, which resets all the form fields to their default values when clicked.
An HTML Animated Button can be enhanced with various animations to make it visually engaging and interactive.
HTML
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Animated Button Example</title>
<style>
button {
padding: 15px 30px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 8px;
font-size: 16px;
cursor: pointer;
transition: all 0.3s ease;
}
button:hover {
background-color: #45a049;
transform: scale(1.1);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
button:focus {
outline: none;
}
</style>
</head>
<body>
<button>Hover Over Me!</button>
</body>
</html>
Output
In this example
- The button changes color, scales up, and adds a shadow when hovered.
- It has smooth transitions and no outline when focused.
HTML Button Groups are used to group multiple buttons together, typically to create related actions or options. Button groups help maintain a clean and organized layout, especially when we have multiple buttons performing related tasks.
HTML
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Horizontal Button Group</title>
<style>
.button-group {
display: flex;
justify-content: center;
gap: 10px;
}
.button-group button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
border-radius: 8px;
border: 1px solid #ccc;
}
.button-group button:hover {
background-color: #3498db;
color: white;
}
</style>
</head>
<body>
<div class="button-group">
<button>Option 1</button>
<button>Option 2</button>
<button>Option 3</button>
</div>
</body>
</html>
Output
In HTML, the disabled attribute is used to make a button non-interactive, meaning users cannot click or interact with it. When a button is disabled, it visually appears inactive, and its functionality is prevented.
HTML
<html>
<head>
<title>Disabled Buttons Example</title>
<style>
button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
border-radius: 8px;
border: 1px solid #ccc;
}
button:disabled {
background-color: #e0e0e0;
color: #a0a0a0;
cursor: not-allowed;
}
</style>
</head>
<body>
<button>Active Button</button>
<button disabled>Disabled Button</button>
<button disabled>Another Disabled Button</button>
</body>
</html>
Output
HTML button TagIn this example
- Disabled Buttons: The disabled attribute makes buttons non-interactive (unclickable).
- Visual Effect: Disabled buttons appear gray and use the cursor: not-allowed style.
- Usage: Prevents users from clicking the button when certain actions or conditions are not met.
The <button> tag supports various event attributes that allow us to trigger actions in response to user interactions. Some of the commonly used event attributes are:
1. onclick: This event is triggered when the button is clicked.
<button onclick="alert('Button Clicked!')">Click Me!</button>
2. onmouseover: This event is triggered when the user hovers over the button.
<button onmouseover="this.style.backgroundColor = 'yellow'">Hover Over Me!</button>
3. onmouseout: This event is triggered when the mouse leaves the button.
<button onmouseout="this.style.backgroundColor = ''">Mouse Out Button</button>
4. onfocus: This event is triggered when the button gains focus (e.g., clicked or tabbed into).
<button onfocus="this.style.backgroundColor = 'lightblue'">Focus Me!</button>
5. onblur: This event is triggered when the button loses focus.
<button onblur="this.style.backgroundColor = ''">Blur Me!</button>
Now you have clear understanding of HTML <button> so you can implement this knowledge to create some interesting components using CSS and JavaScript–
Using CSS
Using JavaScript
Note => The HTML <button> tag supports the Global Attribute and Event Attribute in HTML.
Conclusion
We’ve covered the essential aspects of the <button> tag, from basic usage to more advanced features like event handling, styling, and animation. We've also explored various button types, including reset, submit, and disabled buttons, as well as how to add images, use the border-radius property, and create animated or shadow effects.
Similar Reads
HTML DOCTYPE Declaration HTML DOCTYPE (Document Type Declaration) is an instruction that appears at the beginning of an HTML document, before the <html> tag.Its primary role is to tell the web browser which version of HTML the page is written in, ensuring that the browser renders the content correctly. It is not an HT
4 min read
HTML abbr Tag The <abbr> tag in HTML is used to represent abbreviations and provides additional information about them through the title attribute, which displays a tooltip when hovered over. It helps improve accessibility and SEO by offering context for the abbreviated text.It makes text clearer by explain
3 min read
HTML acronym Tag The HTML <acronym> tag was used to define an acronym, providing a way to identify and explain abbreviated terms in web content. However, it's deprecated in favor of <abbr>, which serves the same purpose but is more semantically correct.Syntax:Â <acronym title=""> Short Form </acr
2 min read
HTML < address> Tag The <address> tag in HTML is used to define contact information for the author or owner of a document or an article. It is typically used for information such as an address, email, or phone number.The <address> element is a block-level element by default.The content inside <address
3 min read
HTML a Tag The <a> tag defines a hyperlink, which is used to link from one page to another. The most important attribute of the <a> element is the href attribute, which indicates the link's destination. This attribute determines where the user is directed upon clicking the link.HTML<a href="http
2 min read
HTML applet Tag The applet tag in HTML was used to embed Java applets into any HTML document. The <applet> tag was deprecated in HTML 4.01, and its support has been completely discontinued starting from HTML 5. Alternatives available in HTML 5 are the <embed> and the <object> tags. Some browsers s
2 min read
HTML area Tag This <area> tag is used in an HTML document to map a portion of an image to make it clickable by the end user. This specifies the location and size of the active region on an image, which can be clicked. Clicking on areas with href attributes directs to a specified URL or action.html<!DOCTY
3 min read
HTML article Tag The HTML <article> tag defines a self-contained, independent piece of content like a blog post, news article, or comment. It is designed for content that can be independently distributed, shared, or reused, providing semantic meaning to the content.This tag is introduced in HTML5.HTML<!DOCT
3 min read
HTML aside Tag The <aside> tag is used to describe the main object of the web page more shortly like a highlighter. It identifies the content that is related to the primary content of the web page but does not constitute the main intent of the primary page. The <aside> tag contains mainly author inform
2 min read
HTML5 < audio> Tag The <audio> tag in HTML5 is used to embed audio content on a webpage. It allows you to play audio files like MP3, OGG, or WAV directly in the browser. The <audio> element provides attributes for controlling playback, such as play, pause, and volume.Using the <source> element enable
3 min read