使用 php 更改 css 背景图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20877311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change css background-image with php
提问by codeninja
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?
我正在为这个网站做个人资料,我想做一个封面照片,比如像 facebook 一样的元素。我遇到的问题是,首先如何保存封面的背景图像?就像,我可以将图像的 url 保存在数据库中,但是我怎么可能把它放在 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?
这看起来非常丑陋的标记。还有其他方法吗?
采纳答案by JAL
The only other options would be to put it in a style tag, or have php generate an external CSS file.
唯一的其他选择是将它放在样式标签中,或者让 php 生成一个外部 CSS 文件。
For the tag you'd do
对于你要做的标签
<style>
div { background-image: url(<?php echo $imageURL;?>); }
</style>
That is a bit cleaner than inline HTML styling.
这比内联 HTML 样式要简洁一些。

