Adding CSS to PHP - php

Alright so I was creating a normal page in html and have linked a CSS page to it. Using
<link href="css/index.css" rel="stylesheet" type="text/css" />
I was wondering if I wanted to use the same method of CSS on some PHP that is echoed out onto the page. Is there a way to do this or do I have to do it using style="" within the tag?
This is what I've tried...
#test{
width:75px;
background-color:#FFF;
display:inline-block;
}
<?php
echo 'Test'
?>

you have to put the echo values on html element with a class/id that has a css
try :
<p class="colored"><?php echo $str ?></p>

It can be achieved liked this.
<?php echo '<link href="css/styles.css" rel="stylesheet" type="text/css" />'; ?>
or perhaps
<style>
.style {
width: <?php echo $myvalue; ?>;
}
</style>

From what I understand, you want to apply dynamically generated CSS styles.
For this, it is better to use style="" directly in the tag:
<a style="width: <?php echo $myValue ?>">Link</a>
If you want to use php within a CSS file, you can create a PHP file instead of your CSS file:
style.css.php
<?php
header("Content-type: text/css");
?>
.style {
width: <?php echo $myvalue; ?>;
}
Then you can link it from your html:
page.php
<link href="css/style.css.php" rel="stylesheet" type="text/css" />
The MIME type of that PHP file will be text/css although the extension is .php
If you want the extension to be .css, you must .css files to the list of files to be interpreted by your server. If you are using Apache, you can enable mod_headers:
a2enmod headers
service apache2 restart
And create a .htaccess file with the following content:
<FilesMatch "\.css$">
SetHandler application/x-httpd-php
Header set Content-type "text/css"
</FilesMatch>
If you do this, you don't need to set the header manually in the css file.

Related

CakePHP 3.0 echo content of css file in template

I have css stylesheet for all e-mail templates which is placed in the
/webroot/css/email-layout.css
I trying to display css inline in templates using the:
<style type="text/css">
<?php echo $this->Html->css(array('email-layout.css')); ?>
</style>
But after receiving the email is still displayed like link, not inline.
How can i solve it please?
Many thanks for advice.
It's because your style is an internet link (external file)? You need to output this css file as internal file when generating e-mail. For example:
<style>
<?php echo file_get_contents($_SERVER['DOCUMENT_ROOT'].'/css/email-layout.css'); ?>
</style>
Does it help?
<?php echo $this->Html->css(array('email-layout.css')); ?>
will output
<link rel="stylesheet" href="/css/email-layout.css" />
But if you want to get the file content, use file_get_contents function
<style type="text/css">
<?php echo file_get_contents(WWW_ROOT . 'css' . DS . 'email-layout.css'); ?>
</style>
WWW_ROOT : Full path to the webroot.

How do I display css code through php variables?

I have decided to utilize php with my css for better management. I have it set up so that I can incorporate variables within my "style.php" file and use them for my css code. With that said, I would also like the ability to display these variables through an echo statement. So in my main index, I want to pretty much be able to do "echo $body;" so that I can actually display the css. It will not allow me to do so.
this is my "style.php" file located in the styling folder
<?php
header("Content-type: text/css");
$body = '
background-color:red;<br>
margin:0 auto;<br>
';
?>
body{
<?php echo $body; ?>
}
this is my "index.php" file
<html>
<head>
<link rel="stylesheet" href="styling/style.php" media="screen">
<?php
include 'globals/vars.php';
?>
<title><?php echo $title; ?> - Home</title>
</head>
<body>
</body>
</html>
You are including HTML tags in the CSS. The PHP all works fine, but you don't need a <br> at the end of every line of CSS.
You can either include the CSS php script as inline CSS through an include or you can put the variables in a separate file, include them in your CSS page as well as your main page.
The reason you can't access $body in index.php, is because $body is declared in your CSS file which is never included in your index.php file. Instead, you are referencing the CSS file using HTML which will include only the output of that php script.
Alternatively, you can look into SASS or another CSS pre-processor which supports variables and other features for "easy management".
EDIT: Regarding what Chris just mentioned, if it's new lines in your CSS file that you're after, just use "\r\n" instead of <br>.
Why do you have <br/>in your code, you should use newline \n instead
Style.php:
<?php
header("Content-type: text/css");
include 'globals/vars.php';
?>
body{
<?php echo $body; ?>
}
Index.php:
<html>
<head>
<link rel="stylesheet" href="styling/style.php" media="screen">
<?php
include 'globals/vars.php';
?>
<title><?php echo $title; ?> - Home</title>
</head>
<body>
<?php echo $body; ?>
</body>
</html>
globals/vars.php:
$body = '
background-color:red;
margin:0 auto;
';
This should work, as you're now declaring the variable $body in the same scope that you are using it.

