tags:

views:

312

answers:

4

i've been trying to change the background image of the input button through css, but it doesn't work.

search.html:

    <body>
<form name="myform" class="wrapper">
  <input type="text" name="q" onkeyup="showUser()" />
 <input type="button" name="button" value="Search" onclick="showUser()" class="button"/>
 <p>
 <div id="txtHint"></div>
</p>
</body>

search.css:

.button {
  background-image: url ('/image/btn.png') no-repeat;
  cursor:pointer;
  }

what could be wrong?

even inline-ing the css didn't seem to work.

A: 

If this is a submit button, use <input type="image" src="..." ... />.

http://www.htmlcodetutorial.com/forms/_INPUT_TYPE_IMAGE.html

If you want to specify the image with CSS, you'll have to use type="submit".

Greg K
will the 'onclick' work on input="image"?
fuz3d
Type `image` and `submit` both act as submit so you can catch the submit event via the `form` tag attribute `onSubmit`. http://msdn.microsoft.com/en-us/library/ms536972(VS.85).aspx
Greg K
If you look at my code that I tested and is working, `input type="button"` and `input type="image"` can be styled using css. He just used the wrong property.
Jonathan Czitkovics
the problem is, when i use `type="submit"` the form reloads but displays no results; but when i use `type="button"`, the results are displayed. the data is being generated via ajax on the click of the button, and the form doesn't have any handler called 'onsubmit'.
fuz3d
If you're loading via AJAX, then you'd move the call to the `onSubmit` event, otherwise you're calling (I presume) via `onClick` and the form is being submitted as well.
Greg K
+1  A: 

You need to type it without the word image.

background: url('/image/btn.png') no-repeat;

Tested both ways and this one works.

Example:

<html>
    <head>
        <style type="text/css">
            .button
            {
            background: url(/image/btn.png) no-repeat;
            cursor:pointer;
                        border: none;
            }
        </style>
    </head>
    <body>
        <input type="button" name="button" value="Search" onclick="showUser()" class="button"/>
        <input type="image" name="button" value="Search" onclick="showUser()" class="button"/>
        <input type="submit" name="button" value="Search" onclick="showUser()" class="button"/>
    </body>
</html>
Jonathan Czitkovics
funny. it doesn't work for me. the firebug console only shows this: `.button {cursor:pointer;}`
fuz3d
Did you try to copy and paste the whats in the code box into an html file to see if that works for you? Also try using quotes around the path or remove the first `/`.
Jonathan Czitkovics
k, it works now [the type="button" one that is]! thanks. i wonder what was wrong with the external css. the only thing now is that it shows the image as the background of the button with the borders. how can i just show the image, without it looking like a button?
fuz3d
The code for that is just like any old div or table tag. `border: none;`
Jonathan Czitkovics
thank you very much.
fuz3d
A: 

background-image takes an url as a value. Use either

background-image: url ('/image/btn.png');

or

background: url ('/image/btn.png') no-repeat;

which is a shorthand for

background-image: url ('/image/btn.png');
background-repeat: no-repeat;

Also, you might want to look at the button HTML element for fancy submit buttons.

Tgr
+1  A: 

Just to add to the answers, I think the specific reason in this case, in addition to the misplaced no-repeat, is the space between url and (:

background-image: url ('/image/btn.png') no-repeat; /* Won't work */
background-image: url('/image/btn.png'); /* Should work */
Pekka
Actually I just tried it and your right! It does not work with the space but that's not the only problem with your code.
Jonathan Czitkovics
thanks for pointing out. i was googling for solutions on this, and one of the answers was with `no-repeat`, hence i tried it.
fuz3d