Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums CSS Question to experts: How to attach a Link to a background image? Re: Question to experts: How to attach a Link to a background image?

#79164
ImpInaBox
Member

Am I right in assuming you want to be able to click the background image and then maybe display it on its own, or in a new tab/window? Like using Firefox: right click the background image and choose view background image?

If you want to do it programmatically (scriptically??) this might be worth a try…

Set an onclick event for the element with the background image that calls a javascript function to get the background image url from the CSS and then go to it…



...

getComputedStyle() is necessary if the background image has been set in a separate style file rather than an inline style – which is usually the case.

The backgroundImage property is the string that sets the background image – something like url(….). The substring bit removes the url(” and “) from the backgroundImage property of the element leaving just the URL itself: http://www….whatever. Quick and dirty but works OK in Firefox.

Any <a> tags in the element will still work but if you click on an otherwise inactive area you should get just the bg image.