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-4.png

Recent Features

  • Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: new Element Madness The first way to create UI-driven...

  • LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

Incredible Demos

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!