DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Languages
  4. A Vote for CSS Variables

A Vote for CSS Variables

Ian Muir user avatar by
Ian Muir
·
Mar. 21, 12 · Interview
Like (0)
Save
Tweet
Share
3.35K Views

Join the DZone community and get the full member experience.

Join For Free

The idea of having variables in CSS has been something that developers and designers have argued about since the very beginning of CSS. Like the PC vs Mac debate, there are many passionate folks both for and against variables and both groups have some valid opinions and concerns. This debate was re-ignited with the recent release of a draft of the new CSS variables module.

Fortunately, as developers and designers we have the opportunity to influence the people who write the specifications. This draft is an early stage document that will undergo a variety of revisions before it can even be considered for spechood. For this reason, I feel it’s important to voice my opinion.

As a developer who works on everything from small wordpress sites to large enterprise apps, I am in favor of CSS variables. While I believe the implementation outlined in the spec could use some polishing, the core concept is a great boon for those of us managing large, complicated stylesheets. If this becomes part of the spec, it will be a large step toward making CSS slimmer, easier to maintain and adhere better to DRY coding practices. Simply put, for those of us that understand CSS, this will enable us to write better CSS and as a result build better websites.

While there are many that dislike the idea of CSS variables, it seems that the single largest complaint is that it would make things more complicated and harder to learn. Personally, I don’t think this is accurate for several reasons:

  1. CSS variables will allow developers to simplify their CSS immensely and write less code. It has been my experience that simple code built on complex concepts is no harder to learn than overly verbose code that might be simpler conceptually.
  2. One of the greatest benefits of CSS is that properties cascade in a predictable manner. It’s so important that it got its own letter in the name. Variables allow this concept to be applied in a great variety of situations.
  3. Above all else, it’s optional. If this becomes a spec, there is no reason that everybody must use it. If you’re happy without variables, keep on being happy without them. Nobody is taking away anything from the language, just adding a new tool for those that need it.

Now that I’ve clearly stated my position, I would like to point out what I believe to be a flaw in the current draft. The data prefix seems unnecessary. Having a single character, such as $ or @ or even § would be better. Ok, maybe not §, but many languages and libraries have established that a single character to denote variables works well. Not only does a single character result in less typing, it also makes it easier to quickly identify variables when scanning through a stylesheet. To me @primary-background stands out more than data-primary-background when viewed along with hundreds of other properties.

This draft does have a few flaws, but overall it is a step in the right direction. It can provide a powerful new tool for those of us that manage hundreds and possibly thousands of lines of CSS and I see no reason why adding a new feature would make any of the existing features harder to learn. The fact that tools like LESS have caught on shows that there is a real need for features like this. When we choose to block potentially powerful new options simply because it is difficult to learn, we cheat those that might use it to build something amazing. We overcame the IE6 box-model and crafted amazing layouts using only floats, there is no reason why we can’t learn how leverage this new functionality to our benefit.

What do you think? Is this new module a great new tool or a disaster waiting to happen?

CSS

Published at DZone with permission of Ian Muir. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 19 Most Common OpenSSL Commands for 2023
  • Using Swagger for Creating a PingFederate Admin API Java Wrapper
  • Keep Your Application Secrets Secret
  • The Power of Docker Images: A Comprehensive Guide to Building From Scratch

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: