Jump to content

How to convert a CSS background property into foreground?

Recommended Posts

Assume I have a CSS background key similar to
.logo { background: url(/static/foobar/mylogo.jpg) 0px -no-repeat; }
Is there a way to convert this background property into a foreground image by Javascript?
All other configurations (margin, padding, background-position-x, background-position-,....) should remain untouched or converted accordingly.
Thank you
Edited by pstein
Link to post
Share on other sites

I think you could use getComputedStyle() to find out what background image has been applied and then generate an <img> tag. It's a bit complicated, especially if you want to keep everything exactly where it is. You'll have to pull out the values of background-size and background position and use them to set the size and position of the <img> tag. Repeating backgrounds would make things a lot more complicated.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...