tags:

views:

433

answers:

3

Hi,

I'm using this css, but it puts an image in the center. Any way to left or right align an icon using an input type="button" html button, so that the text and the image fit on the button nicely?

background: url('/common/assets/images/icons/16x16/add.png');
background-position:center;
background-repeat:no-repeat;
A: 

Use this:

background-position: left/right/center; /*Choose one of the three*/
Kyle Sevenoaks
+2  A: 

If you absolutely must use input, try this:

background-image: url(...);
background-repeat: no-repeat;
background-position: <left|right>;
padding-<left|right>: <width of image>px;

It's usually a little easier to use a button with an img inside:

<button type="submit"><img> Text</button>

However the browser implementations of button for submitting are inconsistent, as well as the fact that all button values are sent when button is used - which kills the "what button clicked" detection in a multi-submit form.

Delan Azabani
don't have to, but I here button tag is buggy in ie 6 or behaves differently in some browsers?
Brett
As far as I've experienced, `button` (with type `submit`) works identically to `input` with the exception of allowing elements inside (rather than just styles applied to it).
Delan Azabani
The 'buggy' part of IE's `<button>` implementation comes from the fact that 1) on a POST / GET, it submits the 'values' for every button, and not just the one clicked, and 2) Instead of sending the actual `value` attribute, IE likes to send the contents (inner HTML) of the button. These facts make the `<button>` tag unreliable if the action taken depends on the button a user clicked.
Duroth
Great point, Duroth. Yes, that's certainly a kicker for things like an edit post form that has "Preview" and "Submit", etc.
Delan Azabani
+1  A: 

This should do do what you want.

<input type="button" name="btnAdd" value="Add Row" title="Add row" class="button_add" />

input.button_add {
    background-image: url('/images/buttons/add.png'); /* 16x16 px */
    background-color: transparent;
    background-repeat: no-repeat;
    border: none;
    cursor: pointer;
    height: 16px;
    padding-left: 16px;
    vertical-align: middle;
}
sshow