Quick Fix: Sass Mixins for CSS Keyframe Animations

CSS Keyframe animations are awesome! I love the power and flexibility that using them gives me, especially when compared to the relatively simple nature of CSS transitions. With this power comes a problem. Even simple animations require a HUGE amount of CSS to make them cross-browser compatible. A simple move on the page requires this code:

.object-to-animate {
  -webkit-animation: move-the-object .5s 1;
  -moz-animation:    move-the-object .5s 1;
  -o-animation:      move-the-object .5s 1;
  animation:         move-the-object .5s 1;
}

@-webkit-keyframes move-the-object {
  0%   { left: 100px; }
  100% { left: 200px; }
}
@-moz-keyframes move-the-object {
  0%   { left: 100px; }
  100% { left: 200px; }
}
@-o-keyframes move-the-object {
  0%   { left: 100px; }
  100% { left: 200px; }
}
@keyframes move-the-object {
  0%   { left: 100px; }
  100% { left: 200px; }
}

Copying and pasting is overly difficult, but what happens if you now want to change the animation to end at left: 300px? And what if you have 30 animations? The complexity of your animation suite increases quickly, but there’s an easy solution to this problem.

Keyframe Animation Sass Mixins to the Rescue!

As with all things CSS, I’m convinced Sass makes generating keyframe animation code so much easier. Two simple mixins makes your life much easier, and your code much cleaner and more maintainable.

@mixin animation($animate...) {
    $max: length($animate);
    $animations: '';

    @for $i from 1 through $max {
        $animations: #{$animations + nth($animate, $i)};

        @if $i < $max {
            $animations: #{$animations + ", "};
        }
    }
    -webkit-animation: $animations;
    -moz-animation:    $animations;
    -o-animation:      $animations;
    animation:         $animations;
}

@mixin keyframes($animationName) {
    @-webkit-keyframes #{$animationName} {
        @content;
    }
    @-moz-keyframes #{$animationName} {
        @content;
    }
    @-o-keyframes #{$animationName} {
        @content;
    }
    @keyframes #{$animationName} {
        @content;
    }
}

Using the mixins looks like this:

@include keyframes(move-the-object) {
  0%   { left: 100px; }
  100% { left: 200px; }
}

.object-to-animate {
  @include animation('move-the-object .5s 1', 'move-the-object-again .5s 1 .5s');
} 

The mixin allows for chaining of animations and as many keyframes as you'd care to use. It's a simple syntax that is very readable and maintainable for the long-term.

What other mixins are you using that make your life easier?

Multiple Background Images Using Sass and Media Queries

Multiple background images

I am trying to create a quick way to theme Kidblog.org’s class pages, and since the largest single asset of the whole page is the background image, I really wanted to use multiple background images to make sure that the closest possible image size is served to the user.

This most likely has zero use to you, but I thought it was a really interesting implementation of RESS multiple background images using media queries and Sass. I hope that if you need it, you use it; and if you don’t need it, maybe it will inspire you to employ some of Sass’s more advanced functionality. Read More…

Quick Fix: Sass Mixins for rem Values with px Fallback

Rems are awesome. They allow us to circumvent the complex cascading em mess that even great frond-end developers find ourselves in at times. The problem with them is figuring out the corresponding px value and remembering to put in the fallback. I’ve adapted a mixin by Karl Merkli to do just that. This beautiful little chunk of Sass will take rem value input in two different formats (with or without unit values) and output both the px and rem values you need. Read More…

My Sass and CSS Best Practices

Modularization, Nesting, Variables, Mixins, Specificity, Etc

In the week since I tweeted this, I’ve been asked about what I meant by it a half-dozen times. As with any dev tool, Sass’s power can be both a boost to productivity and a source of scorn because of misuse. I’ve even been told by a couple of people that they don’t allow their staff to use Sass/Less/CSS preprocessors because of the horrible unmaintainable CSS that it generates. Looking back at the Sass I wrote when I first started using it vs the Sass that I write now, I’m 100% certain that the problems these dev shops have lie 100% with the way the tools are being used, not the tools themselves.

Let’s take a look at some of the lessons I’ve learned and methods I use to write Sass that compiles to clean CSS.

  • Indentation vs. Nesting
  • Modularization
  • Variables
  • Mixins
  • Classes vs. IDs

