如何为 PHP 文件设置固定背景图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11752451/
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
How do I set a fixed background image for a PHP file?
提问by George B?lan
I have an .html file with a login form and wish to recreate it and save it as a .php file. However, I'm encountering difficulties with setting the background image and other CSS related stuff.
我有一个带有登录表单的 .html 文件,希望重新创建它并将其另存为 .php 文件。但是,我在设置背景图像和其他 CSS 相关内容时遇到了困难。
Below is my code for the .php which I'm sure it won't provide guidance to answering my question, but I don't know where to start. I've checked W3schools and many other sites.
下面是我的 .php 代码,我确信它不会为回答我的问题提供指导,但我不知道从哪里开始。我检查了 W3schools 和许多其他网站。
A great start to helping me out would on how I would include a .CSS file in .PHP file. Perhaps giving me the a quick source code example. Also, the code to set a fixed background image would be amazing.
帮助我的一个很好的开始是如何在 .PHP 文件中包含 .CSS 文件。也许给我一个快速的源代码示例。此外,设置固定背景图像的代码会很棒。
<?php
$url = 'C:\Users\George\Documents\HTML\bg.jpg';
?>
<html lang="en">
<head>
<title>This is a test</title>
<style type="text/css">
body
{
background-image:url('<?php echo $url ?>');
}
</style>
</head>
<body>
<p>This is a test.</p>
</body>
</html>
回答by George B?lan
I found my answer.
我找到了我的答案。
<?php
$profpic = "bg.jpg";
?>
<html>
<head>
<style type="text/css">
body {
background-image: url('<?php echo $profpic;?>');
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Hey</title>
</head>
<body>
</body>
</html>
回答by LiJung
It's not a good coding to put PHP code into CSS
将 PHP 代码放入 CSS 中并不是一个好的编码
body
{
background-image:url('bg.png');
}
that's it
就是这样
回答by Xhynk
You should consider have other php files included if you're going to derive a website from it. Instead of doing all the css/etc in that file, you can do
如果您打算从中派生一个网站,您应该考虑包含其他 php 文件。您可以执行该文件中的所有 css/etc 而不是
<head>
<?php include_once('C:\Users\George\Documents\HTML\style.css'); ?>
<title>Title</title>
</hea>
Then you can have a separate CSS file that is just being pulled into your php file. It provides some "neater" coding.
然后你可以有一个单独的 CSS 文件,它只是被拉到你的 php 文件中。它提供了一些“更整洁”的编码。
回答by Brad
Your question doesn't have anything to do with PHP... just CSS.
您的问题与 PHP 无关...只是 CSS。
Your CSS is correct, but your browser won't typically be able to open what you have put in for a URL. At a minimum, you'll need a file:path. It would be best to reference the file by its relative path.
您的 CSS 是正确的,但您的浏览器通常无法打开您为 URL 设置的内容。至少,您需要一条file:路径。最好通过其相对路径引用文件。

