tags:

views:

40

answers:

4

I want to know how I can change a normal default grey HTML button into an image using CSS.

This is the image I want to change into:

button.star {
    background-image: url(img/star.jpg); 
}

alt text

This is the image I'm using. It's for favoriting an item. Thanks :)

Is this the way to do it?

A: 

It is indeed :)

background-image will happily take a url() statement. If you have a bunch of background properties they can be condensed into just one background clause.

Eg.
background-image: url('img/img.png'); background-repeat: no-repeat; background-color: #5500ff;
would turn into
background: url('img/img.png') no-repeat #5500ff;

JamWaffles
thanks i put that, and i did this `<input type="button" id="star" value="favorite">`, but its not showing
getaway
what you have done is use the class selector (`.`), but you gave the `<input>` an ID, not a class. Change `id="star"` to `class="star"`, or change the `.` for a `#` in the .CSS file.
JamWaffles
+2  A: 

That's just some random image from imgur, but basic concept. If there's no content in the button make sure you set the height/width otherwise it won't stretch to fit the button.

As a background

button {
    background: url('http://imgur.com/I0EwG.png') transparent;
    height: 48px;
    width: 45px;
    border: none;
}​

http://jsfiddle.net/robert/Wzu2S/

In the button

HTML

<button><img src='http://imgur.com/I0EwG.png' /></button>​​​​​​​​​​​​​

CSS

button {
    border: none;
    margin: 0;
    padding: 0;
    background: transparent;
}

http://jsfiddle.net/robert/DjYmR/

Robert
how come when i do the css from an external file it deosnt work, but when i do it in the html page, it works whats the problem
getaway
Make sure you don't have anything overriding your CSS. An external sheet is going to have lower priority over inline CSS or any CSS declared on the html file.
Robert
no everything is done externally, this is the inline css that i have? its confusing me lol
getaway
Do you have a live example up I can look at?
Robert
im going to put the whole css file in jsfiddle for is that alright http://jsfiddle.net/DjYmR/4/
getaway
Well that'll be the same as you having it in the HTML document. I was hoping for the link to the web page where you're having the trouble. Without that I'm only guessing that the problem is that it's not finding the image at `img/avatar.jpg`. See if an absolute URL resolves the issue.
Robert
your so right, thanks, sorry for being so dumb, i need to wake up :)) lol i wish i can give you two right answers!! cheers
getaway
A: 

Why button.star if it's a <input> tag ??

MatTheCat
He's probably using `<button></button>`, I don't see any mention of `input`
Robert
Look at his comment http://stackoverflow.com/questions/3917091/change-a-css-button-into-a-star-image/3917101#3917101
MatTheCat
A: 

Your selector is wrong.

button.star means an element of type button with a className of star

I suggest you try

#star {
  /* styles */
}

or

input#star {
  /* styles */
}

or just use the button element instead of input. Make sure you make it type="button" so the form will not post in IE.

Robusto