Read More…

Forcing Internet Explorer 8 Out of Compatibility Mode (Quick Fix)

There is nothing worse than spending weeks designing and developing the perfect website, only to have the client send you a bug report with the screenshot clearly showing they are in compatibility mode. Floats don’t work right, transparent pngs are partially broken, and all that fancy CSS3 stuff that (sort-of) works in IE9/10? Yep. That’s broken too. So how do you fix it? Read More…

Responsive Layouts and Floats with *{ box-sizing: border-box; } (Quick Fix)

I’ve decided to start something new here. I am going to (attempt to) post one short and simple trick I’ve learned that I use every day in my code. I am going to call them, Quick Fix. I know… not really creative. Welcome to my life. What I am going to do is start with fixing a problem I think everyone has when they combine responsive web design and floats or inline-block. Read More…

My Iowa Code Camp Slide Deck

Rocking Responsive Web Development at ICC10

First off, I want to say thank you to everyone who attended my session, Rocking Responsive Web Development, today. It was the most interactive crowd I’ve ever had, and it was a pleasure being a part of such a great event. Here’s my slides from today:

Rocking Responsive Web Development (slides) – Up until the last few years, it was completely acceptable to design/develop a new website for one size of monitor. But with the release of the iPhone, iPad and other mobile devices, we’ve witnessed the beginning of a mobile revolution. By the end of this year, over 50% of Americans will use their phone or tablet as their primary web browsing device. The question is, are the sites that you are creating ready? In this talk, I discuss reasons why responsive is the best way to develop new websites, as well as the best techniques I’ve discovered after a year of full time development of responsive websites.

And this is the demo code from the presentation.

They are very much like the other decks from TCCC and An Evening of Web Development, but they’ve been updated with new information.

Always remember that I am here to help if you have any questions or run into any issues. Also, please leave any feedback from the sessions in the comments. I do this for you, and I want to know how I can make it better.

Read More…

My ‘An Evening of Web Development’ Sessions

Given at MJG International in Savage, MN
A shot of me giving my Rocking Responsive Web Development talk at an Evening of Web Development
Thank you to everyone who came out to see Marc and I last Thursday night. I met a lot of really great people and learned so much. I’ve included my slide decks from my presentations below. Please, give me your honest feedback in the comments. I’m already scheduled to give these talks again multiple times, so I need your advice to improve them.

Embrace Your Inner Designer (slides) – Whether you want to admit it or not, the decisions that you make as a developer greatly influence the final design of your application’s user interface. It doesn’t matter if you’re working with a designer, or you’re a one-man A-Team, there are some very easy and powerful things that you can do to ensure that the final product is intuitive, usable and (gasp!) beautiful. Come embrace that designer inside of you that wants to get out (and wear hipster glasses).

Rocking Responsive Web Development (slides) – Up until the last few years, it was completely acceptable to design/develop a new website for one size of monitor. But with the release of the iPhone, iPad and other mobile devices, we’ve witnessed the beginning of a mobile revolution. By the end of this year, over 50% of Americans will use their phone or tablet as their primary web browsing device. The question is, are the sites that you are creating ready? In this talk, I discuss reasons why responsive is the best way to develop new websites, as well as the best techniques I’ve discovered after a year of full time development of responsive websites.

And this is my responsive site demo I use in the presentation.

Read More…

Differences in jQuery and Media Queries Are Fixed With window.matchMedia()

Browsers rendering the page differently at the same resolution.
Recently, I discovered that the mobile menu solution that we use for our responsive designs (Matt Kersley’s Responsive Menu) is switching the menu to mobile earlier than anticipated in Firefox and IE10 (you can see the differences in the graphic above). After digging, I found it’s because the plugin is using the jQuery API’s .width() function to find $(window).width(). In Firefox and IE10, it follows the spec and includes the vertical scrollbar in that calculation. Webkit (Chrome and Safari) and IE8 don’t include the scrollbar width.The real problem is that no browser includes the scrollbar width when calculating width for use in media queries such as

@media only screen and (min-width:768px) {}

The repercussion of that is the JavaScript spec and the CSS spec calculate width differently. Yikes. Even worse, the different browsers calculate $(window).width() differently. Luckily there’s a solution for that very problem. Read More…