Create CSS3 folding caption effects

For richer user interfaces it is often desirable to include some animation to make an effect smoother or more appealing – or effects such as rotating elements and text. Traditionally in HTML pages, the primary means to add animations was to use JavaScript to adjust the desired CSS property value over a given period of time. In CSS3, these animations and transformations can be handed off to the browser and defined in the CSS layer using CSS3 transitions and transforms, which are now supported in most of the updated web browsers. Internet Explorer, however, is a little less accommodating with regards to transitions, though thankfully the support is there for IE10.

What we’re going to do in this tutorial is really take CSS transitions and transforms to the next level and combine the perspective property. This is done in order to create some very useful folding animations that you could easily use in your own web design projects. We’re also going to finish off by making our folding effects responsive with an added touch of media queries. So, open up your chosen text editor and let’s get started!

The HTML

Once you have opened up your chosen text editor, we’re going to create a new file with the HTML5 doctype. Within our head tag we can add in our meta information and links to two CSS files we are going to create, one for our default styles and one for our main styles. Also because we are using HTML5 and CSS3, let’s include Modernizr to detect browser support.

Container list

The next step would be to create a containing <div> within the <body> tag and with an unordered list included. So let’s give our containing <div> a class name of ‘container’ and then create an unordered list with four list tags within. We’ll give this list two class names of ‘grid ca-style’ (the ca-style stands for caption animation style)

Images and captions

We now need to populate our list items and we first use the <figure> element. The <figure> element is used to represent a unit of content and in this case, this will be for our caption. We then add the link to our image (which is located in a folder called ‘images’) and then we add the <figcaption> element along with our caption content.

Finishing up the HTML

Let’s populate the next two list items with the same markup but making sure we change the <h3> tags with the appropriate heading and the image path. We’ve also created a class called ‘btn_hover’ that will be used to create a simple hover effect on a ‘take me here’ button.

Default CSS

In this step, we’ll create a new file and call it ‘default.css’ so we can set some default styles. Firstly we will add the ‘box-sizing’ CSS3 property and the ‘border-box’ value to every element. This will allow us to apply a natural box layout model to all elements and prevent any unnecessary overflows when using percentages.
Body styles
Let’s now set our font to 100% and then make sure all default padding and margins are set to zero. We then set our font style and colour, as well as the background to our document. Last of all, let’s give our links a default colour and make sure there’s no underline.

The grid

Now we’ll start working on our unordered list. In Step 2 we gave our list a class name of ‘grid’ and the idea is to have four boxes within a grid-like layout. The class ‘grid’ will work like a container or wrapper for the boxes. We then target our <li> items and give those a fixed width of 440px, making sure they are positioned relative to our grid.

The figure element

The <figure> element is intended to be used in conjunction with the <figcaption> element to mark up photos, diagrams and code examples, just to name a few. We are going to use this to include our images, setting its position to relative and making sure its maximum width is set to 100%, ready for when we make this responsive.

Styling the caption

Our caption is going to contain a title, some text (your name) and a simple button. We first set its position to absolute, so we can position it top-left and then give it some padding. We will then give the background a colour and make the text (your name) red. A little later on we will give the caption its height and width.

Caption title

We’ve almost finished the caption by adding the styles to the title. We are going to make sure we clear any paddings or margins and then give it a colour of white by using the hexadecimal value of #fff. You can also experiment by using a <h2> or a <h1>.

Caption button

In this step, let’s finish up our caption by adding in a button. The purpose of the button is to take the user to another page relating to that specific information. So, let’s make sure the text is centred and that we give the button some padding. The button will now resize depending on the amount of text you add in. Everything else should then be relatively straightforward.

Giving it perspective

To activate 3D space, an element needs perspective. There are two ways of doing this in CSS, but we will use the ‘perspective’ property. The value of the ‘perspective’ property determines the intensity of the 3D effect and the ‘perspective-origin’ property allows you to change the bottom position of the 3D element. Do have a play around with these values to see for yourself.

Transform-style property

When the ‘transform-style’ property is applied to an element, it determines if that element’s children are positioned in 3D space, or flattened. The default value is ‘flat’ but we want to make sure we tell the browser that we are working in 3D. This is done relatively easily by using the ‘preserve-3d’ value. After that, we’ll make sure that anything that is overflowing is hidden.

Animating images

Next up would be to animate the images when our caption moves across. Here we can easily do that by using the ‘translateX’ value. The ‘X’ means this will move on the horizontal ‘X’ axis, from left to right. So, if we were to set this a negative value (-25%) then it would move left. In this case, we only want it to move 25% of the parent’s width over to the right, so we use the following code.

Caption visibility

Let’s set the height and width of our caption and make sure we can’t see it by setting the opacity to zero. With 3D transforms, you can rotate an element, so what we would think of as the front, no longer faces the screen. We can accomplish this by using ‘backface-visibility: hidden;’ then set its origin to the left side.

Backflip the caption

Now let’s rotate the caption backwards by 90 degrees on the Y-axis, which will make it be flipped towards us. We are then going to set the speed at which it will return back, by using ‘transform 0.4s’ and ‘opacity 0.1s 0.3s’. The next step is where we make all this magic happen by creating a hover state.

Flipping the caption

Without the hover state, none of the CSS we wrote in the last step would mean anything, as the caption hasn’t flipped forward yet – let’s deal with that now. When the cursor hovers over, we want our caption to become visible by setting the opacity to ‘1’ and then rotate forward by 90 degrees at the same speed we set in the last step.

Position the button

Let’s position our ‘Take me there’ button within our caption. Because we positioned the <figure> element as relative, the button can now be positioned anywhere within the caption using ‘position: absolute’. We can position it 20px from the bottom and 20px from the right.

Button hover state

We can finish off our button by giving it a nice transition. So on hover the button element is rotated(transitioned) from its default colour to a lighter colour, over two seconds and ‘all’ of its properties can be altered during the transition. After that we can use ‘ease in’ to make a smoother transition.

Responsive grid

We are now going to think about mobile devices and add some media queries to make our grid responsive. We will point to our ‘grid’ class and then set the maximum width to 31.5em. Following this, we want to make sure we have enough padding all around.

Responsive images

Now that we have made the containing grid responsive, let’s do the same with our images. We need to target all the ‘li’ items within our grid and set their widths to 100%. After that, we make sure we set a minimum width to 300px. Any less would make the images look too small and vice versa.

001 .grid li {
002 width: 100%;
003 min-width: 300px;
004 }
005 }

Conclusion

Creating animations with CSS3 takes a little bit of time to get your head around, especially when using the 3D properties and values. So, with that in mind, be sure to experiment and see what animations you can achieve through using these new and exciting CSS features.