Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change 'src' value by css for input tag with type="image"

Tags:

html

css

markup

Is it possible to change the value of src attribute of <input type='image' alt="Text will be shown if pics are disabled" src='somepic.png'../> by css?

The problem is: I want to specify which pic will be shown as submit button just using css (so the design team will change only css files!). If I use the alternative way like <input type="submit" class="cssclass" value=" " alt="Text will be shown if pics are disabled"/> and specify the background of this element in css - it doesn't work well if pics are disabled. - No any alternative text is shown instead of pic. However the first way solves this situation...

Please advice something

Thanks.

like image 811
javagirl Avatar asked Apr 21 '11 15:04

javagirl


People also ask

How do you change the input value in CSS?

No, CSS cannot change the value attribute of an input , or indeed any attribute of any element.

Can CSS set src?

No you can't set the image src attribute via CSS.

Can we change input type with CSS?

No. CSS is a presentation language. It cannot be used to alter the semantics and structure of a document.

How do I put an image in an input tag?

The <input type="image"> defines an image as a submit button. The path to the image is specified in the src attribute.


1 Answers

Here it is: http://jsfiddle.net/kizu/66JXn/

Some notes about this solution:

  1. Use <button></button>, 'cause it can include other blocks.

  2. You'll need a bit of extra code to make all these work in Fx and IE:

    • For Fx you need an extra wrapper inside (there are positioning bug) and some extra -moz- properties reset.
    • For IE you must shrink the original button, 'cause there are some extra padding that is hard to remove.
  3. You place the text and another element inside, that would overlay the text. So when the images would absent, the text would be accessible.

That's it :)

like image 126
kizu Avatar answered Oct 29 '22 10:10

kizu