Start learning with our library of video tutorials taught by experts. Get started

CSS: Transitions and Transforms

Flipping cards in 3D


From:

CSS: Transitions and Transforms

with Joseph Lowery

Video: Flipping cards in 3D

On web pages, screen real estate is often at a premium, and designers are always on the search for new techniques to hide and reveal content selectively. This movie will show you how to add a 3D card flip to your content control arsenal. We'll even throw in a JavaScript powered slide reveal for those browsers that don't support the 3D transforms. Let's start by taking a look at the web page, so you can see where we're going. As I hover over each of the paintings, it spins on its Y axis.

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
Please wait...
CSS: Transitions and Transforms
2h 25m Intermediate Jul 11, 2012

Viewers: in countries Watching now:

With robust and ever-increasing browser support, it is now possible to take advantage of expressive CSS3 capabilities across modern browsers. In this course, Joseph Lowery explores the possibilities of the new coding options, which animate well over 50 different properties automatically or interactively, and how they open the door to enhanced user experiences. This course covers the range from simple to complex transitions, including 2D and 3D transforms, and illustrates how transitions are expedited in various web authoring tools, as well as Dreamweaver. The course also contains a start-to-finish interactive slideshow project that allows you to practice and see the transitions and transforms immediately in action.

Topics include:
  • Understanding transition basics
  • Working with 2D and 3D transforms
  • Animating color changes
  • Fading objects
  • Changing the size of page elements and fonts
  • Transitioning multiple properties at the same time
  • Understanding keyframes and animations
  • Working in the z-index
  • Adding JavaScript interactivity
  • Crafting transitions in Dreamweaver
Subjects:
Web Web Design Web Development
Software:
CSS
Author:
Joseph Lowery

Flipping cards in 3D

On web pages, screen real estate is often at a premium, and designers are always on the search for new techniques to hide and reveal content selectively. This movie will show you how to add a 3D card flip to your content control arsenal. We'll even throw in a JavaScript powered slide reveal for those browsers that don't support the 3D transforms. Let's start by taking a look at the web page, so you can see where we're going. As I hover over each of the paintings, it spins on its Y axis.

But instead of showing the painting in reverse, related secondary content is displayed. And when I move off one painting and on to another, the first one reverses its card flip, while the second one flips over. So as you move around, you get a nice little card flip dance going on. Now if we take a look at the page in a non-supporting browser, you'll see the same content, but instead of a 3D flip, you get a 2D slide. How is that for a graceful degradation? Let me show you how it's all done.

I've opened the about.htm file from Chapter 4/ 04_02/about folder in my code editor. Starting in the head area, you can see I have three JavaScript files I want to draw your attention to, beginning on line 15. The first brings in the latest version of the popular jQuery library, next, we included jQuery based easing library to smooth out our non-CSS transitions, and finally, I've linked in the most recent version of Modernizr, which we'll use to detect whether browsers can support CSS 3D transforms.

Browsers that can, will get one set of classes added to specific div tags that will flip the initial content away and flip in the secondary content. While browsers that don't support CSS 3D transforms will have the content animated from the bottom up. So right below those three includes I have a script tag that contains the functionality that handles this code swapping. Let me scroll down just a bit. The opening statement on line 20, if ($('html').hasClass('csstransforms3d')), checks to see if the browser supports the property, and if so, it removes the Class slide, as you see on line 21, and substitutes flip.

Then, if selector with the flip Class is hovered over, it toggles between adding the class "theFlip" and removing it. Now if the browser does not support the csstransforms3d property, the function that starts on line 31 kicks in. Here, the slide class is left in place and a slide up animation is toggled on and off. Now before we get to the CSS, I want to show you the HTML structure of each of the artworks. So let's scroll on down, I'll go down to around line 76 or so, and the div with the ID of theArt is where there is a group of four slides.

