docs.rodeo

MDN Web Docs mirror

{{HTMLSidebar}} 

{{HTMLElement("input")}}  elements of type image are used to create graphical submit buttons, i.e. submit buttons that take the form of an image rather than text.

{{EmbedInteractiveExample("pages/tabbed/input-image.html", "tabbed-standard")}} 

Value

<input type="image"> elements do not accept value attributes. The path to the image to be displayed is specified in the src attribute.

Additional attributes

In addition to the attributes shared by all {{HTMLElement("input")}}  elements, image button inputs support the following attributes.

alt

The alt attribute provides an alternate string to use as the button’s label if the image cannot be shown (due to error, a {{Glossary("user agent")}}  that cannot or is configured not to show images, or if the user is using a screen reading device). If provided, it must be a non-empty string appropriate as a label for the button.

For example, if you have a graphical button that shows an image with an icon and/or image text “Login Now”, you should also set the alt attribute to something like Login Now.

[!NOTE] While the alt attribute is technically optional, you should always include one to maximize the usability of your content.

Functionally, the alt attribute of the <input type="image"> element works just like the alt attribute on {{HTMLElement("img")}}  elements.

formaction

A string indicating the URL to which to submit the data. This takes precedence over the action attribute on the {{HTMLElement("form")}}  element that owns the {{HTMLElement("input")}} .

This attribute is also available on <input type="submit"> and {{HTMLElement("button")}}  elements.

formenctype

A string that identifies the encoding method to use when submitting the form data to the server. There are three permitted values:

If specified, the value of the formenctype attribute overrides the owning form’s action attribute.

This attribute is also available on <input type="submit"> and {{HTMLElement("button")}}  elements.

formmethod

A string indicating the HTTP method to use when submitting the form’s data; this value overrides any method attribute given on the owning form. Permitted values are:

This attribute is also available on <input type="submit"> and {{HTMLElement("button")}}  elements.

formnovalidate

A Boolean attribute which, if present, specifies that the form should not be validated before submission to the server. This overrides the value of the novalidate attribute on the element’s owning form.

This attribute is also available on <input type="submit"> and {{HTMLElement("button")}}  elements.

formtarget