php variables in css

I'm trying to use php variables in my .css.
I have change my style.css to style.php and I've add the next code in it:
<?php header("Content-type: text/css");
$color[0]='#ff0000';
$color[1]='#00ff00';
$color[2]='#0000ff';
$i=rand(0,2);
?>
Then I'm trying to use this color in a css property. Something like this:
background-color: "<?=$color[$i]?>";
When I try to see what happens... nothing happens. If I see on firefox inspector it seems that my code is not changing from php to html.
Any Ideas?
Use classes on body:
<?php
$class[0]='red';
$class[1]='lime';
$class[2]='blue';
$i=rand(0,2);
?>
<body class="<?php echo $class[$i]; ?>">
//The result
<body class="red"></body>
<body class="lime"></body>
<body class="blue"></body>
Work with inheritances on CSS file:
.red .my-another-class{
background-color: red;
}
.lime .my-another-class{
background-color: lime;
}
.blue .my-another-class{
background-color: blue;
}
So if you're loading style.php as a CSS file you should do this in your <head> so it's treated by the browser as such
<link href="style.php" rel="stylesheet">
IMPORTANT:
I'm trying to use php variables in my .css
You CAN NOT use php in .css files, your "css" file has to be a php file, look the example above.
In PHP: Just remove the quotes from your output:
background-color: <?=$color[$i]?>;
If your PHP version is below 5.4 and short open tag is disabled, you have to write it like this:
background-color: <?php echo $color[$i] ?>;
From the PHP docs:
Note: This directive also affected the shorthand <?= before PHP 5.4.0,
which is identical to <? echo. Use of this shortcut required
short_open_tag to be on. Since PHP 5.4.0, <?= is always available.
EXAMPLE
In this example index.html and css.php are on the same level
htdocs
|-- index.html
|-- css.php
index.html:
<html
<head>
<link rel="stylesheet" type='text/css' href="css.php" />
</head>
<body>
Sometext
</body>
</html>
your css.php:
header("Content-type: text/css; charset: UTF-8");
$color[0]='#ff0000';
$color[1]='#00ff00';
$color[2]='#0000ff';
$i=rand(0,2);
?>
body {
background-color: <?=$color[$i]?>;
}

How to use PHP inside css file [duplicate]

