CSS Variables

A step-​​by-​​step guide to vari­ables for your stylesheets. Native CSS vari­ables have long been requested by prac­ti­tion­ers of web-​​design. The debate regard­ing appro­pri­ate­ness of native vari­ables has spanned the 14 year his­tory of CSS with­out near­ing res­o­lu­tion. Regard­less, you can use PHP for vari­able han­dling (and more) in your CSS today.

Using vari­ables in CSS sim­pli­fies web­site devel­op­ment and main­te­nance. Con­sider the case of color man­age­ment. Defin­ing your color scheme as vari­ables used through­out your stylesheet will facil­i­tate minor color tweaks or major color refreshes. You can even use vari­ables to man­age spa­cial rela­tion­ships rel­a­tive to font size (with­out the headache of man­ag­ing em based inheritances).

CSS does not directly facil­i­tate vari­ables. To include vari­ables in your stylesheet, you can over­lay a server-​​side script­ing lan­guage (such as PHP).

First, you will need to define your stylesheet as a PHP doc­u­ment. Rather than nam­ing your stylesheet style.css, name it style.php. You can then include this stylesheet in the nor­mal fashion:

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

Most browsers assume file names end­ing in .php indi­cate HTML out­put. Con­se­quently we must add a bit of code to the top of your stylesheet to announce CSS out­put. On the first line of your style.php file, enter:

<?php header("Content-type: text/css"); ?>

Now declare your variables:


<?php
	// color scheme
	$mainBG = "#2e0303"; //burgandy
	$contentBG = "#f3e7ca"; //cream
	$textColor = "#333"; //dark gray

	$fontSize = 16;
	$lineHeight = 1.25*$fontSize; //20px
?>

These vari­ables are now avail­able for use through­out your stylesheet. I pre­fer to use short­hand PHP for inline use to increase code read­abil­ity. (Since I con­trol the server on which the stylesheet resides, and the file is not intended for gen­eral dis­tri­b­u­tion, long­hand is optional). Your stylesheet may include state­ments like these:


body {
	background-color: <?= $mainBG ?>;
	font-size: <?= $fontSize ?>px;
	line-height: <?= $lineHeight ?>px;
	}
#content {
	width: <?= 33*$fontSize ?>px;
	margin: <?= 2*$lineHeight ?>px auto;
	border: 1px solid <?= $textColor ?>;
	padding: <?= $lineHeight-1 ?>px;
		//remove border width
	color: <?= $textColor ?>;
	background-color: <?= $mainBG ?>;
	}

Do you want to adjust the font size but main­tain typo­graphic bal­ance? Change one vari­able. Your client called and wants the text to be a lit­tle darker? Now your match­ing bor­der will not be for­got­ten in the update. Use of vari­ables in CSS is easy and worthwhile.

Jeffrey D. King

About the Author

Jef­frey D. King is a web designer and con­sul­tant in San Diego. Jeff is pas­sion­ate about the inter­sec­tion of busi­ness and the inter­net. As a stu­dent of design, usabil­ity, brand­ing, and entre­pre­neur­ial strate­gies, he can help your orga­ni­za­tion achieve online success.

Leave a Comment

Please note our comment and privacy policies.

(required)
(required)