December 20, 2013

Pure CSS Loading Spinner

How often do you see the same loading graphic, using the same animated .gif file? I know I see it multiple times every day, but why use such a common poorly rendered graphic when we could create one more visually pleasing just as easily in pure css?

If you do not know which loading graphic I am specifically referring to, let me refresh your memory:

Spinner

Now we could directly recreate this graphic (I have included the code at the bottom of the post) but for this brief example let’s rework this graphic a bit.

HTML:

CSS: $size: 0.5em; $color: #c22;

.loading{ box-sizing: border-box; position: relative; display: inline-block; padding: $size; vertical-align: middle; text-align: center; background-color: transparent; border: 5px solid transparent; border-top-color: $color; border-bottom-color: $color; border-radius: 50%; }

.outer{ animation: spin 1s infinite; }

.inner{ animation: spin 1s infinite; }

@keyframes spin{ 0%: { transform: rotateZ(0deg); } 100%: { transform: rotateZ(360deg); } }

/* styling */ html { width: 100%; height: 100%; background: radial-gradient(circle, #fff 0%, #aaa 100%) no-repeat; overflow-x: hidden; overflow-y: hidden; } body { text-align: center; display: table; width: 100%; height: 100%; overflow-x: hidden; overflow-y: hidden; } #wrap { box-sizing: border-box; display: table-cell; vertical-align: middle; }

See the Pen Simple CSS Loading Spinner by Kevin Carpenter (@graybox-kevin) on CodePen

Rerun

So there we have it, a simple easily customizable smooth, and lightweight loading spinner built using only TWO elements. I don’t think I need to do much explaining of the basic HTML and CSS that’s being used other than you should note that this doesn’t use vanilla CSS but rather SCSS so that we can use variables.

Also, as promised here is the basic redone spinner if you wish to go with the older look:

See the Pen mEHoB by Kevin Carpenter (@graybox-kevin) on CodePen

Please Note – Since we are using CSS3 animations to display these they will not be animated in IE9 and below. However, they will still appear but static instead of animated which isn’t a huge issue. Users on older browsers will just be missing out on yet another feature, so as long as nothing related to functionality breaks this isn’t really a problem.

Paul Weinert
Author

Paul Weinert is our founder, managing principal and spirit animal. He’s one of those odd people you meet that was born in Portland, grew up here and still lives here for some reason. If you had two words to describe Paul after meeting him, we’d bet you’d use “charmingly quirky”.

Paul started GRAYBOX in February 2009, and for the first four years, GRAYBOX had a blend of a contract staffing firm and a traditional agency. GRAYBOX then hired our first employees in 2012 and has been growing ever since. Professionally, Paul has a background in internet business consulting, working especially with mid-market ecommerce, healthcare, and tech companies. He worked as a user experience designer, web developer and internet marketer. Nowadays, Paul serves as our company visionary, team mentor and head of executive leadership — Paul is extremely proud of the entire team and the company. He’s constantly humbled by the team’s awesomeness and the caring community that’s sprung up from his initial ideas.

Beyond GRAYBOX, Paul is married, has two young boys and lives in NE Portland. He’s also an active foster parent, mentors other local entrepreneurs, and volunteers on local non-profit boards. Businesswise, aside from GRAYBOX, Paul has started a few other businesses and is an active investor in the local startup scene.

For the sheer joy of it, let’s play 2 truths and a lie:

  1. Paul firmly believes that any activity would be better if on a boat.
  2. Other than this whole Internet thing, Paul has basically no other skills or expertise in anything.
  3. Paul is rare unicorn, actually born and raised in Portland.