Symbolic CSS Preprocessor Mixins

By  on  

One of the major advantages to using CSS preprocessors is the mixin, a CSS function to dictate how styles are assigned.  They allow conditional logic and arguments, just like any other language, and are a massive help in unifying styles throughout a site.  They even make RTL a breeze.  I use mixins for another reason:  code importance and organization!

The scenario is as follows:  with the redesign of the web app I work on, I have to support two designs at once.  That means I always have the legacy CSS being loaded, and if the user has permissions to the redesign, I load the additional redesign CSS files.  Not ideal but time is an issue.  This provides a CSS organization issue:  some styles are new, some styles override legacy styles, and some are simply there to offset legacy styles.  If you use mixins correctly, you can easily organize these styles for ease in future maintainability.

The Stylus CSS

Instead of simply overriding styles or offsetting old ones, I use a mixin to do so which allows me to easily identify them in the future:

/* Use this in the case of having to override styles from old template */
compat-important(prop, value) {
  {prop}: value !important;
}

/* Used for style which are only present to override other styles -- these will be deleted once legacy design is gone */
compat-only(prop, value) {
  compat-important(prop, value);
}

The compat-important mixin tells myself and other developers that the style should be made !important for the sake of overriding the legacy stylesheet setting.  The compat-only mixin communicates that the style is only set to offset legacy styles. You'll note that one mixin calls the other -- they do the same job but the mixin name is symbolic!

Another advantage to using mixins for code organization is that you can modify the mixin to experiment with results.  For example, once I remove all of the legacy styles from my app, I can make `compat-only` return nothing (i.e. not set any style) and confirm that all of the styles are no longer needed.  So not only do these methods allow me to organize, they also allow me to experiment!

Do you use any symbolic mixins?  If so, please share!

Recent Features

  • By
    Send Text Messages with PHP

    Kids these days, I tell ya.  All they care about is the technology.  The video games.  The bottled water.  Oh, and the texting, always the texting.  Back in my day, all we had was...OK, I had all of these things too.  But I still don't get...

  • By
    Being a Dev Dad

    I get asked loads of questions every day but I'm always surprised that they're rarely questions about code or even tech -- many of the questions I get are more about non-dev stuff like what my office is like, what software I use, and oftentimes...

Incredible Demos

  • By
    MooTools-Like Element Creation in jQuery

    I really dislike jQuery's element creation syntax. It's basically the same as typing out HTML but within a JavaScript string...ugly! Luckily Basil Goldman has created a jQuery plugin that allows you to create elements using MooTools-like syntax. Standard jQuery Element Creation Looks exactly like writing out...

  • By
    9 Incredible CodePen Demos

    CodePen is a treasure trove of incredible demos harnessing the power of client side languages.   The client side is always limited by what browsers provide us but the creativity and cleverness of developers always pushes the boundaries of what we think the front end can do.  Thanks to CSS...

Discussion

  1. Never thought of this concept in CSS preprocessors before, but I’m actually thinking of a way to apply this concept just with a slightly different application. I need to decentralize a massive mashed together stylesheet that holds combined styling for pages where it never should have. I can use a similar concept to give symbolic meaning to the styling of each page specifically and then disable the styling for pages I don’t need and see if it won’t break.

    Thanks for the insight, David!

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!