A Flex Box Playground

For the past several months I’ve been trying to learn more about the CSS flex box module. If you’re like me you learn from reading and then doing, so to help out I built a playground where you can learn about and test out the basics of the flex box module. The playground gives you all sorts of useful information about the various properties of flex box and an area to test basic layouts.


The is the first release (I’ll probably post this on GitHub) and I’m open to any suggestions on making this a better learning resource.

You can access the playground at http://flexboxplayground.catchmyfame.com/

CSS3 Transitions, Transforms, and Animations

(…or why CSS3’s transition, transform, and translate always confuse me)

Maybe it’s me and maybe it’s because I don’t use them all the time, or maybe it’s because they kind of sound alike, but CSS transitions, transforms, and translations tend confuse me, especially since I don’t use them daily. And they all start with “trans”. And in case you were wondering which is used more often, Google shows nearly 100% more results for transitions as for transforms.

So how are transitions and transforms different? In a nutshell:

  • A CSS Transition allows property (e.g. opacity, width, color) changes to occur between two states over a specified duration.
  • A CSS Transform allows elements to be transformed (e.g. scale, rotate, translate, and skew) in 2D or 3D space.


CSS’s transitions are a great, easy way to change an element’s property (e.g. width or height) instantly, or over a period of time. This can be done by adding or removing a class from an element, or by using pseudo-classes like :hover. They can be used with or without JavaScript to create tons of effects.

Like background, margin, and padding, the CSS transition property is actually shorthand for four other properties: transition-property, transition-duration, transition-timing-function, and transition-delay.

  • transition-property
    transition-property: background;
  • transition-duration
    transition-duration: 0.3s;
  • transition-timing-function (ease, linear, ease-in, ease-out, ease-in-out, step-start, step-end, steps(<integer>[, [ start, end ] ]?), and cubic-bezier(<number>, <number>, <number>, <number>)). You may specify multiple timing functions; each one will be applied to the corresponding property as specified by the transition-property property, which acts as a master list. If there are fewer functions specified than in the master list, missing values are set to the initial value (ease). If there are more timing functions, the list is simply truncated to the right size. In both case the CSS declaration stays valid.:
    transition-timing-function: ease;
  • transition-delay
    transition-delay: 0.5s;


  • transition (no delay)
    transition: background 0.3s ease;
  • transition (with .5 second delay)
    Ex: transition: background 0.3s ease .5s;
  • Multiple transitions can be combined and separated with a comma:
    transition: background 0.3s ease .5s, color .2s linear;
  • The keyword all can be used to transition all properties without having to delineate them.
    transition: all 0.3s ease;

Transitions are usually placed on the normal state of an element, and the change, triggered by a hover, click etc., invokes the change.

For example, on an image we might apply:

 background: red;
 transition: background 1s ease 0.5s;
