Embed image in a <button> element

HtmlCssImageButton

Html Problem Overview


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...

Html Solutions


Solution 1 - Html

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)" ... />

Solution 2 - Html

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

Solution 3 - Html

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.

Solution 4 - Html

try this

   <input type="button" style="background-image:url('your_url')"/>

Solution 5 - Html

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

For example:

<script>
function myfunction() {
}
</script>
<img src='Myimg.jpg' onclick='myfunction()'>

Solution 6 - Html

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">

Solution 7 - Html

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

Solution 8 - Html

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>

Solution 9 - Html

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>   

Solution 10 - Html

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

Solution 11 - Html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAmit HaginView Question on Stackoverflow
Solution 1 - HtmlAndrew BarberView Answer on Stackoverflow
Solution 2 - HtmlThinkingStiffView Answer on Stackoverflow
Solution 3 - HtmlEli DaviesView Answer on Stackoverflow
Solution 4 - HtmlChris PView Answer on Stackoverflow
Solution 5 - HtmlSirFireballView Answer on Stackoverflow
Solution 6 - HtmlParameswaranView Answer on Stackoverflow
Solution 7 - Htmluser13617141View Answer on Stackoverflow
Solution 8 - HtmlRay EView Answer on Stackoverflow
Solution 9 - HtmlLogan CundiffView Answer on Stackoverflow
Solution 10 - HtmlAnandView Answer on Stackoverflow
Solution 11 - HtmlproctrView Answer on Stackoverflow