All posts tagged CSS

An Introduction to CSS3 – Creating a Button

This tutorial is just going to be a little tip introducing you to all the great things that CSS3 introduces. However, these things are not yet supported cross browser – Safari, Chrome and Firefox support most of the new features, Opera a few. But funnily enough IE does not, and IE versions 6 to 8 do not support any of the new code we’re going to write today. The IE9 preview introduces support, however for now you’re going to have to accept that this code will not work in IE. There are ways around this – using images as a fallback, or using Javascript. This will be covered in a later tutorial, however today we’ll only be focusing on what these new features are, not fallbacks. So, I suggest you load up the most recent versions of Safari, Chrome or Firefox and we will get going!

Set up a Link

We’re going to set up a basic link for us to transform into a nice button. No images allowed!

<a href="something.html">A Button</a>

Initial CSS

Nice and easy. I’ve also set up some basic CSS properties:

display: block;
width: 200px; height: 50px;
font-family: "Lucida Sans Unicode", Verdana;
font-size: 20px;
text-decoration: none; color: white;
text-align: center;
line-height: 50px;

You will notice I’ve set the text colour to white but no background. Don’t fret, as we’re going to do that shortly. Another neat trick is setting the height of the link to 50px but also setting the line-height to the same 50px value. This means that the text will be perfectly aligned vertically so it sits in the middle of the button.

Background Gradient

The first thing we’re going to do is add a background gradient. These are currently only supported in Safari, Chrome and Firefox 3.6+. Whilst Safari and Chrome are webkit based browsers, Firefox is not, and as such Firefox’s syntax is very different to Safari and Chromes. Eventually, once the CSS3 spec is confirmed, they will all use the same syntax, but no one knows. A very handy tool to do this is this Linear Gradient generator. There’s loads of other handy tools there which we will be mentioning in the future.

Webkit Syntax

Is like so:

background: -webkit-gradient(gradient types, start horizontal %, start vertical 0%, end horizontal % end vertical%, from(colour), to(colour));

Notice how we have to add “-webkit” to the front of the gradient code. Once everything is finalised, you wont have to, but for now we have to put up with it.
CSS3 also allows for radial gradients, however we wont be covering those today, we will be using a linear gradient. The first two percentage values decide where the gradient should start. If you’re doing a gradient from top to bottom, these will usually both be 0. The next two % values are for where the gradient should stop. A normal vertical gradient will have the values 0% 100% – that’s 0 from the left, and 100% from the top. The next two values are easy – which colour to start at, and which to end at. You can also add colour stops, which are done by adding this code after the final part of the gradient declaration:

