Utilizing PHP for CSS Variables:
If you are already using PHP
within your Wordpress
setup, one approach to manage variables is by creating a dedicated php file.
Instead of linking directly to a css file, you can use a dynamic style.php
.
<link rel='stylesheet' type='text/css' href='css/style.php' />
In the header section of your style.php
, specify the content type as css
.
<?php
header("Content-type: text/css; charset: UTF-8");
?>
You can then define global variables within your style.php
like so:
<?php
header("Content-type: text/css; charset: UTF-8");
$brandColor = "#990000";
$linkColor = "#555555";
$borderRadius = "0";
?>
Subsequently, when styling elements, you can reference these variables in the following manner:
a {
color: <?php echo $linkColor; ?>;
border-radius: <?php echo $borderRadius; ?>
}
Considering Css Pre-processors:
Another advantageous option is utilizing css pre-processors which provide enhanced organization capabilities for css files and variables. These tools offer support for not only variables but also advanced features like functions
, mixins
, loops
, etc. My personal preference is Sass, however, there are other alternatives such as Less, among others, worth exploring.