191

I'm trying to display a png image on a <button> element in HTML. The button is the same size as the image, and the image is shown but for some reason not in the center - so it's impossible to see it all. In other words it seems like the top right corner of the image is located at the center of the button and not at the top right corner of the button.

This is the HTML code:

<button id="close" class="closing" onClick="javascript:close_clip()"><img src="icons/close.png" /></button>

Update:
What actually happens, I think, is a margin problem. I get a two pixel margin, so the background image is going out of the button. The button and the image are the same size, which is only 20px, so it's very noticable... I tried margin:0, padding:0, but it didn't help...

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Amit Hagin
  • 3,136
  • 6
  • 26
  • 36
  • For me, it works perfectly... which is the context in you're experiencing this? Possible posting a few more lines of code? –  Dec 30 '11 at 20:07
  • 3
    Can you reproduce this on a live site, such as [JS Fiddle](http://jsfiddle.net/) for us to *see* what's going on? – David Thomas Dec 30 '11 at 20:07

12 Answers12

266

You could use input type image.

<input type="image" src="http://example.com/path/to/image.png" />

It works as a button and can have the event handlers attached to it.

Alternatively, you can use css to style your button with a background image, and set the borders, margins and the like appropriately.

<button style="background: url(myimage.png)" ... />
laurent
  • 88,262
  • 77
  • 290
  • 428
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
  • 3
    ...and needs no JavaScript to submit the form. – ComFreek Dec 30 '11 at 20:10
  • 9
    -1: "*Should*" is too strong a term, since this isn't really what `` is designed for. Why wouldn't you suggest CSS? – Wesley Murch Dec 30 '11 at 20:11
  • I preffer to use a button. if I find no answer I'll do it. thanks – Amit Hagin Jan 01 '12 at 20:52
  • Then style it, as noted in this and other answers. – Andrew Barber Jan 01 '12 at 22:25
  • 7
    Don't forget that both the starting end ending tags are required for the button element, so technically is not valid, it should be . – Tamas Czinege Aug 21 '15 at 16:47
  • These both load the image dynamically at runtime, right? The site goes ahead and gets off the ground in the meantime, doesn't it? – Panzercrisis Aug 12 '16 at 15:30
  • 2
    If you use the CSS-approach, one gotcha is that you probably also want to specify `background-size: cover` in order to make the image cover the button. I was scratching my head wondering why my image was not showing up, until I realized only the transparent top left corner of it was actually inside the button... – Magnus Sep 23 '21 at 09:17
90

If the image is a piece of semantic data (like a profile picture, for example), then use an <img> element inside your <button> and use CSS to resize the <img>. If the image is just a way to make a button visually pleasing, use CSS background-image to style the <button> (and don't use an <img>).

Demo: http://jsfiddle.net/ThinkingStiff/V5Xqr/

HTML:

<button id="close-image"><img src="http://thinkingstiff.com/images/matt.jpg"></button>
<button id="close-CSS"></button>

CSS:

button {
    display: inline-block;
    height: 134px;
    padding: 0;
    margin: 0;
    vertical-align: top;
    width: 104px;
}

#close-image img {
    display: block;
    height: 130px;  
    width: 100px;
}

#close-CSS {
    background-image: url( 'http://thinkingstiff.com/images/matt.jpg' );
    background-size: 100px 130px;
    height: 134px;  
    width: 104px;
}

Output:

enter image description here

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
  • @ThinkingStiff `` element? I think you mean `` in your text ;) – ComFreek Dec 30 '11 at 21:00
  • thanks for the good answer!! but the problem remains... I updated the question a little - maybe it can help you understand it bette. – Amit Hagin Jan 01 '12 at 20:52
  • @AmitHagin I saw your update. Remember a button has borders, which are included in the calculation of its height. So to fit a `20px` tall image, the button height would have to be `24px`. – ThinkingStiff Jan 02 '12 at 00:25
  • @AmitHagin Also, to prevent whitespace issues, change your `` to `display: block;`. I updated the demo link to show this. – ThinkingStiff Jan 02 '12 at 00:32
  • Are there any concerns about accessiblity/alt text with this method? I am really rusty, but is hiding the text by offsetting it by a few thousand pixels still the accepted workaround? – Rob Drimmie Jan 06 '12 at 03:26
  • @RobDrimmie Accessibility can be handled with `alt` or `title`. For SEO, there are all kinds of workarounds, including offsetting text, but I'm not sure which is the currently preferred method. – ThinkingStiff Jan 06 '12 at 03:43