, color-stop(.6,#333))

The .6 is how far along you wish to add the stop, the the colour is, well, the colour you want the stop to be.

We’re not using colour stops today, we’re going to create a simple orange gradient for our button, using this code:

background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#D9AE38), to(#D65D00));

That code is for webkit, and now we need to same code for Firefox. Lets do it.

Firefox Syntax

Firefox sadly does it all its own way.

background: -moz-linear-gradient(horiz start % vertical start% angle in deg,end colour, start colour);

For a start, this time the code is -moz-linear-gradient, not just -gradient as it is with webkit. The first value defines the horizontal position, the second % value is the vertical one. It’s not as clear cut as the webkit code, in fact, it’s hard to explain. I highly suggest you head over to the linear gradient generator using Firefox and mess around yourself. Another important note is that the colours are the reverse of what you might imagine, especially doing a linear gradient. Webkit does it going “down” whereas Firefox orders the colours going “up”. The code for our button looks like this:

background: -moz-linear-gradient(0% 75% 90deg,#D65D00, #D9AE38);

Finally, one easy fallback that takes little effort is just adding a colour. So, our final code (for Webkit/Firefox) looks like below. Other browsers will just display the colour.

background: orange -webkit-gradient(linear, 0% 0%, 0% 100%, from(#D9AE38), to(#D65D00));
background: -moz-linear-gradient(0% 75% 90deg,#D65D00, #D9AE38);

And the results are like so:

Border Radius

The gradient code is not very easy, but border-radius (or “rounded-corners” to me and you) is much more simple. It’s like so:

-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;

The first two declarations are for Webkit and Firefox. The third is the generic one, which is supported by Opera. The higher the value, the more rounded the corners are. Once you’ve added those, your button should be taking shape nicely!

Box Shadow

This is supported in Safari, Chrome, Firefox and Opera but does use vendor specific declarations once more. Opera uses the default without any vendor extras.

box-shadow:horizontal offset px vertical offset px blur px #colour

Once more the same site that had the linear gradient generator has a superb resource for generating box shadows. Horizontal and vertical offsets point out how far down and across the shadow occurs on the box. The blue is simply how blurred the shadow will be, 0px gives you a very crisp shadow, whilst a higher level blurs it a lot more, almost like smudging it.

We’re trying to be slightly subtle here, so here’s the code we’re adding:

-webkit-box-shadow:2px 2px 2px #333333;
-moz-box-shadow:2px 2px 2px #333333;
box-shadow:2px 2px 2px #333333;

This is where you might see the downside of CSS3 and all these vendor declarations. There is a lot of repetition.

Now our button should look like this:

Text Shadow

Text-shadow actually existed in CSS2 but was removed in 2.1. It’s now back in CSS3 and is supported by all but IE. No vendor specific additions required and the code is really simple.

text-shadow:horiz offset px vert offset px blur px #colour;

It’s identical to the code for box-shadow. Again you can mess around with this on the same site as before, the idea here is to experiment until you get it right.

Our code looks something like this:

text-shadow:2px 1px 1px #000;

And our button is complete, and looks like so:

All Done

In future posts we’ll look at fallback solutions in browsers that do not support these new features, but for now I hope you enjoyed this little dive into CSS3. Any problems, get hold of me on twitter and feel free to ask anything.

Create Simple Captions using CSS

Let’s create some interesting image captions using css. To find free photos, check out this article “40 Free Stock Photo Sites”.  The image I selected came from Stock.xchng*Please note that the gray border around each example is styling from The Web Squeeze. Your examples will not have the gray border.

Heading

The most basic caption can be created using any of the heading tags.  In this example, I’ve used an h2 which is defined with Tahoma in a size of 22px.  Simple right?

The html…

<div class=&quot;box&quot;>
<h2&quot;Piping Hot Pepperoni Pizza</h2>
<img src=&quot;pizza.jpg&quot; width=&quot;300&quot; height=&quot;199&quot;; alt=&quot;Pepperoni Pizza&quot; >
</div>

The CSS…

  .box {
   width: 300px;
}
h2 {
font-family: Tahoma, Geneva, sans-serif;
font-size: 22px;
color: #000;
}
  

Dark Background With Light Text


In this example the styling of this caption is actually done in the containing div, which in this case is .box2.

Here is what the html looks like…

 <div class=&quot;box2&quot;>Piping Hot Pepperoni Pizza<img src=&quot;pizza.jpg&quot; width=&quot;300&quot; height=&quot;199&quot; alt=&quot;Pepperoni Pizza&quot; ></div>

The CSS…

.box2 {
font-family: Tahoma, Geneva, sans-serif;
font-size: 18px;
font-weight: bold;
color: #FFF;
background-color: #A42A1D;
text-align: center;
height: 210px;
width: 300px;
}

The height and width are both determined by the height and width of the image.  Setting the height for this div, is important if you don’t want the deep red background to show under the image.  Try this styling without the height attribute.  You’ll see what I mean.

Caption On Top of Image

This example uses the image as the background of the box3 div.

Take a look at the html…

<div class=&quot;box3&quot;>Piping Hot Pepperoni Pizza</div>

You can’t get any simpler than that can you! The magic is always in the CSS!

Check it out…

.box3 {
width: 298px;
background-image: url(pizza.jpg);
height: 199px;
font-family: Tahoma, Geneva, sans-serif;
font-size: 18px;
color: #FF0;
padding-left: 2px;
}

Looking more closely at the css you’ll see in the first line that I set the width of the div.  The image is actually 300px in width so why did I set the width at 298px?  Because I didn’t want the text to sit on the left margin of the photo I added two 2 pixels of padding-left.  Whenever you add padding you must deduct that amount from the overall width or height. As you can see the pizza is now the background image of the div.  That leaves me all the room inside the div to place my text.  And the text will now appear to be over the photo.  Whenever you use a background image in a div, you will have to set the height in order to see the entire image. Again, if you aren’t sure what I mean by not seeing the entire image, delete the height, and you’ll see the difference.

Side Vertical Caption


I’m not going to lie, I love this one.
I found with this caption technique you have to set a div height otherwise you will see very different effects in Firefox versus Internet Explorer.

Here is the HTML for this…

 <div class=&quot;box4&quot;>
 <img src=&quot;pizza.jpg&quot; alt=&quot;pizza&quot; width=&quot;300&quot; height=&quot;199&quot; class=&quot;float-left&quot;>
  P<br />
  I<br />
  Z<br />
  Z<br />
A</div>

The CSS…

.box4 {
width: 325px;
font-family: Tahoma, Geneva, sans-serif;
font-size: 24px;
font-weight: bold;
text-transform: uppercase;
background-color: #F5ECA7;
height: 199px;
}

.float-left {
float: left;
padding-right: 5px;
}

This technique uses two css classes. The first is the box4 div class.  This holds all the styling of the text and also the dimensions of the div.  In order to get the wording off to one side, you need to use a float.  In this example I used float: left; but you could certainly use a right float and have the text appear on the left side of the image.  The key to making this work is selecting the right width of the box. With a 24 px font, I had to play around with the width of the box until I was able to get enough spacing for each letter.  By putting a line break after each letter, I got this really fun vertical text.

Caption With Additional Text


This example shows a colored background area with two differently styled text areas.
Let me first show you the html and css and then we’ll talk about why and how.

HTML…

 <div class=&quot;box5&quot;><img src=&quot;pizza.jpg&quot; width=&quot;300&quot; height=&quot;199&quot; alt=&quot;Pizza&quot; /><br />
<span class=&quot;large-text&quot;>Piping Hot Pepperoni Pizza</span><br />
<span class=&quot;small-text&quot;>Made fresh daily, our pizza is hot, cheesy and topped with only the freshest ingredients.&amp;nbsp;</span></div>

The CSS

.box5 {
  width: 300px;
  background-color: #DE9749;
  }
  .large-text {
  padding-left: 3px;
  font-size: 18px;
  font-weight: bold;
  color: #552B00;
  font-family: Tahoma, Geneva, sans-serif;
  }
  .small-text {
  font-family: Tahoma, Geneva, sans-serif;
  font-size: 12px;
  color: #090400;
  display: block;
  padding: 0px 5px 5px 5px;
  }

Let’s look at .div5.  Simple code.  I just gave the div a width and added a background color.  The large-text div isn’t much more complicated.  This is how I styled the first line of larger text.  You could use a h2, h3, etc.  here if it suits your website but I’m pretending that my website doesn’t deserve a heading. The only added styling is a padding-left of 3 pixels so the text doesn’t sit directly on the left margin.

The small-text class has two added features.  Display: block; was added so that the entire block of text which spans several lines all align together.  If you didn’t use display: block; the second line of text would not be indented like the first line. I also added some padding to give the text some breathing room.

Here are 5 examples of adding interesting captions.  I hope you feel comfortable adding some interest to your websites and exploring ways to make my code your own.

An Introduction To CSS Sprites

What are “sprites”?

The dictionary defines a “sprite” as ‘A small or elusive supernatural being; an elf or pixie.’. It also defines it as ‘A small bitmap image, often used in animated games but also sometimes used as a synonym for icon.’. It’s this second definition that we’re talking about, so for the rest of this article, I won’t be mentioning elves!

Sprites have been used in computer game graphics for many years to make animations smooth, and to reduce load times. All frames of an animation are included in a single image, called a sprite sheet, and only certain areas of the image are visible.

Using this same principle of sprites with CSS, the idea is that you load in a single file with all your images/icons for a particular use. Then to use one of those images/icons, you simply set the stylesheet to reveal that portion of the image in the place you want it.

Think of it like this:

To illustrate just how sprites work, we’ll be using an extremely advanced piece of representational technology – cue cards!

In this example each cue card contains a page element, the green one has an image with 3 icons, and the blue one has a div tag. The div tag on the blue cue card has a transparent background, so a hole has been cut to simulate this.

index cards

Now, if we add our green cue card as a background-image to our div, we can see exactly how sprites work. Our div is only as big as one icon, so we can’t see the full image. However by changing the background-position of the image (in this case from left to right), we can control which one of the icons is visible inside the div.

index cards

If you think of the page-load process as picking up an image cue card and placing it behind a div cue card, then rather than picking up 3 small cue cards, we’re simply picking up a single larger one.

Why do I need CSS Sprites?

Firstly, as illustrated above, by using CSS Sprites you will save a lot of time loading files. This is good, because it means less requests to the server, and also means no delay when switching images because they’re all already there.

If you’re familiar with jQuery or other mechanisms for javascript animation, you can also use the same principles as CSS Sprites to do simple transition animations.

Using sprites also means you can change entire icon themes by changing just one CSS file.

Who uses CSS Sprites?

YouTube Sprite

A lot of websites these days are using CSS sprites, in particular on icon sets and image based navigation. Most notably, YouTube uses a huge CSS Sprite Sheet for pretty much every image present on the entire site. You can see a small crop of this above, but the full image is much larger.

ratings

It’s particularly interesting to look at the star rating icons on the Sprite Sheet. The div which displays the star ratings on YouTube is as wide as 5 stars. When you hover over this menu, the background is simply moved from left to right to display either more empty stars or more filled stars, depending on where you place your cursor.

How do I use CSS Sprites?

Ok, getting down to the nitty gritty of how do you actually do this CSS Sprites stuff!

Effectively, the premise is that you have an element that matches the dimensions of your icons, you set your image file to be the background of this element, and you position it so that the desired icon is the visible one. Then for effects like a mouseover hover, you just change the background position so a different icon is visible.

So let’s try to replicate our cue cards example, but with real code and images! First of all I’ve created an image with our 3 icons in the same positions as they were on the cue card. You’ll notice that I haven’t left any blank space around the icons; this is to preserve file size with a smaller overall image, and also to make the maths a bit easier down the line. More on that in a moment!

sprite

Now let’s write the code, notice that each tag has 2 classes: its name, and “icon”. There’s no need to apply multiple identical styles to multiple different classes, when we can apply a single class to 3 different tags. You’ll see what I mean in a moment when we write the CSS, but for now let’s just create the 3 div tags which will hold our icons.

And now the CSS:

.icon {

float:left;

width:20px;

height:20px;

margin-right:10px

background:url(images/sprite.png);

}

.twitter {background-position:0 0}

.facebook {background-position:-20 0}

.tumblr {background-position:-40 0}

So let’s look at the “icon” class first: Because we know that each one of the individual icons is 20 pixels wide and tall, this is the size that we’re want to set the div to. If the div is bigger than this we’ll be able to see the other images on the Sprite Sheet, if it’s smaller, then we won’t be able to see the full icon. We want to lay these divs out with some space between them so we’ve floated them to the left and added some margin right. Finally we’ve specified that all divs using the class of “icon” will use the same background image.

These styles apply to all of our divs, so there’s no point in repeating them multiple times when we can just group them together into one class.

Now if we look at the CSS for the individual icons we can see that each class has only a single property: “background-position”. This property takes two values, the first is the distance from the left edge of the containing element (in this case a div), and the second is the distance from the top edge of the containing document. So a value of “0 0″ would position the image in the top left corner, and a value of “10px 20px” would position the image 10 pixels to the right, and 20 pixels down.

Our first background-position is “0 0″ because the twitter icon is at the very top left of the image. This is where using very little spacing in our image comes in handy again, because we know each icon is 20px wide, and there’s no space between them. We know that we have to move the background image exactly 20px to the left in order to display the next icon. Remember that the first value of the background-position property represents distance FROM the left, so in order to move our background image TO the left, we need to give it a negative value (in this case -20px). We then move the background image a further -20 pixels for the final icon, and here we are – 3 different divs, displaying different icons, but all using the same background image!

Adding Hover Functionality

That’s all well and good, but it’s pretty standard, so let’s add some hover effects! First of all we’ll add 3 new icons to our Sprite Sheet which are slightly brighter than the original ones:

sprite

In order to use the hover effect we need to set 3 new background positions for our divs when people hover over them so that the background is moved up, as well as left. Remember once again that the distances are from the top and the left edges, so in order to move the image up, we have to give it a negative value.

.twitter:hover {background-position:0 -20px}

.facebook:hover {background-position:-20px -20px}

.tumblr:hover {background-position:-40px -20px}

Here we go, the same icons, but now with added hover effects, all still using one background image!

We’ve used div tags in these examples for the sake of ease, but it’s worth noting that all these techniques would also work with anchor tags if you wanted the icons to be clickable links.

Wrapping Things Up

Now that you understand how CSS Sprites work, you can study other Sprite Sheets (like YouTube’s) to get an idea of other things which are possible, or you could even look into some basic javascript animation to fade in and out between different background positions. Either way, hopefully you’ve learned something new, and your websites will load quicker and use less bandwidth as a result!