Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to change img src attribute using css?

Tags:

css

image

src

I'm trying to change img src (not the background img src) with css

<img id="btnUp" src="img/btnUp.png" alt="btnUp"/>  

#btnUp{
    cursor:pointer;
}
#btnUp:hover{
    src:img/btnUpHover; /* is this possible ? It would be so elegant way.*/
}
like image 943
qadenza Avatar asked Jul 17 '13 16:07

qadenza


People also ask

Can we use img tag in CSS?

Styling with CSS<img> is a replaced element; it has a display value of inline by default, but its default dimensions are defined by the embedded image's intrinsic values, like it were inline-block . You can set properties like border / border-radius , padding / margin , width , height , etc. on an image.

How do you change the image of a link in CSS?

You can't change it with CSS, you need Javascript for that. But if you are trying to change the image on for example hover, you could remove the img-tags and use a div instead on which you set a background-image. In your CSS you can then create a block where you change the image on hover.

How can I change my src value?

You can change an HTML image src attribute programatically by using JavaScript. First, you need to grab the HTML element by using JavaScript element selector methods like getElementById() or querySelector() and assign the element to a variable.

How can I change src image in HTML?

To change the source or src of an image, you need to add an id or class to the image tag. You can get the image element using the name of the id or class , and you can change the source or src of the image using the src property.


Video Answer


2 Answers

You can use :

content: url("/_layouts/images/GEARS_AN.GIF")
like image 96
gjgsoftware.com Avatar answered Oct 03 '22 16:10

gjgsoftware.com


There is another way to fix this : using CSS box-sizing.

HTML :

<img class="banner" src="http://domaine.com/banner.png">

CSS :

.banner {
  display: block;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  background: url(http://domain2.com/newbanner.png) no-repeat;
  width: 180px; /* Width of new image */
  height: 236px; /* Height of new image */
  padding-left: 180px; /* Equal to width of new image */
}

http://css-tricks.com/replace-the-image-in-an-img-with-css/

like image 42
Ben Tayaa Avatar answered Oct 03 '22 16:10

Ben Tayaa