Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change css background-image with php

Tags:

html

css

php

I'm doing a profile for this website, and I wanted to do a cover photo like element like facebook has. The problem I have ran into is, first how do I save the background image of the cover? Like, I can save the url of the image in the database, but how would I possibly be able to put it in css?

I do not really want to just put:

<div style="background-image:url(<?php echo $imageUrl?>) no-repeat center center fixed">
</div>

That looks really ugly markup. Is there some other way?

like image 566
CodeTrooper Avatar asked Jan 01 '26 00:01

CodeTrooper


1 Answers

The only other options would be to put it in a style tag, or have php generate an external CSS file.

For the tag you'd do

<style>
  div { background-image: url(<?php echo $imageURL;?>); }
</style>

That is a bit cleaner than inline HTML styling.

like image 118
JAL Avatar answered Jan 03 '26 14:01

JAL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!