Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to create a button in HTML

Understanding HTML Buttons

HTML, or HyperText Markup Language, is the backbone of most websites you visit every day. One of the most common elements you'll encounter in HTML is a button.

Imagine a button as an elevator button. When you press the button, it sends a signal that, in turn, triggers an action - the elevator moves. Similarly, in HTML, a button often triggers a specific action in a web application.

Creating a Basic Button

Creating a button in HTML is quite straightforward. All you need is the <button> tag. Here's how to create a simplest button:

<button>Click me!</button>

"Click me!" is the text that will be displayed on the button. You can, of course, change the text to anything you want.

Adding Functionality to the Button

A button is not very useful unless it does something when clicked. Let's see how we can make our button more interactive.

Using JavaScript

JavaScript is a programming language that adds interactivity to your website. Just like how a remote control works with your television set, JavaScript can control the behavior of HTML elements on a webpage.

Here is an example of how to use JavaScript to display an alert message when the button is clicked:

<button onclick="alert('You clicked the button!')">Click me!</button>

In the example above, onclick is an event attribute that defines the JavaScript to be run when the button is clicked. alert('You clicked the button!') is a JavaScript function that displays an alert box with a specified message.

Styling the Button

Just as you can decorate your room to suit your taste, you can use CSS (Cascading Style Sheets) to style HTML elements, including buttons. CSS is like the paint and furniture for your room - it adds color, style, and layout to your HTML content.

Here's how to make your button look nicer with CSS:

<button style="background-color: blue; color: white; padding: 10px 20px;">Click me!</button>

In this example, style is an attribute that allows inline CSS. The CSS property background-color changes the background color of the button to blue. The color property changes the text color to white. The padding property adds space around the text inside the button.

Sometimes, you might want a button that, when clicked, directs the user to a different webpage. You can think of it like a door. Clicking on the door (button) takes you to a different room (webpage).

Here's how to create a button that acts like a link:

<button onclick="window.location.href='https://www.example.com'">Go to Example.com</button>

In this example, window.location.href is a JavaScript property that gets or sets the current URL. When the button is clicked, the browser navigates to the URL specified.

Conclusion

That's it! You've just learned how to create a basic button in HTML, add functionality with JavaScript, style it with CSS, and even make a button that serves as a link. Remember, practice is key to mastering this, so go ahead and try creating different types of buttons and experimenting with various styles and functionalities.

In the next article, we'll dive deeper into JavaScript and learn more about adding complex functionalities to our buttons. Keep coding and exploring!