I'm not entirely sure this is possible. You may want to mess around with CSS variables, although you may run into the same problem you're having here; I don't believe that you can send one CSS function a value generated from another (even CSS variables are accessed through a var
function). I know it's not as satisfying, but have you considered some quick and easy jQuery?
$("div[data-img]").each(function(){$(this).css("background-image", "url(" + $(this).attr("data-img") + ")")});
Here's a forked JSFiddle
You could do something similar with vanilla Javascript, as well, if you're reluctant to add new libs to your project.
EDIT:
If you're able to edit the markup, a far simpler (although maybe not entirely possible) solution may be to just use an inline style:
<div style="background-image: url('http://www.hdwallpapers.in/walls/cute_baby_in_autumn-wide.jpg')">radomPicture</div>
I can't imagine many scenarios where adding an img-data
attribute would be significantly easier than adding a style
attribute, but I guess that depends on how all of this markup is being generated, or if that power is in your hands to begin with.