css works with php?
Moderator: General Moderators
-
naseemsadak
- Forum Commoner
- Posts: 28
- Joined: Wed Apr 15, 2009 2:50 am
css works with php?
Hi,
I just want to know if css actually works with php? I want to add a simple background image to my website but what I do not understand is, do you have to save your script as .css or .php in order for both to work? I'm using an old version of php editor 2.2 to do php scripts.
Thanks,
Naseem
I just want to know if css actually works with php? I want to add a simple background image to my website but what I do not understand is, do you have to save your script as .css or .php in order for both to work? I'm using an old version of php editor 2.2 to do php scripts.
Thanks,
Naseem
Re: css works with php?
Well CSS works on it's own, either in a seperate file or you can embed it on the page.
For example, if you put this in the <head> </head> section of your site it'll work fine.
That can be put in your PHP file if you have a <head> </head> tag.
For example, if you put this in the <head> </head> section of your site it'll work fine.
Code: Select all
<style type="text/css">
body {
background: url('http://www.url.com/picture.gif');
}
</style>-
naseemsadak
- Forum Commoner
- Posts: 28
- Joined: Wed Apr 15, 2009 2:50 am
Re: css works with php?
Thanks allot. Well if there is any useful links on working with css and php, I'd appreciate if someone could provide me with website addresses.
Re: css works with php?
What do you mean by working with css and php?
Dynamically creating CSS styles using PHP?
If so take a look at this
http://www.barelyfitz.com/projects/csscolor/
Dynamically creating CSS styles using PHP?
If so take a look at this
http://www.barelyfitz.com/projects/csscolor/
-
naseemsadak
- Forum Commoner
- Posts: 28
- Joined: Wed Apr 15, 2009 2:50 am
Re: css works with php?
Meaning working with css in php scripts. I've been searching the net for some info and from what I read, that for css you have to save your script as .css. I just tried creating a simple php script with css for background image but couldn't get it working. I know html and css work, just my confusion is php working with css.
-
naseemsadak
- Forum Commoner
- Posts: 28
- Joined: Wed Apr 15, 2009 2:50 am
Re: css works with php?
Code: Select all
<html>
<head>
<?php
header("Content-type: text/css");
?>
body
{
background: url(<?php echo('http://www.travelblog.org/Wallpaper/pix/waterfall_desktop_background-1600x1200.jpg');?>;
}
</head>
<body>
</body>
</html>Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\testcss.php:3) in C:\xampp\htdocs\testcss.php on line 4
body { background: url(http://www.travelblog.org/Wallpaper/pix ... 0x1200.jpg; }
Tried my best but still stuck.
Last edited by Benjamin on Tue May 05, 2009 1:20 pm, edited 1 time in total.
Reason: Changed code type from text to html.
Reason: Changed code type from text to html.
Re: css works with php?
This is completely wrong.....you do not use the header() for parsing css.naseemsadak wrote:Code: Select all
<html> <head> <?php header("Content-type: text/css"); ?> body { background: url(<?php echo('http://www.travelblog.org/Wallpaper/pix/waterfall_desktop_background-1600x1200.jpg');?>; } </head> <body> </body> </html>
Please read what the php header does
http://php.net/header
And as far your content goes it is almost correct, you do not need to echo out the image location
Code: Select all
<html>
<head>
body
{
/* WORKS *
background: url('http://www.travelblog.org/Wallpaper/pix/waterfall_desktop_background-1600x1200.jpg');
/* ALSO WORKS */
background: url(<?php echo '"http://www.travelblog.org/Wallpaper/pix/waterfall_desktop_background-1600x1200.jpg"'; ?>);
}
</head>
<body>
</body>
</html>
Last edited by Benjamin on Tue May 05, 2009 1:20 pm, edited 1 time in total.
Reason: Changed code type from text to php.
Reason: Changed code type from text to php.
-
naseemsadak
- Forum Commoner
- Posts: 28
- Joined: Wed Apr 15, 2009 2:50 am
Re: css works with php?
So would I need a style tag or do away with both style and header tags?
-
naseemsadak
- Forum Commoner
- Posts: 28
- Joined: Wed Apr 15, 2009 2:50 am
Re: css works with php?
I want css to work in php
Re: css works with php?
I think you're misunderstanding what PHP does - a common misunderstanding.
PHP is a server side language. The browser never sees it. When you go to a URL that is a PHP file, the server executes the PHP file and that PHP file then outputs something. It can be an image, a csv, but most often it's an HTML file.
Your browser then receives that html code & treats it like it should - as html.
Any CSS you are using interacts with your html on your computer. PHP and CSS never interact, nor can they. One is server side, the other is client side.
PHP is a server side language. The browser never sees it. When you go to a URL that is a PHP file, the server executes the PHP file and that PHP file then outputs something. It can be an image, a csv, but most often it's an HTML file.
Your browser then receives that html code & treats it like it should - as html.
Any CSS you are using interacts with your html on your computer. PHP and CSS never interact, nor can they. One is server side, the other is client side.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
-
naseemsadak
- Forum Commoner
- Posts: 28
- Joined: Wed Apr 15, 2009 2:50 am
Re: css works with php?
Thanks this was the answer I was looking for at first whether css and php work with each other and now I know.
Re: css works with php?
Here's an example of using php with css. It uses an inline style, but it could just as easily be used in the style definitions in the head of the document.
Also, this is not written in the most efficient way, but it serves to illustrate. The query string could be in the URL, such as:
http://www.example.com?f=aging
or it could be submitted via a form.
Code: Select all
if($f->query_string == 'young_adults') {
echo ' style="background-image:url(../images/backgrounds/bg_young_adult.jpg);"';
}
if($f->query_string == 'parents') {
echo ' style="background-image:url(../images/backgrounds/bg_parenting.jpg); "';
}
if($f->query_string == 'motherhood') {
echo ' style="background-image:url(../images/backgrounds/bg_motherhood.jpg);"';
}
if($f->query_string == 'aging') {
echo ' style="background-image:url(../images/backgrounds/bg_aging.jpg);"';
}
http://www.example.com?f=aging
or it could be submitted via a form.
Re: css works with php?
I think what you mean is a sort of dynamic CSS -
There are some ways you can achieve this.
Instead of you having a seperate file .css you have inline CSS with the "script" block. This allows you to add CSS to the .php page...
File.php
The second alterative is to create a php page of which sole serves css formatted data...
You can still use the header() command...
or finally - change where the header gets sent...
then you referance this as you would any other css page...
There are some ways you can achieve this.
Instead of you having a seperate file .css you have inline CSS with the "script" block. This allows you to add CSS to the .php page...
File.php
Code: Select all
<?php
$header_color = "#AAFF00" //some sort of dynamic color creation...
?>
<html>
<head>
<style type="text/css">
.headerColor{
color: <?php echo $header_color; ?>;
}
</style>
</head>
<body>
<h1 class='headerColor'>My Header 1</h1>
...
</body
</html>
The second alterative is to create a php page of which sole serves css formatted data...
You can still use the header() command...
Code: Select all
<html>
<head>
<?php
/*
By right, you cannot send a header once you placed ANY character in the output buffer (the stuff to see),
in this code, you have already "sent" '<html>' and '<head>' as such, destroy the data you need to send out...
*/
ob_flush();
ob_end_clean()
header("Content-type: text/css");
?>
body
{
background: url(<?php echo('http://www.travelblog.org/Wallpaper/pix/waterfall_desktop_background-1600x1200.jpg');?>;
}
</head>
<body>
</body>
</html>or finally - change where the header gets sent...
Code: Select all
<?php
header("Content-type: text/css");
echo ".headerColor{\ncolor: red;\n}\n";
Code: Select all
<style type="text/css">
@import url('dynamic_css.php');
</style>
Last edited by Benjamin on Tue May 05, 2009 3:07 pm, edited 1 time in total.
Reason: Changed code tags to html, php
Reason: Changed code tags to html, php
-
naseemsadak
- Forum Commoner
- Posts: 28
- Joined: Wed Apr 15, 2009 2:50 am
Re: css works with php?
I found this article that has helped me http://www.daniweb.com/forums/thread160935.html#