PepsiGam3r
PepsiGam3r

Reputation: 302

Is it possible to get an HTML attribute to CSS?

I'm making a carousel and I wanna set a background image for every carousel item and I want the background image to be the same image that's inside the item, so the image has a src attribute and I want to get it into the CSS automatically to set it as a background

Sorry for my bad English

Upvotes: 2

Views: 50

Answers (1)

Lal
Lal

Reputation: 14810

It can be done with Javascript

Suppose, you have an <img> like

<img id="my_img" src="my_img_src"/>

Then, you can get the src of the <img> as follows

var myimgsrc = document.getElementById("my_img").src;

Now, the variable myimgsrc holds the src of your <img>.

Again using Javascript you can use the variable to set the CSS like,

document.getElementById("my_img").style.backgroundImage = "url('"+myimgsrc+"')";

Try this..

Upvotes: 1

Related Questions