I have a css file named test.css and I want to use into it of $var.$var is at test.php. test.css is attached in test.php. My structure is something like this:
//test.php
<html>
<head>
<?php $var = 'anything';?>
<link href="test.css" rel="stylesheet" type="text/css" />
</head>
<body></body>
</html>
and this is test.css:
// test.css
.<?php echo $var> { // css property }
Currently test.css does not work. In fact, I want to knoe how can I use of a php variable as a class name into a css file ?
Actually you can.
1st Solution
Instead of using the .css file extension, use .php
Set up variables
<?php
header("Content-type: text/css; charset: UTF-8"); //look carefully to this line
$brandColor = "#990000";
$linkColor = "#555555";
?>
Use variables
#header {
background: url("<?php echo $CDNURL; ?>/images/header-bg.png") no-repeat;
}
a {
color: <?php echo $linkColor; ?>;
}
...
ul#main-nav li a {
color: <?php echo $linkColor; ?>;
}
2nd and short solution
Create a file and name it like style.php, then in your style.php set your styles in tags like below
style.php
<style>
.blabla{
....
}
#heeeHoo{
...
}
</style>
then include style.php to your file (test.php) like
<html>
<head>
<?php include 'style.php'; ?>
</head>
<body>
</body>
</html>
That is the correct answer. Think like inline css but that is actually in external file
You can use <style> in the PHP file.
//test.php
<html>
<head>
<?php $var = 'anything';?>
</head>
<body>
<style>
<?php echo $var; ?>
</style>
</body>
</html>
The style can also be put in the <head>.
Related
I have embedded my CSS file as PHP to make it more dynamic as shown in various examples online but I can't seem to get it working
<link rel='stylesheet' href='myphpstylesheet.php'>
My PHP file which I added in the link
<?php
$bgcolor = '#FF00FF';
?>
<style>
Div #container {
Background-color: <?php echo $bgcolor ; ?>
}
</style>
My HTML file
<html>
<head>
<link rel='stylesheet' href='myphpstylesheet.php'>
</head>
<body>
<div id="container ">
I am a div with lorem ipusum
</div>
</body>
</html>
But the color does not apply please help
I did some search on embedding css as php file,
I don't really see the reason for this just use css as intended.
Anyway you can try these methods hope it fixes you problem also take note of herf and hrefas pointed out by Riggs Folly as they are not the same.
METHOD 1
You can edit your httpd.conf or .htaccess with this line
AddType application/x-httpd-php .css
the web server will now parse PHP code that is within the CSS files but i would
avoid messing with those at all costs.
METHOD 2
You can also include a PHP file, in the same manner as you include a CSS file
<link rel="stylesheet" href="style.php" media="screen">
this should be done within the head of the HTML document that is the <head></head> tags
Then your style.php file should look something like this:
<?php
header("Content-type: text/css; charset: UTF-8");
$brandColor = "#990000";
$linkColor = "#555555";
$CDNURL = "http://cdn.blahblah.net";
?>
Then your css file should look somthing like this:
#header {
background: url("<?php echo $CDNURL; ?>/images/header-bg.png") no-repeat;
}
a {
color: <?php echo $linkColor; ?>;
}
ul li a {
color: <?php echo $linkColor; ?>;
}
You should use PHP's include command to inject the external PHP:
<?php include 'myphpstylesheet.php'; ?>
Inside that PHP file, you have a missing PHP closing tag, and the CSS needs to be declared properly within <style> tags:
<?php
$bgcolor = '#FF00FF';
?>
<style>
div #container {
background-color: <?php echo $bgcolor ; ?>
}
</style>
There's many ways to have dynamic control over CSS.
If you wanted to maintain the method, and truly feed it a dynamic PHP file, you have to make sure the PHP output is ONLY css. Here's an article on that.
It's possible to define a main color and a secondary color (hex-codes) in my application, those are saved to the db.
The secondary color for example is used for links. I don't want to say Text but instead <a href="#" class=secColor>Text</a> where .secColor has something like
.secColor {
color: $fromDatabase;
}
I'm using Laravel btw.
You can include a .php file as a css by using following code:
index.html:
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" href="style.php">
</head>
<body>
<!-- stuff goes here -->
</body>
</html>
style.php:
<?php
header("Content-type: text/css");
?>
//DB Query
.secColor{
color: <?php echo $fromDatabase;?>
}
You can do this:
Create new route rule:
Route::get('style/generate.css', function ($id) {
// take your color
$data['firstColor']= Colors::where('alias', '=', 'firstColor')->get();
...
return View::make('css.colors', $data)
});
And create new view in resources/views/css/colors:
.firstColor{
color: $colors['firstColor'];
}
And in your main views
I use this method for create js custom file
I have a php file called shtype_porosine.php and also an external css file style.css. The php file I use to login to the web site and it has three different div tags. For the first one (div id="container_dyte") the body backgraound color must be white, but for the two others that are included include('include/administrator_index_nsp.php'); and include('include/login_forma.php'); the body color must be black.
I am not sure if a can use the body tag three times in the same php document, if so than the problem is solved (with body class="" i founded in the previous question "how-to-css-if-else-to-change-body-background-color"), but if not than which would be the solution?
Below is the code of the php file shtype_porosine.php
Thank you
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>website</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
session_start();
// this is for an administrator
if(isset($_SESSION['auser'])){
?>
<div id="container_dyte">
<span>Something here...</span>
</div>
<?php
}else{
// these is for a user
if(isset($_SESSION['p_user'])){
include('include/administrator_index_nsp.php');
}else{
// this is the login form to apear if fails the adminstrator and the user
include('include/login_forma.php');
}
}
?>
</body>
</html>
Check if $_SESSION[ 'auser'] is set, if it is, make a body with a white background-color.
Otherwise make a body with a black background-color.
Like this:
<?php
session_start();
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>website</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<?php
if(isset($_SESSION['auser'])){ ?>
<body style='background-color:white'>
<?php } else { ?>
<body style='background-color:black'>
<?php }
if(isset($_SESSION['auser'])){
?>
<div id="container_dyte">
<span>Something here...</span>
</div>
<?php
}else{
// these is for a user
if(isset($_SESSION['p_user'])){
include('include/administrator_index_nsp.php');
}else{
// this is the login form to apear if fails the adminstrator and the user
include('include/login_forma.php');
}
}
?>
</body>
</html>
<body class="<?= isset($_SESSION['auser']) ? 'white' : 'black';?>">
<body style="background-color:<?= isset($_SESSION['auser']) ? 'white' : 'black';?>">
You don't have to change any "body". Just set the background-color css property for each of the div:
<div id="container_dyte" style="background-color:white">
In file include/administrator_index_nsp.php
<div style="background-color:black">
In file include/login_forma.php
<div style="background-color:black">
Easy way:
Give the divs diferent ID names and use CSS.
HTML
<div id="the_id">
CSS
#the_id {
background-color:white;
}
Medium way:
Make a container div with a specyfic ID or CLASS and set up a CSS.
CSS
#container div {
background-color: black;
}
#container:first-child {
background-color: red;
}
#container:last-child {
background-color: white;
}
Hard way:
If you have a css specyfic for this page you can select the div number by style property.
body:nth-child(1) { background-color: black; };
body:nth-child(2) { background-color: red; };
body:nth-child(3) { background-color: white; };
If the css is ment for other pages do not use this answer.
In order to solve this issue, include a custom css, which will apply a required background color to the <body> tag. For example:
if (mode=='mode1')
{
echo "<div id='div1'></div>";
echo "<style type='text/css'> body { background-color:red } </style>";
}
if (mode=='mode2')
{
echo "<div id='div2'></div>";
echo "<style type='text/css'> body { background-color:yellow } </style>";
}
if (mode=='mode3')
{
echo "<div id='div3'></div>";
echo "<style type='text/css'> body { background-color:blue } </style>";
}
So using this logic, you can set the background color of the body, according to the divs showed
Ok, let me explain:
I have a some files, something basic like this:
index.php
<html>
<head>
<title>Simple page</title>
</head>
<body>
<?php include 'home.php'; ?>
</body>
</html>
home.php
<div class="thisWillBeBlue">Still not blue</div>
style.css
.thisWillBeBlue {background: blue}
Now the question: Using php I want to insert the style.css inside the head tag, calling it from the file home.php. Well, I came out with a solution, but it was not very effective:
index.php
<?php $css = array();
$css[] = 'linktothecss.css'
?>
<html>
<head>
<title>Simple page</title>
<?php
foreach($css as $item){
echo "<link rel='stylesheet' href='".$item."' />";
}
?>
</head>
<body>
<?php include 'home.php'; ?>
</body>
</html>
But the problem it is, If I call the css from home.php it will be added to the array later, therefore it will not be echoed inside the head tag. Any ideas?
You could do it using ob_start() and ob_end_flush() functions
e.g.
index.php
<?php
$csspage = "default.css";
function loadCSS($buffer) {
global $csspage;
return (str_replace('{{ css }}', $csspage, $buffer));
}
ob_start("loadCSS"); ?>
<html>
<head>
<!-- the string {{ css }} is just a placeholder that will be replaced
with the new value of $csspage defined later in the code, otherwise
it will replaced with its initial value (default.css)
-->
<link href="{{ css }}" />
</head>
<body>
<?php include 'home.php'; ?>
</body>
</html>
<?php ob_end_flush(); ?>
home.php
<?php $csspage = "custom_style.css"; ?>
<div class="thisWillBeBlue">blue</div>
Further reference: http://it1.php.net/ob_start
I think you are looking for something like this ..(include a piece of code in their header files, so that it will allow you to add more stylesheets )
This will allow you to add more stylesheets to it on each page.
(add this to <head>)
<?php
if (!empty($styles) && is_array($styles)) {
foreach ($styles AS $style) {
echo '<link rel="stylesheet" href="/assets/css/'. $style .'">';
}
}
?>
You can put a variable at the top of an individual script if you need a specific stylesheet:
<?php
$styles = array('custom_style.css');
?>
CSS file references can be placed in the body of your code, if needed.
<body>
<link href="linktothecss.css" rel="stylesheet" type="text/css" />
<div class="thisWillBeBlue">
I'll be blue as soon as linktothecss.css finishes loading!
</div>
</body>
The only difference is that when in the HEAD, they are guaranteed to be loaded before the page is rendered. When they are in the BODY, there may be a split-second where they are still loading and the styles haven't been applied yet.
If you definitely want them in the HEAD, you could define the css requirements in a separate folder with the same file name, like so:
index.php:
<html>
<head>
<?php
include('css-requirements/home.php');
?>
</head>
<body>
<?php include('home.php'); ?>
</body>
</html>
and
css-requirements/home.php:
<link href="mycss.css" rel="stylesheet" type="text/css" />
<link href="myothercss.css" rel="stylesheet" type="text/css" />
1) database
2) php
3) css
4) html form
The user enters some properties for example background: red in the html form and it saves in the db, then php loades from the db the color option in background css... and then how can I put php variable in the css properties...
also some hostings does not allow to add handlers in .htaccess, is there other options?
Yes, it is possible.
You can serve a css file with php by setting the content-type.
<?php header('content-type: text/css; charset=utf-8'); ?>
You would output the css from the php file.
You could store values in a mySQL or other table with a form, and later retrieve them with the mySQLi or other library.
This is an example of the php script:
<?
header('content-type: text/css; charset=utf-8');
$bgColor = "#FFF"; // Get it from database, I'm setting manually for this example
?>
body{
background-color:<?php echo "$bgColor" ?>;
}
Alternatively, you could output inline css on the page. Either directly in to a tag, or inline on elements.
If really needed a native css file for some reason (I can't think of one), you could technically overwrite a specific css file using php's fwrite() function, though I wouldn't recommend doing it this way.
<?php
$filename = "phpstyle.css";
$fp = fopen($filename, 'w');
$bgcolor = "#FFF";
$css = "body{";
$css .= "background-color:".$bgcolor.";";
$css .= "}";
fwrite($fp, $css);
fclose($fp);
?>
Yes you can serve a css through php script.
One method is to output style elements through php.
<?php
#css on php file
$color = '#FFFFFF';
?>
<html>
<head>
<title>Css through php</title>
<style>
.test{
text-color: <?php echo $color; ?>
}
</style>
</head>
<body>
<p class="test">Your body </p>
</body>
</html>
Another method is to generate css dynamically in a php file and linking it on an html page.
<?php
#css on php file [test_css.php]
header("Content-type: text/css");
$color = '#FFFFFF';
?>
.test{
text-color: <?php echo $color; ?>
}
<html>
<!-- test.html -->
<head>
<title>Css through php</title>
<link rel="stylesheet" type="text/css" href="test_css.php">
</head>
<body>
<p class="test">Your body </p>
</body>
</html>
In addition to Khaleel's answer, you can put CSS in the HTML document itself, in a
<style>
...
</style>
section. You can use PHP variables to fill this in.
Or you can use inline styles in the HTML
<span style="...">...</span>
Why not just:
<?php if (myCondition) { ?>
<style>.myClass {
background: <?php echo $myColor; ?> !important;
} </style>
<?php } ?>
The myCondition triggers the insertion of in body styling. The !important tag overwrites all other existing value of that property.
Simple to understand and easy to use. Works every time.
ether create a css file using php dynamically, such as:
<link href="../styles/styles.php" type="text/css" />
or use php to write the css inline on your html file:
<html>
<head>
<style type="text/css">
body {
background-color: <?php echo $redcolor; ?>;
}
</style>
</head>
<body></body>
</html>
however, you cannot create a standard css file with php.