19

I want to use a Submit Button Image instead of the standard Button. I searched on Google and SO and got a few different ways.

Which is the right way of using an Image as a Submit Button ?

Also i would want to add three states for the button - normal, hover, onclick

What i tried

HTML

<input type="submit" value="Subscribe">

CSS

input[type=submit] {
 background:url(../images/btn-subscribe.gif) none;
 text-indent:-9999px;
 width:109px;
 height:41px;
}

What shows up

enter image description here

What it should Display

enter image description here

asked Aug 2, 2012 at 6:43
2
  • 2
    Either remove text-indent and add value="Subscribe" in <input> or add the letters Subscribe on the image Commented Aug 2, 2012 at 6:56
  • @Harsha M V: See my answer below. Commented Aug 2, 2012 at 7:06

5 Answers 5

28

Edited:

I think you are trying to do as done in this DEMO

There are three states of a button: normal, hover and active

You need to use CSS Image Sprites for the button states.

See The Mystery of CSS Sprites

/*CSS*/
.imgClass { 
background-image: url(http://inspectelement.com/wp-content/themes/inspectelementv2/style/images/button.png);
background-position: 0px 0px;
background-repeat: no-repeat;
width: 186px;
height: 53px;
border: 0px;
background-color: none;
cursor: pointer;
outline: 0;
}
.imgClass:hover{ 
 background-position: 0px -52px;
}
.imgClass:active{
 background-position: 0px -104px;
}
<!-- HTML -->
<input type="submit" value="" class="imgClass" />

Gerwin
1,6145 gold badges25 silver badges53 bronze badges
answered Aug 2, 2012 at 7:05
Sign up to request clarification or add additional context in comments.

3 Comments

For user-interface optimization I always add the css-rule cursor: pointer;. It changes the cursor to the hand icon on hover, just like links do by default.
WARNING, some browsers (Chrome) force some styles on submit. This issue appears when you use transparent PNG as submit background - overall background will have color = "buttonface" and will not be 100% transparent. Add background-color: transparent.
The link to the image you used is broken.
11
<input type="image" src="path to image" name="submit" />

UPDATE:

For button states, you can use type="submit" and then add a class to it

<input type="submit" name="submit" class="states" />

Then in css, use background images for:

.states{
background-image:url(path to url);
height:...;
width:...;
}
.states:hover{
background-position:...;
}
.states:active{
background-position:...;
}
answered Aug 2, 2012 at 6:44

5 Comments

is adding the name=submit the key ?
What if i want to add button states ?
@HarshaMV What lbu said. name can be used to check for button click. For ex: In php if(isset($_POST['name']))
have updated my question. thats what i am getting when i try to add a bg image
@HarshaMV Made a comment below your question
9

<INPUT TYPE="image" SRC="images/submit.gif" HEIGHT="30" WIDTH="173" BORDER="0" ALT="Submit Form">
Where the standard submit button has TYPE="submit", we now have TYPE="image". The image type is by default a form submitting button. More simple

Ram Sharma
8,8157 gold badges48 silver badges59 bronze badges
answered Mar 16, 2013 at 3:57

Comments

4

It's very important for accessibility reasons that you always specify value of the submit even if you are hiding this text, or if you use <input type="image" .../> to always specify alt="" attribute for this input field.

Blind people don't know what button will do if it doesn't contain meaningful alt="" or value="".

Ram Sharma
8,8157 gold badges48 silver badges59 bronze badges
answered Mar 19, 2013 at 12:25

Comments

3

You have to remove the borders and add a background image on the input.

.imgClass { 
 background-image: url(path to image) no-repeat;
 width: 186px;
 height: 53px;
 border: none;
}

It should be good now, normally.

Lee
5641 gold badge10 silver badges32 bronze badges
answered Aug 2, 2012 at 9:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.