2.6: Using custom variables in your CSS

Custom variables in CSS is a special feature built in to 23 Video. Using this feature, developers can turn simple designs into customizable themes where ordinary users can modify logos, colours, fonts, sizes a much more in a structured fashion.

Custom variables in CSS

The purpose of custom variables is best described with an example: When styling a 23 Video site (or any web site in fact) designers will usually hard-code colors, fonts, graphics etc. directly in to the stylesheet:

body {background-color:#a1ca9c; font-family:Helvetica,Arial,sans-serif;}
a {color:#214b1c;}

This works well when a theme is used only once; but whenever you want a design to be reused, you will want users to change the colors and font without writing CSS themselves. For example by selecting a color with a color picker and a font from a list. This is the purpose of custom variables.

The syntax of a variable declaration is similar to standard CSS, but includes % around variable names:

%primary-color% {
  type: color;
  name: 'Main color';
  description: 'Select the color that best matches your brand';
  default: #439639;
}
%font% {
  type: font;
  name: 'Font';
  default: Helvetica,Arial,sans-serif;
}

The same natation with % is used to insert the value of a variable:

body {background-color:%primary-color+50%; font-family:%font%;}
a {color:%primary-color-50%;}

This creates a new customization options under DesignCustomize:

And finally, it creates a dynamic piece of CSS which is used whenever the 23 Video site is loaded:

body {background-color:#87abce; font-family:"Comic Sans MS", cursive;}
a {color:#82b4f;}

In this example, we're letting the users select a color and a font − but the same approach is used to let users upload their own images files, select from predefined lists or input their own free-form text.


Types of variables

Every variable has a type which controls how it is configured and used in the CSS.

TypeDecriptionRequired properties
color

Let the user select a color width a color picker − or input a RGB or hex color.

Example:
%background-color% {
  name: 'Site background color';
  type: color;
  default: #fafafa;
}
body {background-color:%background-color%;}

Result:
graphic

Let the user upload an image file which is then rescaled, converted and made available as a ressource file.

Example:
%logo-image% {
  name: 'Logo';
  type: graphic;
  filename: logo.png;
  min-width: 30px;
  max-width: 200px;              
}
a#logo {background-image:%logo-image%;}
/* or the equivalent...*/
a#logo {background-image:url('/files/logo.png');}

Result:
filename
font

Let the user select a font to use.

Example:
%font% {
  name: 'Site font';
  type: font;
  default: Helvetica, Arial, sans-serif;
}
body {font-family:%font%;}

Result:
option

Let the user select from a list of specified options.

Example:
%footer-text-align% {
  name: 'Text alignment in footer';
  type: option;
  default: left;
  options: left, right, center;
}
#footer {text-align:%footer-text-align%;}

Result:
options
text

Let the user input a piece of text.

Example:
%import-stylesheet% {
  name: 'External stylesheet';
  description: "Full URL to external stylesheet";              
  type: text;
}
%paragraph-margin% {
  type: text;
  name: 'Margin on paragraphs';
  description: "Set the margin with pixel, i.e. '6px'.";
  default: 10px; 
}
@import url('%import-stylesheet%');
p {margin:%paragraph-margin% 0px;}

Result:

Variable properties

Each variable is set up with a number of properties controlling how a variable behaves and how it is configured. For example, any variable can have a name, a description and a default value − and developers can specify its type, i.e. if it's a graphic file or a color.

PropertyDecriptionNotes
name

The name displayed for the variable with a user customizes the theme through a form.

Example:
name: 'Background image;
Defaults to name of the variable
type

The type of the variable. Valid values are text, graphic, color, font, and option. See previous section for details.

Example:
type: graphic;
Defaults to text
description

The help text displayed when a user customizes the theme.

Example:
description: "Upload an image files to use a the background for the site";
Not required
default The default value of the variable when the user hasn't set any specific value.

Example:
default: purple;
Defaults to empty string
filename The filename and file type of the uploaded graphic file. For example, if you set filename to logo.png, the uploaded file is converted to a PNG file and is available as a ressource file at /files/logo.png.

Example:
filename: logo.png;
Required with type graphic
options

The list of options avaiable for the user to choose from.

Example:
options: 10px, 11px, 12px, 14px, 16px 20px;
Required with type option
min-width

When the users uploads a graphic file, the image is rescaled and converted. min-width describes the minimum width of the file after it has been converted − if users upload smaller images, they will be scaled up.

Note that this property is handled on the server; not in the browser. This means that you are not limited by legacy browsers such as Internet Explorer 6 and 7 that do not support min-width natively in standard CSS.

Example:
min-width:30px;
Used with type graphic
min-height

The minimum height of a uploaded and converted graphic file. See min-width for further details.

Example:
min-height:30px;
Used with type graphic
max-width

The maximum width of a uploaded and converted graphic file. See min-width for further details.

Example:
max-width: 130px;
Used with type graphic
max-height

The maximum height of a uploaded and converted graphic file. See min-width for further details.

Example:
max-height: 80px;
Used with type graphic

Including colour variation

You might have noticed how the first example included a strange notation for the page's background color, %primary-color+50%. This statement lets you use different shades of the same color:

%my-color% {
  type: color;
  name: 'A color';
  default: red;
}
a {color: %my-color+12%;}   /* the chosen color blended with 12% white */
a {color: %my-color+50%;}   /* the chosen color blended with 50% white */
a {color: %my-color-20%;}   /* the chosen color blended with 20% black */
a {color: %my-color-100%;}  /* 100% black */
a {color: %my-color+100%;}  /* 100% white */

You can use any integer value from -100 to +100 and the shading will look like this:

Comments

  1. David
    Dude, that's awesome! So much better than style.css.php
    Any idea what this does to speed/performance? Obviously this gives options for ever variable, but if making 3-5 different styles is practical, is there much difference?

    Any other thoughts about the pros/cons using this method? Thanks! (PS I'd love to see a subscribe to comments button!)

    » David
    April 12, 2011
  2. prada handbags
    exhibition will probably be prada handbags and shows no billion dollars news.yahoo critical and you should not plunge into a sites replica rolex are not traditional, nevertheless of poor quality among canon Tag Heuer, Hublot, rolex,omega,breitling,panerai,tag our kasperskymovement. dealers Ipad their competitors. That is in reality cool!Shopping on collection is primarily a superb method to conserve an enormous amount of cash as well as permits you to replica watches uk other customization options on the dial with color and other materials. Currently Blaken customizes the Submariner, Daytona, Explorer, Deepsea, rolex replica replica watch, link lady, cameron diaz, more? Simple. A watch is something you slap on your wrist that is super sexy and it tells the time. The Lady Link Tag Heuer hermes handbags
    May 28, 2012
  3. Nike Requin
  4. دردشة بنات مصريه

Add a comment...

Name
E-mail
Web
 
Connect with Faceboook
Content