Wired

Webmonkey

  • Web Dev & Design
    • Ajax
    • Backend
    • Blog Publishing
    • CSS
    • Databases
    • Fonts
    • Frameworks
    • HTML
    • HTML 5
    • JavaScript
    • Mobile
    • Multimedia
    • Programming
    • Security
    • UI/UX
    • Web Standards
  • Software & Apps
    • Browsers
    • Software
    • Web Apps
  • Platform & APIs
    • APIs
    • Identity
    • Location
    • Social
    • Web Services
  • Reference
    • Color Charts
    • HTML Cheat Sheets
    • Special Characters
    • Glossary
    • Templates
    • Code Snippets
  • twitter
  • facebook
  • RSS Feeds
Jul 1 2008
File Under: Programming, Visual Design, Web Basics

WebKit Experiments with CSS Variables

  • By Scott Loganbill

WebKit, the code that makes up the guts of browsers such as Apple’s Safari, has gained a much-requested new feature with the addition of variables in cascading stylesheets.

The feature, which won’t be available publicly until the next major release of Safari — and then only if it survives this experiment — is available now with its latest nightly WebKit build.

The enhancement comes after ten years of pleas for the feature by web developers. It would make designing and reading website code much easier by allowing developers to reference commonly-used styles by names chosen by the site’s designer. For example, watch how variables make this little piece of code, used to make the background of the page and tables light gray, a little more readable:

Without variables:


//Sets the background of the page and tables to the hex code for the color gray.

body {

   background-color:#ECEAE1;}

table {

   background-color: #ECEAE1;}

With CSS variables:


//Defines "DefaultBGColor" to light gray

@variables {

  DefaultBGColor: #ECEAE1;}

//Sets the background and any table on the page to the default background color

body {

  background-color: var(DefaultBGColor);}

table {

  background-color: var(DefaultBGColor);}

Current implementations of CSS have some standard variables for properties like alignment or color (”left,” “dark blue” or “red,” for example). However, unless you design your site around those pre-defined colors, you’re stuck having to memorize hex codes and pixel lengths. This is especially challenging for sites with multiple designers referencing the same code. Variables would mirror other coding standards by making these references configurable and recognizable according to site design.

Webkit, the rendering engine that powers Apple’s Safari and Linux’s Konqueror, is the first browser to support CSS variables. Web designers are not likely to take advantage of it until other major browsers, like Microsoft’s Internet Explorer and Mozilla’s Firefox, hop on the bandwagon. Still, it’s a nifty advancement, and something worth noting for the future.

To get started with Cascading Style Sheets (CSS), follow Webmonkey’s tutorial.

Tags: CSS, safari, Webkit
  • Post Comment  | 
  • Permalink
  • Digg
  • Stumble Upon
  • Facebook
  • Delicious
  • Reddit

Comments (0)

All fields required

Webmonkey's Picks

Browse Our Tutorials

HTML, JavaScript, design and more

Cheat Sheets

HTML, CSS and special characters

Color Charts

Brighten up your pages

Cut & Paste Code

Templates and snippets you can steal

Recent Comments

  • Scott L on Code Optimizers Can Make View Source Useless
  • Anica on A Brave New Web Will Be Here Soon, But Browsers Must Improve
  • MangoMen on Turn Your WordPress Blog into a Forum
  • James on Code Optimizers Can Make View Source Useless
  • Arieh on 5 Reasons Why You’d Want Internet Explorer on the iPhone
  • Recent Articles

  • Code Optimizers Can Make View Source Useless
  • Mozilla Labs Seeks to Tame Your Address Book With ‘Contacts’
  • Chrome Web Browser Adds Automatic Translation, Better Privacy Controls
  • Internet Explorer 9 Shows Up Faster, But Still Lacking
  • Opera on the iPhone? Yes, and Soon
  • Corrections | Sitemap | FAQ | Contact Us | Wired Staff | Advertising | Press Center | Subscription Services | Newsletter | RSS Feeds
    Condé Nast Web Sites:
    Webmonkey | Reddit | ArsTechnica | Details | Golf Digest | GQ | New Yorker

    Registration on or use of this site constitutes acceptance of our User Agreement (Revised 4/1/2009) and Privacy Policy (Revised 4/1/2009).

    Wired.com © 2010 Condé Nast Digital. All rights reserved.

    The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of Condé Nast Digital.