How to Code Buttons in HTML

This tutorial walks you through step by step how to code buttons in HTML.

In this tutorial we’re going to introduce adding a button directly via HTML. As one of the most commonly used elements on any website knowing how to create and customise buttons is a great skill to have. Even when using other systems such as WordPress it can be necessary to customise and add HTML elements directly.

To create a button you can use: <button> </button> so for example <button> This is a button </button> within that you can add additional tags such as </i> <strong> etc.

When creating a button it’s important to remember the need to assign a type to it depending on the purpose of the button the available types are:

Button
Reset
Submit

For example when creating a submit button for a form you may use:

<button type=”submit”>Submit</button>

Adding a link to buttons, in most instances you’ll need to do this with Javascript by setting an onclick event for the button.

For example <button onclick=”Javascript Path”> Button </button>

Alternatively if you’d rather use a Href for the purposes of a standard button e.g. a read more button within a page you can simply set a class for that href and style it to look similar to a typical button.

Styling the button:

For example:

<a href=”/this-is-a-link” class=”button-style-1”> This is a link </a>

We’ll cover the styling of the href later in this tutorial.

When styling a button you can also add a class directly to it and then style via CSS for example:

<button class=”button-style-1”> This is a button </button>

To style the class in both of these instances you’ll want to edit the class directly within your main css file.

For example:

.button-style-1 {

Background: green;
Padding: 5%;

}

In the case of styling with a href you may also need to directly style the a tag. 

For example:

.button-style-1 a {

Color: white;

}

Adding additional attributes to the button:

When using a button tag you can add additional attributes to it depending on the purpose of the button these include:

FormID, Formaction, formmethod, formnovalidate, formtarget, form name and form value.

There are additional attributes that can be used such as formenctype and autofocus.

Form ID:

This provides information as to which form the button belongs to. For example form_1.

Formaction:

This is used to identify where the data from the form is sent when using the button in the submit format.

Formmethod:

There are two methods for submitting the form data; these are Get and Post this attribute details which is being used.

Formnovalidate:

This sets the form data to not be validated on submit, for example whether an email address matches the expected format.

Formtarget:

Used to specify the target of the forms output options include:

_blank
_self
_parent
_top

Using _blank for example opens the results of the form in a new page. You can also utilise this when working hrefs.

Name:

Simply allows a name for the button to be identified.

Value:

This sets the initial value of the button for example ‘Enter Message’.

Further Reading:

HTML, CSS, PHP, Javascript and Jquery Code Commenting

This is a quick simple  tutorial on using buttons with HTML. We hope it gives you a good overview of the topic. We’d appreciate a share or any comments if you found this useful, thanks!

Tech Typed
Tech Typed
Articles: 186

Leave a Reply

Your email address will not be published. Required fields are marked *