Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Flipping cards in 3D

From: CSS: Transitions and Transforms

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.

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.

Show transcript

This video is part of

Image for CSS: Transitions and Transforms
CSS: Transitions and Transforms

26 video lessons · 14531 viewers

Joseph Lowery
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

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.


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.

Join now Already a member? Log in

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 preferences from the dropdown menu.

Continue to classic layout Stay on new layout
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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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.