21

The simplest way to put an image into a button:

<button onclick="myFunction()"><img src="your image path here.png"></button>

This will automatically resize the button to the size of the image.

Eli Davies
  • 211
  • 2
  • 2
12

try this

   <input type="button" style="background-image:url('your_url')"/>
Chris P
  • 2,059
  • 4
  • 34
  • 68
4

Why don't you use an image with an onclick attribute?

For example:

<script>
function myfunction() {
}
</script>
<img src='Myimg.jpg' onclick='myfunction()'>
SirFireball
  • 91
  • 1
  • 6
3

Add new folder with name of Images in your project. Put some images into Images folder. Then it will work fine.

<input type="image" src="~/Images/Desert.jpg" alt="Submit" width="48" height="48">
krlzlx
  • 5,752
  • 14
  • 47
  • 55
Parameswaran
  • 314
  • 3
  • 12
2

The topic is 'Embed image in a button element', and the question using plain HTML. I do this using the span tag in the same way that glyphicons are used in bootstrap. My image is 16 x 16px and can be any format.

Here's the plain HTML that answers the question:

<button type="button"><span><img src="images/xxx.png" /></span>&nbsp;Click Me</button>
Ray E
  • 134
  • 1
  • 9
1

Try like this format and use "width" attribute to manage the image size, it is simple. JavaScript can be implemented in <button> element too.

<button><img src=""></button>
TylerH
  • 20,799
  • 66
  • 75
  • 101
Nay Aung Lin
  • 85
  • 1
  • 7
  • @LoganCundiff I'm confused by your comment. An img element is not a focusable or interactive element so how is this not ADA compliant? – JD Tripp Jan 04 '23 at 21:35
  • @JDTripp Apologies, you are correct. You can make this accessible to screen readers by adding an alt attribute. – Logan Cundiff Jan 05 '23 at 16:06
1

General Answer:

<button style="background: url('icons/close.png'); background-size:cover"></button>

Since currently selected answer has some issues, posting this answer to save people trouble.

Make sure to give your button the width/height necessary to see your image as well as possible adding a "background-position" attribute to make your image show up as intended.

REACT VERSION:

<button style={{backgroundImage: "url('icons/close.png')"}}></button>   
Logan Cundiff
  • 479
  • 8
  • 13
1

It is better to use the "a" tag so as not to change the style of the image

<a href="https://example.com"> <img src="./assets/logo.png"> </a>

in your case:

<a id="close" class="closing" onClick="javascript:close_clip()"> <img src="icons/close.png" /> </a>
0

To use Image as button create a button download button image and than open it in paint and note down the top left and right bottom coordinates

`<Img src =" button.jpg" usemap=" #button" >.   
<map name = " # button " >.   
<area shape ="rect" coords = " Top- left , bottom right " 
href = " page you want to open by button" > `  

You can use multiple< area> tag to create different button from just one image .

Note : There is one issue with this method that if you try to change the height and width of the image the pixels shift and your button won't work

For that change the button image size externally by photoshop or any other photo editor

That's it you have created your button without java script and with few lines of code

Anand
  • 1
  • 1
-8

Buttons don't directly support images. Moreover the way you're doing is for links ()

Images are added over buttons using the BACKGROUND-IMAGE property in style

you can also specify the repeats and other properties using tag

For example: a basic image added to a button would have this code:

    <button style="background-image:url(myImage.png)">

Peace

proctr
  • 452
  • 4
  • 15
  • 3
    Your answer is incorrect, the official [documentation](https://www.w3.org/TR/2011/WD-html5-20110525/the-button-element.html) says that it's allowed. – biphobe Nov 07 '16 at 10:52