This question already has answers here:
How do i run PHP inside CSS
(2 answers)
Closed 9 years ago.
I have CSS file and I want to refer some image paths in that files in PHP varaible format. Then I refer that css file inside a html file. Following are my file
CSS file
<? header ("Content-type: text/css");?>
body{ margin:0px; font:9px/11px "Tahoma", Arial, Helvetica, sans-serif; color:#010000;
background:#f3f6e1 url(<?php echo base_url().'public/';?>images/body_bg_1.gif) repeat-x 0 0}
HTML file
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="css/layout.css" media="screen">
</head>
Other things. Can you explain me how to do this ?
If you're able to rename your CSS file "layout.php", there's no need for all of these workarounds:
Your layout.php file would look like:
<?php header("Content-type: text/css; charset: UTF-8"); ?>
body{ margin:0px; font:9px/11px "Tahoma", Arial, Helvetica, sans-serif; color:#010000;
background:#f3f6e1 url(<?php echo base_url().'public/';?>images/body_bg_1.gif) repeat-x 0 0}
Your HTML files would look like:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="css/layout.php" media="screen">
</head>
This question is very similar: Include: css with php file extension?
Perhaps the return value of base_url() does not end in the path separator.
With that in mind, try this:
#import url("<?php echo base_url().'/public/';?>css/layout.css");
(Notice the slash before "public")
Check the source of the page via your browser's "view source" or similar, and check if the path in the #import is correct
or
Use a request logger similar to Chrome's devtools' "network" tab to see what URL your browser is trying to load the imported CSS file from.
You also view the CSS via your browser to identify whether the contents are being correctly built. If you see <?php inside the response, you'll need to make Apache treat the CSS file as if it was PHP.
You can add something similar to the following into your .htaccess file:
<FilesMatch "\.css$">
SetHandler application/x-httpd-php
Header set Content-type "text/css"
</FilesMatch>
You should ensure that the "mod_headers" Apache module is enabled to allow the use of the Header directive.
Although, personally I would rename such dynamic stylesheets to have a .php.css extension. This will have no effect, but then Apache can be configured to only pass the dynamic stylesheets to the PHP preprocessor.
<FilesMatch "\.php\.css$">
SetHandler application/x-httpd-php
Header set Content-type "text/css"
</FilesMatch>
I believe the problem is that a .css file isn't going to be interpreted as PHP and so your code in the file is not going to be executed. If you included it as a PHP file, your code should be executed and your values should be filled in.
[Edit]
This does seem to be the way to do it, as noted by an answer someone linked to in a comment on the original post here.
It's simple if you have a bit of knowledge of url rewriting.
Write a .htaccess file in your root directory, It would look something like:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^customized\.css$ css\_generator\.php [L]
</IfModule>
Now just create a file css_generator.php having content:
<?php header('Content-type: text/css; charset: UTF-8'); ?>
body{ margin:0px; font:9px/11px "Tahoma", Arial, Helvetica, sans-serif; color:#010000;
background:#f3f6e1 url(<?php echo base_url().'public/';?>images/body_bg_1.gif) repeat-x 0 0}
Your html should look like:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="customized.css" media="screen">
</head>
Understanding what just happened.
On page load when your browser will load customized.css, it will be redirected to css_generator.php
the contents of css_generator.php will be also available as yoursite.com/customized.css
Hope this helps

How can I include CSS in php?

i want to include my css/stylesheet via php so that...
<link rel="stylesheet" href="http://www.mydomain.com/css/style.php">
so that i can than dynamicly change different stylesheets.... how can i do that
As long as you set your MIME type in style.php to CSS, you should be in business. Add this to the very top:
<?php Header ("Content-type: text/css; charset=utf-8");?>
Another option if you're running on an Apache server is to tell it to check .css files for PHP. Add this to your .htaccess file to do this:
AddType application/x-httpd-php .css
Then you could simply include a regular .css file:
<link rel="stylesheet" href="http://www.mydomain.com/css/style.css">
You can add this php code in your html head section but file should be .php.
For example: index.php
<html>
<head>
<?php
$cssFile = "style.css";
echo "<link rel='stylesheet' href='" . $cssFile . "'>";
?>
</head>
<body>
...
...
</body>
</html>
You can store any css file path in $cssFile variable using different conditions.
In style.php:
echo file_get_contents('style.css');
This will just output the contents of style.css.
Another variation of dynamically changing the page style:
<link rel="stylesheet" href="css/<?php echo $user['style']; ?>.css">
Add multiple CSS files dynamicly
The answers seem to be different that they question...
If you want to add all CSS files in the CSS map and not have to worry about changing the code whenever a css file name changes or another one is added, use:
<?php
foreach(glob("CSS/*.css") as $css_file)
{
echo '<link rel="stylesheet" href="'.$css_file.'" type="text/css" medial="all" />';
}
?>
why don't you do it the other way around and just include a different css file in your php?
print "<link rel='stylesheet' href='$path_to_css_file'>";
You can directly include CSS into a HTML file:
<style type="text/css">
<?php include 'stylesheet.php'; ?>
</style>

Categories