Change css background-image with php

2020-07-22 18:09发布

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?

标签: php html css
1条回答
疯言疯语
2楼-- · 2020-07-22 19:01

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.

查看更多
登录 后发表回答