Using CSS3 to add reflection to an iframe

August 28th, 2010 | 9,528 views | 11 Comments » |

Mozilla just landed a really cool new extension to the CSS background-image property in the Firefox 4 nightlies. The new extension allows you to use arbitrary DOM elements as background images. The syntax looks like this:

#iWantBackground {
    background: -moz-element(#sourceElementID);
}

-moz-element lets you use virtually any element as a background, including iframes and canvas elements. I wanted to play around with it a bit, so I hacked a quick demo of an iframe with a live-updating reflection. It borrows heavily from the code in this excellent article.

Demo
(Works in Firefox 4)

I know that lots of my readers aren’t running the latest Firefox nightly, so here’s a screenshot of what the reflection effect looks like:

-moz-element  Reflection on iframe

Here’s the code I wrote:

    <style type="text/css" media="screen">

        body {
            background: #000;
        }

        #main {
            margin: 30px auto 0 auto;
            width: 800px;
        }

        #source {
            display: block;
            width: 800px;
            height: 480px;
            border: 0;
        }

        #reflection {
            margin-top: 5px;
            background: -moz-element(#source) bottom left no-repeat;
            -moz-transform: scaleY(-1);
            height: 200px;
            border: 0;
            mask: url(#reflection-mask);
        }

    </style>
    <div id="main">
        <iframe id="source" src="http://en.wikipedia.org/wiki/Color"></iframe>
        <div id="reflection"></div>
    </div>

    <!-- SVG from http://hacks.mozilla.org/2010/08/mozelement/ -->
    <svg>
      <mask id="reflection-mask" maskContentUnits="objectBoundingBox">
        <rect x="-0.1" width="1.2" height="1" fill="url(#reflection-gradient)"/>

      </mask>
      <linearGradient id="reflection-gradient" gradientUnits="objectBoundingBox" x1="0" y1="1" x2="0" y2="0">
        <stop stop-color="white" stop-opacity="0.6" offset="0"/>
        <stop stop-color="white" stop-opacity="0" offset="100%"/>
      </linearGradient>
    </svg> 

Also, I decided to add a new hacks section to my blog to keep track of all the random code I write.

You should share this with your friends:

11 Comments | Leave a comment » More posts about: Hacks

11 Comments on “Using CSS3 to add reflection to an iframe”

  1. 1 Chris said at 6:01 pm on August 28th, 2010:

    Hmm, it’s not working for me in FF4 beta 3 :(

  2. 2 Chris said at 6:02 pm on August 28th, 2010:

    Ignore that, I thought beta 3 was the latest >.<

  3. 3 Feross Aboukhadijeh said at 1:24 am on August 31st, 2010:

    Ya Chris, you’ve got to make sure to download a Firefox nightly. I don’t think this feature has made it into a beta yet.

  4. 4 MacPaul said at 10:22 am on September 11th, 2010:

    Just a heads, I was hoping to check the reflection demo in Safari but the page 404′s, so does the URL to your hacks page

  5. 5 Feross Aboukhadijeh said at 1:18 pm on September 11th, 2010:

    Thanks for letting me know. I’ve been moving things around and missed this link. :)

  6. 6 Giovanna said at 8:48 am on September 15th, 2010:

    Thanks for Sharing!We all will enjoy more on the web.

  7. 7 Giovanna said at 8:49 am on September 15th, 2010:

    Gracias por compartir! Todos podrán disfrutar de más en la web.

  8. 8 Christian Sonne said at 6:24 am on October 18th, 2010:

    The link to the demo still throws a 404

  9. 9 Feross Aboukhadijeh said at 2:42 am on October 25th, 2010:

    So sorry about that. I’ve been moving things around a lot. The link to my hacks page should work now.

  10. 10 SkullBocks said at 10:50 am on November 1st, 2010:

    Hi Feross, I want to tell you, that I found a Bug in YouTube…

    If you’re are interested, visit my website:

    http://www.Skull-Bocks.com

    Bye

  11. 11 Playing with -moz-element in Firefox 4 said at 12:20 am on April 12th, 2011:

    [...] tinkering phase, so essential logic handling may have been lacking. The most compelling idea was a nice iframe reflection, but Webkit has a far more human-friendly, dedicated effect for [...]


Leave a Reply

Fork me on GitHub