CSS Card Tricks

Know any good card tricks? Hopefully, after today, you will! We’re going to build some simple and attractive playing cards with pure CSS, then we’ll learn how to target and animate each card for some added fun.

Along the way, we’ll see how to use before and after to build the cards with minimal markup. Let’s get started!

The Ultimate Designer Toolkit: 2 Million+ Assets

Envato Elements gives you unlimited access to 2 million+ pro design resources, themes, templates, photos, graphics and more. Everything you'll ever need in your design resource toolkit.

Explore Digital Assets

Launch the live demo or download the source code.

Building a Card

The first task on our list is obviously to build a card. We’ll pull this off with minimal markup and without embedding any images. Impossible you say? Nay! Let’s see how it works.

Start by creating a new HTML document and inserting a basic div with two classes: card and suitdiamonds.

Now it’s time to put something on the card. We’ll make everything an ace for the sake of simplicity, so all you need is a paragraph with an “A”.

As we proceed, keep in mind that my entire goal here is minimal, reusable code. Notice that I’ve used classes, not IDs. This is of course because classes are reusable. Also, there’s a reason the class name here isn’t just “diamonds”, which we’ll see later. As we go forward and look at our CSS, notice where I chain together selectors and create an almost module-style code base to cut down on repetition.

Card CSS

Believe it or not, that’s all the markup that we need for our card. Everything else will be pulled off with CSS. To begin here, we need some basic page styles, followed by our card class styles.

As you can see, the card styles are pretty simple. We make the card white, round the corners, apply a shadow, etc. Nothing too fancy aside from the relative positioning.

Now let’s make our “A” look a little more attractive. Again, not much here, just some text alignment and font styles.

We should be doing pretty well at this point, let’s take a look and see what we’ve got:


It looks like a card, does it not? There’s something missing though… the suits! Diamonds, hearts, clubs, spades; you have to have suits. This gets difficult if we want to stay away from images, but I’ve got a few tricks up my sleeve.

Suit CSS

Given that we don’t want to add any extra markup to our page, we’ll obviously want to look to before and after to insert the suit icons. Fortunately, most standard browsers recognize a wide variety of symbols. If you check out CopyPasteCharacter, you’ll find exactly what you need for a deck of cards.

As you can see, here we inserted a diamond using both before and after (so we’ll have two). Next, we need to repeat this across all four suits:

If you’re quick, you’ll realize that one of these symbols on every card should be upside down. This would be simple enough with a CSS transform, but given that we’ll never actually view the card upside down on our screen, I think this is an unnecessary bit of reality that’s not worth the execution code.

Now, all we’ve done up to this point is create the symbols, we still have to properly size and position them. Normally, this would take a ton of selectors given that we have four suits, each of which has a before and after version to style. We were clever though and we used the word “suit” in each class name. This means that we can easily target all of the suit classes at once. Then, we need only style the before and after classes separately. Awesome!

What the heck? If you’ve never seen this before, it’s something super fancy with a name that I love to say because it makes me sound smarter than I really am: the arbitrary substring attribute value selector. I explain this in depth here, but basically, it allows you to target parts of an attribute and grab anything that has that part in common.

With that, we’ve got a complete card styled up! It looks pretty impressive considering our markup is so brief.


A Hand of Cards

We only put a single card on our table, but we’ve provided the style for all four suits. Let’s create a “hand” of cards to show them all off. Here’s the markup:

This is pretty much the same as before, only this time we’ve got a wrapper div with a “hand” class and we’ve used all four suit classes. For our hand class, we don’t really need a lot of styling. However, we do use floats on our cards, which means we’ll need a clearfix on the card parent, which is the hand div. Here’s Nicolas Gallagher’s code for this along with a simple hover for the cards.

That’s it! Now we have ourselves a nice little set of cards made with pure CSS and HTML. Here’s what they look like:


Spread ’em

Now that we’ve built our cards, let’s have some fun with them. What I want is to have a single card, that when hovered over, fans out into our full hand. In the fanned out form, the cards shouldn’t be straight like we saw above, but rotated like when you hold them in your hand.


The HTML to pull this off is pretty much exactly what we saw before. Only this time, add a “spread” class to the container div.


Pulling this off is going to take quite a bit of CSS, but it’s all pretty easy to wrap your mind around. First, we’re going to be a little bit more specific about our container. Do this by setting a height, width and positioning context on the spread class.

Next. absolutely position the cards so that they all stack on top of each other relative to the spread div. Reset the positioning values and set up a transition for those positioning values so that any changes will be animated.

Now we have to set up a bunch of hover states that manually position each card in the array that we want. We do this with CSS3 transforms targeted at each suit along with varying top and left values. Ultimately, setting this up was a lot of trial and error. Fortunately, I’ve done that work for you, here’s the code that you’ll need.

Finally, when the cards are in their spread out form, I want another hover state that subtly highlights each card. We can do this by darkening that box shadow that we applied earlier.

See It In Action!

Now that we’re all finished with the code, it’s time to take another look at the live example. As a bonus, I threw in a third hand that grows when you hover over a card.

Demo: Click here to launch.


What Will You Do With Them?

So there you have it, pure CSS and HTML playing cards. To be honest, I have no idea what to do with them, but they were sure fun to build. If you have any great ideas for improving or using these in a fun way, leave a comment below and let me know!