Button HTML tags are used in HTML to enhance the interface and versatility of elements like<b>, <img>, etc. A button when clicked, triggers a function enclosed within. You might have generally seen button HTML being used in forms when you “submit,” login interfaces, etc.
Button HTML can easily be created without any complex knowledge or programming skills. All you need is to be familiar with certain HTML tags and attributes. You only need to use the right tag, i,e. <button></button>. You can modify the appearance of the button as you need.
Buttons can be customised to execute different functionalities. Did you use any buttons on your web page or project? If not, let us learn how to make buttons in HTML.
What is a Button Tag in HTML?
A button HTML tag is used to create clickable buttons on a website or application. These tags are written with open and closed tags.
You can customize the appearance of your button, and add different colors, fonts, and more. Buckle up as we are about to begin this journey in the world of web development.
Button Tag HTML Key Takeaways
- The HTML button tag is used to create clickable buttons to execute different functionalities on a web page.
- These tags support both global and event attributes.
- It is important to declare the type attribute inside the button element tag to let the browser understand the objective of the button.
- Button tag HTML is not a self closing tag as it is not empty.
- HTML button tag is supported on all modern browsers like Google Chrome, Safari, Opera, Edge, etc.
Syntax of Button HTML tag
The <button> tag is always written in pairs with an opening tag <button> and a closing tag </button>. You can also write button element as <button/> tag. Let us check a simple representation of html button tag below.
<button type = “button”> Click Here! </button> |
Also, check the code implementation of the following button inside the HTML document.
<!DOCTYPE html>
<html lang=”en”> <head> <meta charset=”UTF-8″ /> <meta name=”viewport” content=”width=device-width, initial-scale=1.0″ /> <link rel=”stylesheet” href=”style.css” /> </head> <body> <button type =”button”>Click Here!</button> <script src=”script.js”></script> </body> </html> |
Check how simple it was to declare a clickable button in a html document. There are numerous ways we can customize a button according to functionality. It can be enhanced using other languages like CSS or Javascript.
Let us try to add a simple customization in our button element using the “style” attribute in CSS.
<!DOCTYPE html>
<html lang=”en”> <head> <meta charset=”UTF-8″ /> <meta name=”viewport” content=”width=device-width, initial-scale=1.0″ /> <link rel=”stylesheet” href=”style.css” /> </head> <body> <button type =”button”>Click Here!</button><br><br> <button type = “button” style = “background-color:Yellow”>New Button </button> <script src=”script.js”></script> </body> </html> |
Now, see the difference in the button element when we used the style attribute to provide a background color to it. Likewise, we can use different CSS attributes and Javascript to add functionalities to the button.
Also, check HTML Programming: Basics, Types, Uses
How to create buttons using input tags?
We can simply create buttons using HTML input tags by defining its “type” as “button”.
<input type =”button” value = “Click Here!”> |
Here type attribute declares the input element being used as a button. The “value” attribute is used to declare the label of the button tag. We can also create submit buttons, reset buttons using input tags. Check an example below.
<!DOCTYPE html>
<html lang=”en”> <head> <meta charset=”UTF-8″ /> <meta name=”viewport” content=”width=device-width, initial-scale=1.0″ /> <link rel=”stylesheet” href=”style.css” /> </head> <body> <form> <input type = “text” value=”Username”> <input type = “submit”> <input type =”Reset”> <script src=”script.js”></script> </body> </html> |
Also, check How to add CSS with HTML in web development?
Let us do more with our Button HTML tag
Till now, we have created a simple button tag using the <button> attribute, now let us customise our <button> elements using <style> elements in CSS.
<!DOCTYPE html>
<html lang=”en”> <head> <meta charset=”UTF-8″ /> <meta name=”viewport” content=”width=device-width, initial-scale=1.0″ /> <link rel=”stylesheet” href=”style.css” /> <style> button { background-color:yellow; color: red; padding: 15px 20px; text-align: center; display: inline-block; font-size: 20px; } </style> </head> <body> <button type=”button”>Click Here!</button> <script src=”script.js”></script> </body> </html> |
This is how we can use <style> attributes to enhance the appearance of our button tags in html.
To add functionalities to your button tag, you can use Javascript functions or event handlers. You might have heard or seen creating alerts using button elements in html. Well, we can display the same alerts with our button using the “onclick” event handler in Javascript.
Let us check how we can apply alert functionality in our button using Javascript event handlers.
<button type=”button” onclick= “alert(‘Hey! you clicked me’)”>Click Here!</button> |
Always remember it is easier to declare an HTML button, however there are numerous actions and styles that a button can inherit. You can modify animation, hover effects, size, font style, and more in the html button tag.
Recommended Course
- Generative AI Course
- Python DSA Course
- DevOps Course
- UI UX Course
- Digital Marketing Course
- Product Management Course
Attributes in Button HTML Tag
There are various HTML button tag attributes which must be used as per the need. Check some of the frequent HTML attributes being used inside the button tag.
HTML Button Attributes | Description |
type | Specifies the button type (button, submit, reset). |
name | Specifies a name for the button (used with forms). |
value | Defines the value associated with the button (used with forms). |
disabled | Disables the button, preventing user interaction. |
autofocus | Automatically focuses the button when the page loads. |
form | Associate the button with a specific form by ID (used outside form elements). |
formaction | Overrides the form’s action URL for the submit buttons. |
formenctype | Specifies the MIME type used to encode the form data when submitting. |
formmethod | Defines the HTTP method to use when submitting the form (get or post). |
formnovalidate | Tells the form not to validate data on submission. |
formtarget | Specifies where to display the response after submitting the form. |
class | Assign CSS classes for styling the button. |
id | Assign a unique ID for the button element. |
onclick | JavaScript code or function to run when the button is clicked. |
title | Displays additional information when hovering over the button. |
accesskey | Defines a shortcut key to activate the button. |
How to add alt text to a button html?
Alt text is used to display an alternative text when the image does not load successfully on a web page. It is a good web development practice to specify an alt text for every image element we generate.
<button type=”button”>
<img src=”xyz.png” alt=”Button Label”> </button> |
In case the image does not load the alt text will be displayed under the button element.
How to add a link to the button HTML tag?
We can create clickable HTML button tag using ahref element tag in html. When you click on the button you will be redirected to the address enclosed within the button.
<a href=”https://pwskills.com”>
<button type=”button”> PW Skills </button> </a> |
Learn Full Stack Web Development with PW Skills
Become a certified web developer with front end and back end skills with our Full Stack Web Development Course. You will be guided by expert mentors throughout the course. Master advanced tools and frameworks and work on real world projects to strengthen your portfolio.
Attract new opportunities after completing the course with our interview preparation sessions. Our experts will also guide you through the resume preparation processes, and more. Get frequent doubt support and more only at pwskills.com
Button HTML Tag FAQs
Q1. Is button html a self closing tag?
Ans: No, html button tag is not an empty tag or a self closing tag. When we use the button element, we use an opening tag and a closing tag in html.
Q2. Which is better, a button tag or input button tag?
Ans: A button tag consists of a wide range of functionalities like we can add image, animations with text while input button tag is simply a plain text form.
Q3. How to declare a disabled button tag in html?
Ans: You can simply use disabled attribute to declare a disabled button which is non-clickable and execute no function for the time being.
Q4. How do you make buttons appear in html?
Ans: You can put text inside the button tag to make the button appear in html.