A string which specifies a name or keyword that indicates where to display the response received after submitting the form. The string must be the name of a browsing context (that is, a tab, window, or {{HTMLElement("iframe")}} . A value specified here overrides any target given by the target attribute on the {{HTMLElement("form")}}  that owns this input.

In addition to the actual names of tabs, windows, or inline frames, there are a few special keywords that can be used:

This attribute is also available on <input type="submit"> and {{HTMLElement("button")}}  elements.

height

A number specifying the height, in CSS pixels, at which to draw the image specified by the src attribute.

src

A string specifying the URL of the image file to display to represent the graphical submit button. When the user interacts with the image, the input is handled like any other button input.

width

A number indicating the width at which to draw the image, in CSS pixels.

Obsolete attributes

The following attribute was defined by HTML 4 for image inputs, but was not implemented by all browsers and has since been deprecated.

usemap

If usemap is specified, it must be the name of an image map element, {{HTMLElement("map")}} , that defines an image map to use with the image. This usage is obsolete; you should switch to using the {{HTMLElement("img")}}  element when you want to use image maps.

Using image inputs

The <input type="image"> element is a replaced element (an element whose content isn’t generated or directly managed by the CSS layer), behaving in much the same way as a regular {{htmlelement("img")}}  element, but with the capabilities of a submit button.

Essential image input features

Let’s look at a basic example that includes all the essential features you’d need to use (These work exactly the same as they do on the <img> element.):

<input
  id="image"
  type="image"
  width="100"
  height="30"
  alt="Login"
  src="https://raw.githubusercontent.com/mdn/learning-area/master/html/forms/image-type-example/login.png" />

{{ EmbedLiveSample('Essential_image_input_features', 600, 50) }} 

Overriding default form behaviors

<input type="image"> elements — like regular submit buttons — can accept a number of attributes that override the default form behavior:

Using the x and y data points

When you submit a form using a button created with <input type="image">, two extra data points are submitted to the server automatically by the browser — x and y. You can see this in action in our X Y coordinates example.

When you click on the image to submit the form, you’ll see the data appended to the URL as parameters, for example ?x=52&y=55. If the image input has a name attribute, then keep in mind that the specified name is prefixed on every attribute, so if the name is position, then the returned coordinates would be formatted in the URL as ?position.x=52&position.y=55. This, of course, applies to all other attributes as well.

These are the X and Y coordinates of the image that the mouse clicked on to submit the form, where (0,0) is the top-left of the image and the default in case submission happens without a click on the image. These can be used when the position the image was clicked on is significant, for example you might have a map that when clicked, sends the coordinates that were clicked to the server. The server-side code then works out what location was clicked on, and returns information about places nearby.

In our above example, we could write server-side code that works out what color was clicked on by the coordinates submitted, and keeps a tally of the favorite colors people voted for.

Adjusting the image’s position and scaling algorithm

You can use the {{cssxref("object-position")}}  property to adjust the positioning of the image within the <input> element’s frame, and the {{cssxref("object-fit")}}  property to control how the image’s size is adjusted to fit within the frame. This allows you to specify a frame for the image using the width and height attributes to set aside space in the layout, then adjust where within that space the image is located and how (or if) it is scaled to occupy that space.

Examples

A login form

The following example shows the same button as before, but included in the context of a typical login form.

{{ EmbedLiveSample('A_login_form', 600, 170) }} 

HTML

<form>
  <p>Login to your account</p>
  <div>
    <label for="userId">User ID</label>
    <input type="text" id="userId" name="userId" />
  </div>
  <div>
    <label for="pwd">Password</label>
    <input type="password" id="pwd" name="pwd" />
  </div>
  <div>
    <input
      id="image"
      type="image"
      src="https://raw.githubusercontent.com/mdn/learning-area/master/html/forms/image-type-example/login.png"
      alt="Login"
      width="100" />
  </div>
</form>

CSS

And now some CSS to make the basic elements sit more neatly:

div {
  margin-bottom: 10px;
}

label {
  display: inline-block;
  width: 70px;
  text-align: right;
  padding-right: 10px;
}

Adjusting the image position and scaling

In this example, we adapt the previous example to set aside more space for the image and then adjust the actual image’s size and positioning using {{cssxref("object-fit")}}  and {{cssxref("object-position")}} .

{{EmbedLiveSample("Adjusting_the_image_position_and_scaling", 600, 300)}} 

HTML

<form>
  <p>Login to your account</p>
  <div>
    <label for="userId">User ID</label>
    <input type="text" id="userId" name="userId" />
  </div>
  <div>
    <label for="pwd">Password</label>
    <input type="password" id="pwd" name="pwd" />
  </div>
  <div>
    <input
      id="image"
      type="image"
      src="https://raw.githubusercontent.com/mdn/learning-area/master/html/forms/image-type-example/login.png"
      alt="Login"
      width="200"
      height="100" />
  </div>
</form>

CSS

div {
  margin-bottom: 10px;
}

label {
  display: inline-block;
  width: 70px;
  text-align: right;
  padding-right: 10px;
}

#image {
  object-position: right top;
  object-fit: contain;
  background-color: #ddd;
}

Here, object-position is configured to draw the image in the top-right corner of the element, while object-fit is set to contain, which indicates that the image should be drawn at the largest size that will fit within the element’s box without altering its aspect ratio. Note the visible grey background of the element still visible in the area not covered by the image.

Technical summary

Value None — the value attribute should not be specified.
Events None.
Supported common attributes alt, src, width, height, formaction, formenctype, formmethod, formnovalidate, formtarget
IDL attributes None.
DOM interface

`{{domxref("HTMLInputElement")}}` 

Methods None.
Implicit ARIA Role button

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN