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
    CSS Gradients

    With CSS border-radius, I showed you how CSS can bridge the gap between design and development by adding rounded corners to elements.  CSS gradients are another step in that direction.  Now that CSS gradients are supported in Internet Explorer 8+, Firefox, Safari, and Chrome...

  • By
    Interview with a Pornhub Web Developer

    Regardless of your stance on pornography, it would be impossible to deny the massive impact the adult website industry has had on pushing the web forward. From pushing the browser's video limits to pushing ads through WebSocket so ad blockers don't detect them, you have...

Incredible Demos

  • By
    Image Reflection with jQuery and MooTools

    One subtle detail that can make a big difference on any web design is the use of image reflections. Using them too often can become obnoxious but using reflections on large, "masthead" images is a classy enhancement. Unfortunately creating image reflections within your...

  • By
    iPhone Click Effect Using MooTools or jQuery

    One thing I love about love about Safari on the iPhone is that Safari provides a darkened background effect when you click a link. It's the most subtle of details but just enforces than an action is taking place. So why not implement that...

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!