div:hover {

Example: http://jsfiddle.net/9tmL826b/ (hover over the square)

We typically place transitions on the normal state of the element instead of on the rule that triggers the change (e.g. the hover state) which may seem like a more intuitive thing to do. But why?

While you can place transitions on the changed state of an element (:hover, :active, etc.), placing them on the normal state allows you to save code, without having to rewrite the rules for each separate state/trigger, and it also allows the transition to occur in both directions.

For example, if we moved the transition rule from the above example from the div selector to the div:hover selector like:

div:hover {
 transition: background 1s ease 0.5s;

We’d get the animated color change when hovering over the div, but when hovering out, the change would be abrupt, since the hover state essentially doesn’t exist, and neither does the transition rule. The hover state is binary, it either exists or it doesn’t, and by placing the transition rule on the hover state, it ceases to exist when the hover state doesn’t exist.

As Dan Cederholm points out, “…you may want the transition to also happen on the :focus or :active pseudo-classes of the link as well. Instead of having to add the transition property stack to each of those declarations, the transition instructions are attached to the normal state and therefore declared only once.”

Remember that since not all browsers have implemented the formal transition rules that you may need to use browser-specific prefixes (-webkit-, -moz-, -ms-, -o-) when building your rules.

Here’s a handy list of all the CSS properties that can be transitioned (animated): http://www.w3.org/TR/css3-transitions/#properties-from-css-

Transforms (2D)

Transforms (as opposed to transitions) allow you to manipulate elements in 2D/3D space.

When basing transforms on the state of an element (e.g. :hover, :active, :focus, etc.), unlike transitions, transforms get added to the changed state of an element.

You can use transform and transitions together. For example, you could create a transform that scales an element by 150%, and then couple that with a transition that will animate it. To use a transition with a transform, you just specify the transform as the property that you want to transition:

transition: transform 0.2s ease-in-out;

Common Transforms

Scale – Default scaling is from the center. Use transform-origin to change the center point.

transform: scale(1.5);

Translate – Moves an element from its normal position. Shorthand for translateX(x) and translateY(y)

transform: translateX(150px); /* or translate(150px, 200px)*/

Skew – Skews an element from its normal position. Shorthand for skewX(x) and skewY(y)

transform: skewX(150pxdeg); /* or skew(40deg, 20deg)*/

Matrix – Specifies a 2D transformation matrix comprised of the specified six values. To be honest, I have no experience using this, and have rarely seen it used. That’s not to say it’s not useful and could do some wonderful and amazing things. It’s just that I have yet to notice any. For a nice explanation, see http://stackoverflow.com/questions/21469502/how-do-i-use-the-matrix-transform-and-other-transform-css-properties

Combining Transforms

Multiple transforms can be combined into one statement by separating each with a space.

transform: scale(1.5) rotate(-10deg);

Example: http://jsfiddle.net/j08691/ex817n3s/

And here’s a handy list of all the 2D and 3D CSS transform functions: http://www.w3.org/TR/css3-transforms/#two-d-transform-functions

Lost in Translation

For an example of why you might want to use translate over the top/right/bottom/left position rules see this article by Paul Irish. Transition, transform, and translate – easy to use but tough to separate in your head.

3D Transforms!

3D CSS transforms are quite similar to, and in some cases the same as, 2D transforms.

See http://www.w3.org/TR/css3-transforms/#three-d-transform-functions for the current list of 3D transforms.

In the browser’s 3D space, the x-axis runs horizontally, the y-axis runs vertically, and the z-axis runs perpendicular to the screen (with positive values being closer to the viewer and negative values being farther away).

I’m not going to go into the topic of 3D transforms since it’s already been done quite well by http://desandro.github.io/3dtransforms/docs/introduction.html and http://www.smashingmagazine.com/2012/01/06/adventures-in-the-third-dimension-css-3-d-transforms/ and it could warrant a small book on its own, however here’s one simple example that uses several of the techniques I’ve outlined above: http://jsfiddle.net/j08691/uUY9h/2/


You might think that what we’ve discussed so far is already a type of animation, and you’d be right. But they’re very basic, and you can create animations more advanced than what CSS transitions provide by using CSS animations.

To use animations you’ll need to get familiar with CSS keyframes. I’ll only touch on CSS animations for now to give you an idea of how they can be used, and go into more depth in the future.

CSS keyframes can be thought of as a specialized @-rule that wraps a set of property changes (at least two) over a duration. Note that like most of the newer CSS technology you’ll usually need to use vendor-specific prefixes (e.g. @-webkit-keyframes, @-moz-keyframes) in addition to the hopefully future standard @keyframes.


@-webkit-keyframes throb {
 0% {
 -webkit-box-shadow: 0 0 0 50px 50px rgba(50, 160, 50, 0.2);
 50% {
 -webkit-box-shadow: 0 0 50px 50px rgba(50, 160, 50, 0.9);
 100% {
 -webkit-box-shadow: 0 0 50px 50px rgba(50, 160, 50, 0.2);

This creates the “throb” rule with three keyframes in the animation. here we increase and decrease the opacity of a div’s box-shadow property. 0% (a.k.a “from”) is the start of the animation and 100% (a.k.a “to”) is the end. By itself this rule won’t do anything. To actually animate the element, we need a separate animation (-webkit-, -moz-, …) rule.

The second part of an animation references the keyframe we created by name (e.g. “throb”), sets the duration for the keyframe to run, and the number of times to execute it, with an optional easing effect.

div {
 -webkit-animation: throb 1.5s infinite ease-in-out;
 -moz-animation: throb 1.5s infinite ease-in-out;
 animation: throb 1.5s infinite ease-in-out;

This is the shorthand notation for animation. The same rule could’ve been written as:

div {
 -webkit-animation-name: throb;
 -webkit-animation-duration: 1.5s;
 -webkit-animation-iteration-count: infinite;
 -webkit-animation-timing-function: ease-in-out;

Examples: http://jsfiddle.net/j08691/bya759zq/ and http://jsfiddle.net/j08691/6u2ax1nq/4/

This is just the tip of the animation iceberg. For more information, check out https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_animations

As you can see, CSS Transitions, transforms, and animations are a lightweight and easy to use alternative to other HTML element manipulation tools.

A web developer's journal