Producing web pages
* HTML CSS DHTML XHTML A to Z of tags        Accessibility          Design

Using the <button> tag to create buttons

The HTML 4.0 standard introduces the new <button> tag. You to use the tag set up plain buttons, buttons with text, graphical buttons and buttons with text and pictures. You can already do most this in a form with the <input> tag but the <button> tag is easier to use and you can put almost anything inside it. Note: you can use it tag anywhere on your page, not just in a form.

Here are some examples of what you can do with the <button> tag:

This HTML…

Displays as…

<button>Home</button>

<button>
<font face="Comic Sans MS">Home</font>
</button>

<button>
<font face="Comic Sans MS" size="4" color="red">Home</font>
</button>

<button>
<img src="home.gif" border="0" alt="Home">
</button>

<button>
<table width="800"border="1">
<tr>
<td><img src="home.gif" alt="Home"></td>
<td><font face="Comic Sans MS">Home</font></td>
</tr>
</table>
</button>

There are a few tags that you can't include within a button: the <button>, <select>, <input> and <a> tags.

If you want the buttons to be anything other than decorative, add a bit of javascript to make some thing happen. For example:

To jump to another page:

<button onClick="window.location='http://www.ann.com'">
Home</button>

To open a new window:

<button onClick="window.open=('hints.htm')">Hints</button>

To close a window:

<button onClick="window.close=()">Close</button>

If you want to use the <button> tag in a form, use the type attribute to tell the browser what to do when you click the button. You can set up a submit, a reset or a plain button which simply presses in and out.

Here are some examples that create submit buttons but you could set up reset buttons in a similar way.

To create a button with text:

This HTML…

Displays as…

<button type="submit">
<center>
Mail <b>Me!</b>
</center>
</button>

To create a graphical button put the <img> tag within the <button> tag:

This HTML…

Displays as…

<button type="submit">
<img src="mail.gif">
</button>

This is the same as using <input type="image" SRC="go.gif">.

You can also create a button with text and graphics:

This HTML…

Displays as…

<button type="submit">
<b>Mail Me!</b>
<img src="redball.gif">
</button>

Note that you can also use the name and value attributes in the same way as for the <input> tag. See Submit and reset buttons for more details.

If you create a button with the type set to "button", you create a button that presses in and out. You can use them to kick off a script or applet.