--- title: slug: Web/HTML/Element/input/image tags: - Element - Form Image - Form Image Button - Forms - HTML - HTML Image Button - HTML forms - Image Button - Input - Input Type - Number - Reference translation_of: Web/HTML/Element/input/image ---
{{HTMLElement("input")}} 要素の image
型は、テキストではなく画像の形をとる送信ボタン、すなわち送信ボタンを作成するために使用されます。
{{anch("Value", "値")}} | なし — value 属性は指定しないでください。 |
イベント | なし |
対応している共通属性 | {{htmlattrxref("alt", "input")}}, {{htmlattrxref("src", "input")}}, {{htmlattrxref("width", "input")}}, {{htmlattrxref("height", "input")}}, {{htmlattrxref("formaction", "input")}}, {{htmlattrxref("formenctype", "input")}}, {{htmlattrxref("formmethod", "input")}}, {{htmlattrxref("formnovalidate", "input")}}, {{htmlattrxref("formtarget", "input")}} |
IDL 属性 | なし |
メソッド | なし |
<input type="image">
要素は value
属性を受け付けません。表示する画像へのパスを src
属性で指定します。
すべての {{HTMLElement("input")}} 型で共通する属性に加え、 image
型の入力欄は次の属性にも対応しています。
属性 | 説明 |
---|---|
{{anch("alt")}} |
画像が表示できない場合に表示する代替文字列 |
{{anch("formaction")}} |
データの送信先の URL |
{{anch("formenctype")}} |
フォームデータを送信する際に使用するエンコーディング方法 |
{{anch("formmethod")}} |
フォームを送信する際に使用する HTTP メソッド |
{{anch("formnovalidate")}} |
論理属性で、存在する場合、送信前にフォームを検証しないことを示す |
{{anch("formtarget")}} |
フォームの送信結果を読み込む閲覧コンテキストを示す文字列 |
{{anch("height")}} |
画像を描画する CSS ピクセル単位の高さ |
{{anch("src")}} |
画像の読み込み元の URL |
{{anch("width")}} |
画像を描画する CSS ピクセル単位の幅 |
alt
属性は、画像が表示できない場合のボタンのラベルとして使用される代替文字列を提供します (エラーの場合、{{Glossary("user agent", "ユーザーエージェント")}}が画像を表示できない場合、画像を表示しないように設定されていた場合、ユーザーが読み上げ端末を使用している場合など)。提供する場合は、ボタンのラベルとして適切な空ではない文字列である必要があります。
例えば、「今すぐログイン」をアイコンやテキストが入った画像を表示するグラフィックボタンの場合、 alt
属性も 今すぐログイン
のように設定してください。
重要: alt
属性は技術的には省略可能ですが、コンテンツの利用性を最大化するために常に設定してください。
<input type="image">
の alt
属性は、機能的には {{HTMLElement("img")}} 要素における {{htmlattrdef("alt", "img")}} 属性と同様に動作します。
文字列で、データの送信先の URL を示します。これはこの {{HTMLElement("input")}} が属する {{HTMLElement("form")}} 要素の {{htmlattrxref("action", "form")}} 属性より優先します。
この属性は <input type="submit">
および {{HTMLElement("button")}} 要素でも使用できます。
文字列で、フォームのデータをサーバーに送信する際に使われるエンコーディング方法を識別します。許されている値は3つです。
application/x-www-form-urlencoded
multipart/form-data
=file
(<input type="file">
) が含まれている場合は、このエンコーディング型を使わなければなりません。text/plain
formenctype
属性が指定された場合、所属するフォームの {{htmlattrxref("action", "form")}} 属性を上書きします。
この属性は <input type="submit">
および {{HTMLElement("button")}} 要素でも使用できます。
文字列で、フォームのデータを送信するときに使用する HTTP メソッドを示します。この値は所有者であるフォームの {{htmlattrxref("method", "form")}} を上書きします。許されている値は次の通りです。
get
formaction
or {{htmlattrxref("action", "form")}} attribute, appending a question mark ("?") character, then appending the form's data, encoded as described by formenctype
or the form's {{htmlattrxref("enctype", "form")}} attribute. This URL is then sent to the server using an HTTP {{HTTPMethod("get")}} request. This method works well for simple forms that contain only ASCII characters and have no side effects. This is the default value.post
formaction
or {{htmlattrxref("action", "form")}} attribute using an HTTP {{HTTPMethod("post")}} request. This method supports complex data and file attachments.dialog
この属性は <input type="submit">
および {{HTMLElement("button")}} 要素でも使用できます。
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 {{htmlattrxref("novalidate", "form")}} attribute on the element's owning form.
この属性は <input type="submit">
および {{HTMLElement("button")}} 要素でも使用できます。
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 {{htmlattrxref("target", "form")}} 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:
_self
_blank
_parent
_self
._top
_self
.この属性は <input type="submit">
および {{HTMLElement("button")}} 要素でも使用できます。
A number specifying the height, in CSS pixels, at which to draw the image specified by the src
attribute.
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.
A number indicating the width at which to draw the image, in CSS pixels.
The following attribute was defined by HTML 4 for image
inputs, but was not implemented by all browsers and has since been deprecated:
属性 | 説明 |
---|---|
{{anch("usemap")}} |
The name of an image map ({{HTMLElement("map")}}) element to use with the image; this is obsolete. Use the {{HTMLElement("img")}} element to create image maps instead |
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.
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.
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) }}
<input type="image">
elements — like regular submit buttons — can accept a number of attributes that override the default form behavior:
application/x-www-form-urlencoded
: The default value if the attribute is not specified.text/plain
.If this attribute is specified, it overrides the {{htmlattrxref("enctype","form")}} attribute of the element's form owner.
post
: The data from the form is included in the body of the form and is sent to the server.get
: The data from the form is appended to the form
attribute URI, with a '?' as a separator, and the resulting URI is sent to the server. Use this method when the form has no side-effects and contains only ASCII characters.If specified, this attribute overrides the {{htmlattrxref("method","form")}} attribute of the element's form owner.
self
: Load the response into the same browsing context as the current one. This value is the default if the attribute is not specified._blank
: Load the response into a new unnamed browsing context._parent
: Load the response into the parent browsing context of the current one. If there is no parent, this option behaves the same way as _self
._top
: Load the response into the top-level browsing context (that is, the browsing context that is an ancestor of the current one, and has no parent). If there is no parent, this option behaves the same as _self
.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 {{htmlattrxref("name", "input")}} 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. 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.
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.
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) }}
<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>
And now some simple 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; }
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)}}
<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>
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 at 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.
仕様書 | 状態 | 備考 |
---|---|---|
{{SpecName('HTML WHATWG', 'forms.html#image-button-state-(type=image)', '<input type="image">')}} | {{Spec2('HTML WHATWG')}} | |
{{SpecName('HTML5 W3C', 'forms.html#image-button-state-%28type=image%29', '<input type="image">')}} | {{Spec2('HTML5 W3C')}} |
{{Compat("html.elements.input.input-image")}}
<input>
element's frame: {{cssxref("object-position")}} and {{cssxref("object-fit")}}