Treehouse

WebKit Marquee CSS: Bringin’ Sexy Back

By on  

We all joke about the days of Web yesteryear.  You remember them:  stupid animated GIFs (flames and "coming soon" images, most notably), lame counters, guestbooks, applets, etc.  Another "feature" we thought we had gotten rid of was the marquee.  The marquee was a rudimentary, javascript-like effect to move text from one side of a block to another.  I was recently looking at WebKit's CSS specs and found that Safari has implemented CSS marquees.

The CSS Format

.marquee {  
	overflow-x: -webkit-marquee;
	-webkit-marquee-direction: ahead|auto|backwards|down|forwards|inherit|left|reverse|right|up;
	-webkit-marquee-increment: small|medium|large;
	-webkit-marquee-repetition: {number};
	-webkit-marquee-speed: slow|normal|fast;
	-webkit-marquee-style: alternate|inherit|none|scroll|slide;
	font-size:1.4em;
}

There are five pieces to the marquee puzzle:

  • overflow-x:  Should be set to -webkit-marquee
  • -webkit-marquee-direction:  The direction of the marquee.
  • -webkit-marquee-increment:  The size by which to move.
  • -webkit-marquee-repetition:  The  number of times the marquee should repeat.
  • -webkit-marquee-speed:  The speed at which to move the ext.
  • -webkit-marquee-style:  The effect which the marquee should function.

CSS Marquee Usage

.marquee {  
	overflow-x: -webkit-marquee;
	-webkit-marquee-direction: backwards;
	-webkit-marquee-style: slide;
	-webkit-marquee-speed: fast;
	-webkit-marquee-increment: large;
	-webkit-marquee-repetition: 6;
	font-size:1.4em;
}

The example uses each of the marquee properties given above and should be straight forward.  If you choose to use WebKit's marquee, experiment with the settings to make sure the marquee does what you want.  When it doesn't, fix it with JavaScript...or question why you're using a marquee at all.

No word yet on when the WebKit team plans to implement a CSS counter.  I'm sure I'll do a tutorial on it when the time comes.

ydkjs-1.png

Recent Features

  • Create a CSS Cube

    CSS cubes really showcase what CSS has become over the years, evolving from simple color and dimension directives to a language capable of creating deep, creative visuals.  Add animation and you've got something really neat.  Unfortunately each CSS cube tutorial I've read is a bit...

  • Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

Incredible Demos

  • Pure CSS Slide Up and Slide Down

    If I can avoid using JavaScript for element animations, I'm incredibly happy and driven to do so.  They're more efficient, don't require a JavaScript framework to manage steps, and they're more elegant.  One effect that is difficult to nail down with pure CSS is sliding up...

  • Multiple Backgrounds with CSS

    Anyone that's been in the web development industry for 5+ years knows that there are certain features that we should have had several years ago. One of those features is the HTML5 placeholder; we used JavaScript shims for a decade before placeholder came...

Discussion

  1. Why not go all the way and make it work in firefox?

    https://developer.mozilla.org/en/HTML/Element/marquee

  2. While I agree (strongly) with garyb that one should make all effects work in as many browsers as possible, Firefox – and in fact almost any other browser on the planet – support the marquee element, not the marquee CSS-rule. Firefox has chosen to use an XBL-binding in their internal stylesheet.

    In fact, one can make lots of XBL-bindings through CSS in Firefox, not only marquees. But that is not recommended. The Webkit marquee CSS property is also intended for internal usage as a way to support the marquee element.

    And yes, browsers do support some elements that have not been formally standardized. That does not mean one should use them, though.

  3. inder

    How to make is stop on hover? and please can you give me an image slider like blogger homepage which auto scrolls but also have arrows to slide them fast :)

    thanks

  4. how to add google adsense code in marquee ?

  5. To do the hover, simple apple a regular hover style in the normal way and change the scroll speed in the hover style! Enjoy! :)

  6. Personally, I never had a problem with marquee.. Altho its great to see it as a css class I dont see any reason why you cant use the html tag as before. At least you can be sure it will work on all browsers. Then us css to style it.

    Just my 2 cents :)

  7. Mats

    Good article, however the Demo does not work. For me. Google Chrome v23.

    Another reflection, I tested to use marquee on some table cell that has overflow:ellipsis applied and overflow-x does not overwrite the ellipsis so you get scrolling cut of text. Fugly.
    Maybe I should file a bug with the webkit team… :)

    • Yeah! overflow > overflow-x.

      a {overflow:hidden;text-overflow:ellipsis}
      a:hover {overflow-x: -webkit-marquee;text-overflow:string}
      

      = The link will scroll, but with truncated mode.

      a {overflow:hidden;text-overflow:ellipsis}
      a:hover {overflow-x: -webkit-marquee;text-overflow:string;overflow:visible}
      

      = The link won’t scroll, but without truncated mode.

  8. I’ve been looking for the most efficient and cross-browser marquee implementation. For whatever reason, even the webkit CSS marquee implementation is glitchy. The common approach is to use timer (or jQuery animate implementation) to adjust the CSS margin property of the element. This is too glitchy and very inefficient. I came up with implementation that utilises CSS3 transitions for browsers that support it and otherwise animate the scrollLeft property of the containing element. It is an experimental implementation, though it works well with IE7+. Other people might find it useful as well, https://github.com/gajus/marquee (demo https://dev.anuary.com/60244f3a-b8b2-5678-bce5-f7e8742f0c69/).

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!