For each slide, both the image and its description are nested under a div with a class of artGroup and the default class of slide. The code for the image like that found on line 78 is placed in front of the div with a class of details, line 79, that contains the image information. We're ready to tackle the CSS necessary for both the 3D transform and the sliding animation for older browsers. Let's switch over to transitions.css that's found in the _css folder, and I'll scroll down to the bottom, create a few empty lines to bring our code up. Let's enter in the CSS rules we'll need for the older browsers first.

And I am just going to put in a little comment here. The first one is the class of artGroup with another class of slide. And all we need to add here is the property overflow set to hidden. Now we need to create a class that positions the detail content so that it's out of view. So again, we'll target artGroup.slide, and then within that the class of detail.

All of my images are of the same size, so I can use one value here, so let's set bottom to -364 pixels. Okay, that's all we need to do for the older browsers. So for newer browsers--that's the main event--let's add the CSS necessary to make the card flip happen. In all, we'll need just four rules. First, since this is a 3D value we're working with, let's set up our 3D by adding a perspective attribute to the general image and details container, which is called artGroup that has the class of flip.

This will ensure that the children of this div will exist in 3D space. And we'll start with the -webkit-perspective property. Let's set it to a value of 800 pixels. It's kind of a medium value that should ensure no distortion. And now I am going to copy my -webkit-perspective value, paste it in three times, get rid of that extra space I put in there by mistake. And let's change the second entry of -webkit to Mozilla, -moz, the third one to -o for Opera, and let's get rid of it for the fourth one altogether.

Perspective is done. Next we want to target that child, which is the class artwork and set up the transition for the transform property with a brief duration. Again, I'll enter the selector artGroup flip, and now we'll do the second class we want to target, which is artwork, and this is the individual paintings. First we'll set up our transition for the transform property, and as always we'll start with -webkit.

Name the property we want to transition, which is transform, but of course, since this line is vendor-specific we have to add in that prefix as well. So -webkit again, -transform and the duration of 1 second. We'll leave off the timing function, and that will revert to using the default value, which is ease. Okay, let me copy and paste our transition statement, make the necessary changes, and that section is done.

Next, because perspective on the parent element only pertains to the direct children--which here is the class of artwork--we want to use the transform style property set to preserve 3D to pass that perspective along to the next level of children, which will be the image and the details. I get a little error here just to make it clear what we are doing, so we'll enter in -webkit-transforms-style, and set that to its value of preserve-3d.

Okay, that is eminently copyable, and equally replaceable, so let's make those alterations, and now that bit of code is done. Now let's add in the actual 3D transform using the rotateY property. Set to spin 180 degrees in a right to left direction-- that will be a negative direction. We'll specify two selectors, one for each face of the card.

First we'll do the detail, and that will add in the image itself. So let's enter in our -transform: rotateY property, starting with the -webkit version, and we want this to go -180 degrees I'll do my copy/paste routine. Finally, we need to make sure that when the card is flipped, the other side does not show through. For that we'll add the backface visibility property with the value of hidden, and this goes on to a rule targeting the image and the detail.

So -webkit-backface-visibility:hidden, select, copy, paste. And changing those prefixes one more time, we are going to save the file now and then let's go take a look at it in the browser. So here I have opened the about.htm file, and when you first look at it before I refresh the page, you see the details showing up, and that's because we didn't have the opacity in place.

So let's reload the page, and there are my images, and when I rotate over them, they flip beautifully. Now let's go over to Firefox, which this version does not support 3D transforms, and now when I roll over the paintings, I am getting the 2D sliding action instead of the 3D rotation. But it still looks good, and the content is still visible. So now you've got another rather snazzy tool that you can use in your designs to hide and show supplementary content at will, courtesy of 3D transforms and just a little bit of JavaScript.

There are currently no FAQs about CSS: Transitions and Transforms.

Share a link to this course
Please wait... Please wait...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed CSS: Transitions and Transforms.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked

Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.