Creating playing cards with CSS & HTML


In this little tutorial we’re going to create simple playing cards using only HTML and CSS (without image files). The purpose of this article is to cover the basics of how CSS and HTML work together. This should work no matter what versions you’re using (CSS2.1, CSS3, HTML4, HTML5, etc.). Note: This tutorial assumes you have a basic knowledge of HTML.

Intro to CSS:

First lets go over how and where to use CSS. There are three main ways to use CSS in an HTML document.

1. Inline Styling

The first way you can use css is to place the CSS code in and HTML element’s style attribute. For example:

<p style="font-style: italic">My CSS formatted text.</p>

All the text between the specific styled <P> tags will be italic. No other text will be italicized unless the same style attribute is added to that text’s HTML element. This method is generally frowned upon due mainly to the fact that it creates much longer and larger web pages that are harder to maintain in the long run.

2. Internal Style Sheet

Another option is to place your CSS in between <STYLE></STYLE> tags usually in the <HEAD> of your HTML document. For example:

&amp;amp;amp;amp;lt;style type="text/css"&amp;amp;amp;amp;gt;
 p { font-style: italic; }
&amp;amp;amp;amp;lt;/style&amp;amp;amp;amp;gt;

Now any text in the entire HTML document that is in between <P></P> tags will be italicized. There is no need to use style=” “ anymore because it’s already defined. This method is best for page specific styling like a custom blog posts and should also be avoided when possible. You can use this anywhere you’d like in an HTML document but keep in mind that any the CSS loaded prior to the CSS in a style element will be overridden by the new style.

3. External Style Sheet

The 3rd and most common way to use CSS is through an external file containing all the style tags to be used on an entire website, server, program, etc. The file is usually hosted on the same server, ends in .css, and is called in the head of a HTML document through the <LINK> element. For example.

&amp;amp;amp;amp;lt;link rel="stylesheet" type="text/css" href="style.css" /&amp;amp;amp;amp;gt;

the file “style.css” could look like this:

BODY { font-family: verdana,arial,helvetica,sans-serif; font-size: x-small; background-color: #FFFFFF; color: #000000; }
p { font-style: italic; }
a {text-decoration: none; }
h1 { font-family: verdana,arial,helvetica,sans-serif; color: #E47911; font-size: small; }
h2{margin-bottom: 0em; }
h2 { margin-bottom: 0em; }
h3 { margin-bottom: 0em; }
.small { font-family: verdana,arial,helvetica,sans-serif; font-size: xx-small; }
#big { font-family: verdana,arial,helvetica,sans-serif; font-size: x-large;}

Note: .small would apply to any HTML element with a class name of “small.” #big would apply to any HTML element with an ID of “big.” For example:

&amp;amp;amp;amp;lt;div class="small"&amp;amp;amp;amp;gt;This text will be extra-extra-small.&amp;amp;amp;amp;lt;div&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;p id="big"&amp;amp;amp;amp;gt;This text will be extra-large.&amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;gt;

Creating Playing Cards

Now that you know a little about CSS and HTML, lets make some cards.

Content

In the BODY of your HTML create One DIV element with the class name “outline” with 2 DIVS inside named “top” and “bottom.”

&amp;amp;amp;amp;lt;div class="outline"&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;div class="top"&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;div class="bottom"&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;gt;
 


The first DIV will be the “outline” of the card and hold everything else we need in our card. The “top” and “bottom” DIVs will hold our card’s number or letters, and symbols respectfully. So lets add our card’s content.

&amp;amp;amp;amp;lt;div class="outline"&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;div class="top"&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;span&amp;amp;amp;amp;gt;A&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;span&amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;hearts;&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;h1&amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;hearts;&amp;amp;amp;amp;lt;/h1&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;div class="bottom"&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;span&amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;hearts;&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;span&amp;amp;amp;amp;gt;A&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;gt;
 


As you can see we’ve added our card’s suit and rank. I’ve decided to use &hearts; for a heart suit here but you can use any suit you’d like by using &spades; &clubs; or &diams; Also notice we’ve added SPAN tags to individually wrap our card’s suit and rank in the top and bottom divs.

When rendered, we get something like this:

A♥

♥A

Not quite a playing card, but we can see we’re getting there. Now for the styling!

Styling

Now lets style our card. I like to start with the standard HTML elements first then work my way down the page.

body { background: #339900; }
span { display: block; }


Through our BODY styling we’ve changed our pages background color to a “poker table” green. We’ve also set up our SPAN tags so that they align one on top of another instead of side by side. We now have something like this:

A♥

♥A

So lets style our DIV’s.

body{ color: #CC0033; background: #339900; }
span{ display: block; }

.outline{ width: 110px; text-align: center; padding: 10px; margin: 10px; background: #FFF; color: #cc0033; }
.top{ text-align: left; }
.bottom{ text-align: right; }


Through .outline we’ve defined the width of our card, centered the text, added 10px of padding and margin for spacing, changed the background color of the DIV to white, and changed the text color to a dark red (in that order). For the .top and .bottom div we’ve just aligned our text to either the left or right accordingly. So now we have something like this:

 

A♥

♥A

Well that’s more like it. We could stop here as technically we have a card but we want our card to be a little more realistic. To do this, we can use CSS to add rounded borders and drop shadows. Unfortunately this currently will not work in Internet Explorer however IE9 will be here soon! So for Opera, Firefox, Chrome, etc let’s add the following to our CSS

body{ color: #CC0033; background: #339900; }
span{ display: block; }

.outline{ width: 110px; border: 0px solid black; text-align: center; padding: 10px; margin: 10px; background: #FFF }
.top{ text-align: left; }
.bottom{ text-align: right; }

.shadow{ box-shadow: 1px 1px 3px #000; -moz-box-shadow: 1px 1px 3px #000; -webkit-box-shadow: 1px 1px 3px #000; }
.rounded{ border-radius:10px; -moz-border-radius:10px; -webkit-border-radius:10px; }


This is sort of a catch all method for creating shadows and rounded corners using box-shadow, or a browser specific box-shadow. The settings edit the shadows distance and blur amount as well as color and the rounded border’s corner radius. Feel free to play around with these for a better understanding of what each one does.
You may have noticed that we didn’t add .shadow or .rounded anywhere in our HTML. Luckily, we can add more than one class to an element’s class attribute. So to tie in our CSS we need to go back and add ” shadow rounded” to our “outline” DIV in the first line of our HTML like so:

&amp;amp;amp;amp;lt;div class="outline shadow rounded"&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;div class="top"&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;span&amp;amp;amp;amp;gt;A&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;span&amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;hearts;&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;h1&amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;hearts;&amp;amp;amp;amp;lt;/h1&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;div class="bottom"&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;span&amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;hearts;&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;span&amp;amp;amp;amp;gt;A&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;gt;


Finally, you should have your completed playing card! Only 51 more cards to go.

 

A♥

♥A

Well I hope you enjoyed this article. Play around with the height and width, add texture/images if you’d like. Let me know what you think.

Download the files used in this tutorial: [download id=”3″ format=”3″]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>