用php改变css背景图片

问题描述:

我正在为这个网站做个配置文件,并且我想要做一个像facebook一样的元素的封面照片。我碰到的问题是,首先如何保存封面的背景图像?就像,我可以将图像的url保存在数据库中,但是我怎么能够把它放在css中呢?

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生成外部CSS文件。

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>

这比内嵌HTML样式更简洁。

That is a bit cleaner than inline HTML styling.