• Home
  • About Mitch
  • Speaking
  • Articles
  • Contact
  • Home
  • About Mitch
  • Speaking
  • Articles
  • Contact

Digital Strategist

WordPress Developer

Content Creator

Unapologetic Punk

Mitch Canter

  • X
  • Bluesky
  • GitHub
  • Twitch
  • YouTube
  • LinkedIn
Tutorial

CSS Transitions – A Little Dab'll Do You

CMDR Mitchcraft

Reading time: 3 minutes

CSS Transitions, when used properly, are a great way to provide a bit of eye candy to your site.  Providing movement and motion, either upon scroll or reveal of content, is a great way to subtly draw a viewer’s attention.  It’s also a way, unfortunately, to annoy the ever-loving crap out of your readers.  When CSS Transitions were first revealed, people worried that they would be used like Flash and… completely junk up the web.

BUT, when used sparingly, CSS Transitions add that last little spark to a site that really makes the difference between a good design and a great design.

Using CSS Transitions

CSS Transitions are actually extremely easy to use.  As the name suggests, everything is done via CSS, so there’s no jQuery or any other languages to know.  The only things you need to be aware of are syntax and flow of the transition.

We’ll start with an extremely simple example: using transitions to create a button that “glows” when you hover over it.

JSFiddle of Final Button

The Initial CSS

We’re going to create a simple button for the base of our CSS transition.  To do that, we’ll have a bit of CSS:

a.demobutton{
    background: black;
    display: inline-block;
    border-radius: 12px;
    color: white;
    padding: 12px;
}

JSFiddle of Typical Button

This is a very basic button and, to be honest, the style itself doesn’t matter as much as the CSS transitions we’re adding to it.  Next, we want to set the “hover color” of the link.

a.demobutton:hover{
    background: #09f;
}

JSFiddle of Button with Hover

Once you hover over the button, the background color will turn a nice blue color. But, as you hover over it, it’ll turn blue instantly – we want to smooth that out a bit.

The Additional CSS

a.demobutton{
    background: black;
    display: inline-block;
    border-radius: 12px;
    color: white;
    padding: 12px;
    -webkit-transition: .25s all ease-in-out;
    transition: .25s all ease-in-out;
}

JSFiddle of Final Button

Let’s break this down:

The first thing you’ll realize is that we’ve added two lines of code to the button that say, basically, the same thing – the difference is the beginning piece of the code.  This “vendor prefix” is a spec of CSS that tells certain browsers to behave in certain ways.  -webkit-, for example, is the Safari / Chrome vendor prefix.  There’s also -moz-, -o-, and -ms- (which are Mozilla/Firefox, Opera, and Microsoft/IE respectively), but in this case, the CSS transitions for those will work with the standard code (in all honesty, the -webkit- is unnecessary, but I put it in to have a reason to talk about vendor prefixes).

Next, a string of values (width, 2s, ease-in-out).  The first value is the time limit.  This goes in seconds (in this case, two seconds), but can be divided into fractions of a second (0.25s).

The second value of the CSS transition tag is the CSS attribute that we will be modifying.  We can either use a specific value (width, height, display, border, etc) or we can catch all of the attributes we define with the “all” tag.

The final value is the “speed curve”, known in the spec as the “timing-function”.  This can be defined as a “graphical description” of what the speed does.  If you want to see the various timing functions demonstrated, head over to The Art of Web and scroll down nearly half-way.  The balloons all “rise” according to the various timing functions.  For me, “ease-in-out” seems to be my favorite, as it’s a nice balance between a default speed and having it be “zippier” toward the middle of the animation.  Think of it like a car starting, driving, and then stopping – you accelerate to speed, and brake to stop, but it’s not instant.  That’s ease-in-out in a nutshell.

Notice that we’ve added the code to the initial CSS, and not the hover CSS.  That’s because we want the browser to register the transition before we hover the effects we want to apply.

And that’s it – it’s really just that simple.  Only a few lines of code, but CSS transitions add a lot of character to your styles with just a touch of motion – the perfect complement to any modern design.

What’s your favorite use of CSS Animations?  And, conversely, have you seen any websites that just take it too far?

 

000000, 09F, 999999
  • Know Your Role: The Ultimate Guide to WordPress User Roles

    Know Your Role: The Ultimate Guide to WordPress User Roles

    Reading time: 3 minutes

    WordPress user roles play a critical role in managing access and permissions on a WordPress site. By assigning specific roles to a user, site owners can control what actions each user can perform on the site. WordPress User Roles There are six main roles included with any WordPress installation: Creating a User in WordPress Creating…

    WordPress
  • 50 Days to a Better WordPress Blog–Day 1: Font Readability

    50 Days to a Better WordPress Blog–Day 1: Font Readability

    Reading time: 4 minutes

    When it comes to typography online, there are a few simple ideas you can remember to help your users read your articles time and time again and always be begging for more, without them having to get new glasses every time.

    WordPress