I have a title in my header that changes depending on the page you're on using this simple PHP code..
<?php if(empty($title)) $title = "ASK REAL QUESTIONS, <br>GET FREE ANSWERS.";
echo $title; ?>
Here is an example of a page titled "QUESTIONS AND ANSWERS".. The title would say "QUESTIONS.ANSWERS" instead of "ASK REAL QUESTIONS, GET FREE ANSWERS."
<?php
$title = "QUESTIONS<br>.ANSWERS";
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/includes/header.php";
include_once($path); ?>
and the CSS of $title..
.title {
margin: 0 auto;
color: #fff;
box-sizing: border-box;
line-height: 24px;
padding-top: 12px;
font-size: 125%;
position: absolute;
text-shadow: 2px 2px 5px #313131;
display: inline-block;
text-align: center;
}
Now here's what I want...
Where it would say "QUESTIONS.ANSWERS".. I want the QUESTIONS to be colored #fff as declared in the CSS... but I want the ".ANSWERS" to be colored #39f.. how can I declare this in PHP?
I tried this... but I get syntax errors.
<?php
$title = "QUESTIONS<br><font color="#39f">.ANSWERS"</font>;
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/includes/header.php";
include_once($path);
?>
THANKS! :)
PHP can be injected into css. How I would do this is make my .css file a .php instead and preface my css scripts with a <style>
tag. Then I would, in $title, add a span tag in front of both QUESTIONS and ANSWERS with different id's. Then I would go into my new php file and add conditional statements that dynamically change the color of the text when my conditions are met.
For example:
We place <span>
tags in front of the two words and give them unique id's. This acts as a specific reference point for css.
<?php
$title = "<span id='question'>QUESTIONS</span><br>.<span id='answer'>ANSWERS</span>";
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/includes/header.php";
include_once($path);
?>
$path is what changes so we will use that as our condition. (As that is what will change when we want the style to change.
title in my header that changes depending on the page you're on using
We then change our title.css to titlecss.php. It look like this now.
<style> //need style tags now that it is .php document
.title {
margin: 0 auto;
color: #fff;
box-sizing: border-box;
line-height: 24px;
padding-top: 12px;
font-size: 125%;
position: absolute;
text-shadow: 2px 2px 5px #313131;
display: inline-block;
text-align: center;
}
//with the new id's we inject a php tag after `color:` that echoes the new color
//based on the conditional statement we make (still in `titlecss.php`):
#question {
color: <?php
if $path === "(whatever $path would be for this style)"{
echo "color: #fff;" }
?>
}
#answer { } //same as #question
</style>
Now when $page
meets our requirements titlecss.php
will dynamically change the color of the text.
In header.php
you will need to use an include
function instead of <link>
to bring titlecss.php
to the page.
Your code not working because you closed string with " character before it's really ends.
Try this (I changed old deprecated tag "font" to "span" and replaced double quotes to single quote):
<?php
$title = "QUESTIONS<br><span style='color:#39f'>ANSWERS</span>";
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/includes/header.php";
include_once($path);
?>