Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases

dylan kane
dylan kane
2,772 Points

Insert Json document with an image path value into a css background attribute

I have a mongodb database with documents that have img values set up like this:

"img": "../folder/img.jpg"

is it possible to use this string in my css to change the background image? I need to do this because the image should change with other values in the json document.

1 Answer

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Dylan;

You would need to utilize some JavaScript/JQuery in there too to alter the CSS, right? You could write a script that will read the JSON document and determine if name = "Dylan", for example you pull the user.photo data from your JSON data and "push" it into your CSS/HTML. There is likely something like this out there already, perhaps not exactly or with your particular dev stack, but I can't image that it doesn't exist.

Ken