PDA

View Full Version : PHP in CSS?



Hypertext
21-03-2008, 07:49 PM
Is it possible for me to put php in a css file eg to randomize a color.

--ss--
21-03-2008, 07:54 PM
I'm not sure but there's one way of finding out , test it by trying it out ;).
If it doesn't work by doing it in the stylesheet then if it's something like the colour for one certain thing, have the css within the HTML tags with the php controlling it ;).

Hypertext
21-03-2008, 07:57 PM
If I direct css via <link> in a .css will that still allow php?

Robbie
21-03-2008, 08:41 PM
Why not make CSS files for the colours you want then randomize the css file that is included.

DeejayMachoo$
21-03-2008, 08:45 PM
no make a file i.e css.php

and have

<style type="text/css">
css goes here and you can use php if u use <?PHP around the bit you want phped dont forget ?> to close :)
</style>

[Oli]
21-03-2008, 08:45 PM
Why not make CSS files for the colours you want then randomize the css file that is included.

Or just make different (css) classes and make the php script randomize which class to be used.
Easyest in my opinion.




Regards,
Olivier Pauwels

DeejayMachoo$
21-03-2008, 08:48 PM
;4564164']Or just make different (css) classes and make the php script randomize which class to be used.
Easyest in my opinion.




Regards,
Olivier Pauwels

thats a good idea imo i think i will use that in the future

Hypertext
21-03-2008, 08:50 PM
imo?

and I already have a style.css is there any bypass, eg redirect from .css to .php or could I use mod rewrite :S

[Oli]
21-03-2008, 09:01 PM
imo?

and I already have a style.css is there any bypass, eg redirect from .css to .php or could I use mod rewrite :S

huh what ?

inside your style.css put this:

.style1{
//styles here
}

.style2{
//styles here
}

.style3{
//styles here
}

.style4{
//styles here
}

Then in your main file place a php script that randomizes to use

<div class="style1"></div>
or
<div class="style2"></div>
or
<div class="style3"></div>
or
<div class="style4"></div>

or use <span>'s, whatever you need :p


Easyest way no ?


(PS: imo = in my opinion)

Hypertext
21-03-2008, 09:16 PM
The thing is I already have like 60 pages, so i was jw if it was possible to change the .css or .htaccess

Decode
21-03-2008, 10:06 PM
Add this to the top of your .css file (you can add more colours if you want)


<?php
$colour[] = "#FFCCCC";
$colour[] = "#FFFFFF";
$colour[] = "#000000";
srand ((double) microtime() * 1000000);
$colour2 = rand(0,count($colour)-1);
?>


Then to display a random colour use this (example)



body {background-color: <?php echo "$colour[$colour2]"; ?>;
font-size: 12px;
font-face: arial;
}


:)

Hypertext
21-03-2008, 10:19 PM
Can I use php in the .css?

Decode
21-03-2008, 10:36 PM
Can I use php in the .css?
I dont know, you should try it. :)

Hypertext
21-03-2008, 10:36 PM
Will do.

QuickScriptz
22-03-2008, 03:40 AM
Make life easy for yourself:

style.php


<style type="text/css">
body{
color: <?=$_GET['color']?>;
}
</style>


And to use the above what you could do is like....



<?php
require('style.php?color=red');
?>


You don't necessarily have to do it like that but that is just an example of how you could do it. So basically just make it a php file and then include/require it instead of using a css link.

Josh-H
22-03-2008, 08:21 AM
No you can't use php in a .css file. I believe you could do this by just using

<style>
CSS HERE
</style>

at the top of each php page as you would be able to put php inside that.

Blob
22-03-2008, 10:13 AM
Do this:

style.php


<?php
header("Content-type: text/css");
?>
CSS HERE <?="with php aswell"; ?>


Then just do


<link href="style.php" rel="stylesheet" type="text/css" />

Florx
22-03-2008, 10:32 AM
Ahaha beat me to it! Grr.

Want to hide these adverts? Register an account for free!