the Future of the Web
  • Home
  • Articles
  • Contact
  • Using Animated GIFs with CSS

    Dec 23 2006

    Now that plain old HTML, CSS and JavaScript have made the web quite sophisticated, Flash isn't really as necessary as it once was. Nonetheless, I still see Flash being used to make things, well, flashy. Flash is often used just to add a touch of animation, like things moving around when the user moves their mouse, and other simple effects.

    Well many, if not all, of these kinds of effects can be achieved with plain old CSS and JavaScript, thanks to animated GIFs. Yes, those awful animated GIFs. Just like JavaScript was until recently, animated GIFs are associated with the ugly web of the past (think dancing hamsters). However, if used carefully, animated GIFs can be used to make a web site very lively and feel more interactive in the same way Flash is used.

    To demonstrate this, I've put together probably the most boring proof-of-concept ever. You'll have to use your imagination to see how this technique can be applied in much more creative and better-looking ways.

    First, I created a simple animated GIF of a white bar moving left to right over a transparent background. I actually used Flash to create this. In fact, you could take a Flash interface, and publish the different pieces to animated GIFs in order to eliminate the need to use Flash. Anyway, here is my boring GIF on a black background, so that you can see what's happening:

    Now, I'm going to use this together with CSS to create an animating hover effect. Here is the CSS and HTML I will use:

    <style type="text/css">
    /* IE 6 needs a:hover to be defined */
    a:hover { width: auto } 
    
    a.shine:hover span {
        /* don't take up space, and appear above the text */
        position: absolute;
        display: block;
    
        /* set the background to the animated GIF */
        background: url(shine.gif);
    
        /* the width and height of the animated GIF */
        width: 230px;
        height: 50px;
    }
    </style>
    
    <a href="/blog/cat/javascript" class="shine">
        <span></span>
        JavaScript
    </a>
    

    I'm using an empty span inside the link to act as a placeholder for the animated GIF. When the mouse is hovering over the link, the span comes to life by expanding to an animated GIF which appears above the text. Since the background of the GIF is transparent, and the background of the text is white, the effect is that of a "shine" washing over the text.

    Click here to see the code in action.

    And there you have it. Yes, I know, my demo is boring. But you have to use your imagination to think of all the amazing things you can do by combining animated GIFs with CSS and even JavaScript effects. You can do something as simple as an animated roll-over image, or as complex as an interactive game. The possibilities are only limited by your ability to design them.

    Tags: css gifs animation flash
    View 6 Comments | Add a comment
  • Comments

    1. Kel at 12:46am on December 24, 2006

    Neat - reminds me of when I used to used an animated gif as a fake "loader" in the lowsrc to make it look like something was actually happening while the actual graphic got loaded.

    Of course till we see something like the MNG (animated PNG) file format take off, you'll never have any sort of decent transparancy. At it's best dithering, Gif89a still looks clunky 'cuz of the 1 bit transparency.

    2. stephen at 7:40am on December 29, 2006

    I like it ... because it has potential. In conjunction with the ALA sprite method, this could have some scary results.
    Thanks for sharing.

    3. Manoj at 2:00pm on April 16, 2008

    Cool!!

    4. Tom at 5:29am on November 7, 2009

    Thanks for the idea, will test this out.

    5. Joe Taylor at 10:21pm on December 12, 2009

    Nice one! Great starting point for zillions of uses. Not that I'm looking to see animated .gifs all over web pages again....

    6. Goldie at 10:35am on February 3, 2010

    This seems like an interesting way of creating the effect. Thanks.

    Add a Comment

    Note: HTML tags and entities will be converted so that they are displayed as you type them. This means if you type in <em>, people will see <em>, and if you type &lt;em&gt;, people will see &lt;em&gt;.

  • Request a Quote

  • Jesse Skinner

    Jesse Skinner
    • About Me
    • Email Me
    • RSS Feed RSS Icon
    • @JesseSkinner
  • Recent Articles

    • jQuery Live Events
    • I need web developers
    • buttons need type="submit" to submit in IE
    • Win $200 in a Web Dev Writing Contest
    • Use Arrays in HTML Form Variables
    • 5 Reasons Freelancers Can Succeed in a Shrinking Economy
    • Keeping a Live Eye on Logs
    • Using PHP's empty() Instead of isset() and count()
    • Testing Web Pages with Lynx
    • Stop CSS Background Flickering in Internet Explorer 6
    • See All...
  • Categories

    • javascript (39)
    • links (21)
    • about (19)
    • web (14)
    • html (12)
    • server (11)
    • css (8)
    • browsers (8)
    • carnival (7)
    • work (6)
    • design (4)
    • seo (4)
    • ads (4)
    • standards (4)
    • events (4)
  • Older Articles

    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • September 2007
    • August 2007
    • July 2007
    • June 2007
    • May 2007
    • April 2007
    • March 2007
    • February 2007
    • January 2007
    • December 2006
    • November 2006
    • October 2006
    • September 2006
    • August 2006
    • July 2006
    • June 2006
    • May 2006
    • April 2006
    • March 2006
    • February 2006
    • January 2006
    • December 2005
    • November 2005
    • October 2005
    • September 2005
    • August 2005
    • April 2005
    • See All...
Copyright © 2010 The Future of the Web