bohowe's avatar

I want to use model data in CSS

I'm trying to display an uploaded image in the CSS but it's not working

.profile-background {
    background: url("../storage/backgrounds/$user->background_image") no-repeat;
}

How to make this work?

0 likes
2 replies
bobbybouwmann's avatar
Level 88

Your PHP variables are not available inside your CSS. You either need to add the CSS dynamically or set the styles inline. Here are some examples

<style>
.profile-background {
    background: url("../storage/backgrounds/$user->background_image") no-repeat;
}
</style>

Or using inline styling

<div style="background: url("../storage/backgrounds/$user->background_image") no-repeat;"></div>

I would probably pick the inline styling if it's just the background ;)

3 likes

Please or to participate in this conversation.