CSS Jelly Buttons

Jelly Fish

Jelly Fish

I keep seeing these nice looking jelly button images, but I was wondering if there’s a decent way of creating these jelly buttons without using images. Since CSS3 is now becoming popular, I decided to try to create one just using HTML and CSS3. So, if you would like to create a 3D looking jelly button like the one below, read on.

Jelly Button

  1. First, create the HTML with an anchor element. Let’s start with:

    <a href="#" title="My Cute Little Jelly Button">Jelly Button</a>
    

    Example:

  2. Ever notice that jelly buttons look like they have a smaller rectangle within a rectangle? Let’s add a span element inside the anchor element.

    <a href="#" title="My Cute Little Jelly Button"><span></span>Jelly Button</a>
    

    Example:

  3. Next, let’s add a class attribute to the anchor so that we can refer to it with CSS.

    <a class="jellybutton" href="#" title="My Cute Little Jelly Button"><span></span>Jelly Button</a>
    

    Example:

  4. Time to start the CSS! Let’s start by creating a box:

    <style type="text/css">
        a.jellybutton {
            position: relative;
            display: block;
            width: 150px;
            padding: 5px;
            border: 1px solid #555;
            background: #481525;
            color: #FFF;
            text-align: center;
        }
    </style>
    

    Example:

    Jelly Button

  5. Let’s add some spiffy CSS3 styles. By adding a linear gradient and rounding the corners, it will look more like a button.

    Creating rounded corners is pretty simple with the syntax for border-radius.

    You can use a gradient in the CSS anywhere where you would normally use an image, such as in a background. Below is the syntax. More information is available about this over at the Surfin’ Safari site. Below, we have designated a light color on the top and a dark color on the bottom.

    -webkit-gradient(<type>, <point> [, <radius>]?, <point> [, <radius>]? [, <stop>]*)

    We’re also using RGBA for the gradient. This currently only seems to work in Webkit browsers, like Safari and Google Chrome, and Firefox.

    <style type="text/css">
        a.jellybutton {
            position: relative;
            width: 150px;
            text-align: center;
            padding: 5px;
            display: block;
            border: 1px solid #555;
            background: #481525 -webkit-gradient(linear, 0% 0%, 0% 100%, from(rgba(255, 255, 255, 0.5)), to(rgba(0, 0, 0, 0.5)));
            color: #FFF;
            -webkit-border-radius:7px;
        }
    </style>
    

    Example:

    Jelly Button

  6. Now it’s starting to look a little three dimensional! The final step is styling the span to make it look jelly-like. Remember, we had used a gradient and border-radius on the anchor element? Well, we’re just doing the same thing here. The gradient is white on the top with a 0.5 alpha, which is like saying 50% opacity, and it is completely transparent at the bottom.

    <style type="text/css">
        a.jellybutton {
            position: relative;
            width: 150px;
            text-align: center;
            padding: 5px;
            display: block;
            border: 1px solid #555;
            background: #481525 -webkit-gradient(linear, 0% 0%, 0% 100%, from(rgba(255, 255, 255, 0.5)), to(rgba(0, 0, 0, 0.5)));
            color: #FFF;
            -webkit-border-radius:7px;
        }
        a.jellybutton span {
            position: absolute;
            top: 0;
            left: 2px;
            width: 156px;
            height: 50%;
            display: block;
            background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(rgba(255, 255, 255, 0.5)), to(rgba(255, 255, 255, 0)));
            -webkit-border-radius: 7px;
        }
    </style>
    

    Example:

    Jelly Button

Well, there you have it folks! A purely CSS jelly button — without the use of an image. I suspect that this might make page load a little quicker without having to download a resource for a jelly button image on the client.

Also, please remember that because of the use of CSS3, this currently only works in Webkit browsers, such as Safari and Google Chrome.

Let’s make my site faster

Image via Wikipedia

Image via Wikipedia

The other day, I was reading something that I saw on Ajaxian.com. It was an article about The End of Days for “View Source”. As I was reading through it, I saw a link to Google about Making the Web Faster. This got me thinking. I should probably make my own site faster. I mean, how can you make the web faster if you don’t work on making your own site faster first, right? So, over the next few weeks I plan on doing just that, making my site faster.

Stay tuned folks and see what comes out of this. Maybe I’ll end up getting a higher ranking in Google. ;)

Syncing friends between Twitter accounts

Recently, I’ve created a new Twitter account. The reason for this is that I’m thinking of having a public account instead of, or maybe along side of, a private account. So, I’m testing it out to see if having a public account is something I really want to do or not.

So, I created a new account. After adding my design and icon, I realized that I had to follow people. So, I started following the people I’m following on my private account. This quickly became a task that I did not enjoy. So, I decided to write a script for it to make it a bit easier.

Below is the Perl script I created to handle this.


#!/usr/bin/perl

use Net::Twitter;
use Getopt::Long;
use strict;

# set your own username and password here
my $namefrom = '';
my $passfrom = '';
my $nameto = '';
my $passto = '';
GetOptions('namefrom=s' => \$namefrom, 'passfrom=s' => \$passfrom, 'nameto=s' => \$nameto, 'passto=s' => \$passto);

my $twitfrom = Net::Twitter->new(username=>$namefrom, password=>$passfrom);
my $twitto = Net::Twitter->new(username=>$nameto, password=>$passto);

# establish friends
if($twitfrom->friends()) {
  foreach my $friend (@{ $twitfrom->friends() }) {
        $twitto->follow($friend->{'id'});
  }
}

print "friends are synced between accounts\n";

I saved this file as sync_following.pl. So, now all I have to do is call the script with the options for my two accounts.

$./sync_following.pl --namefrom strimble --passfrom redacted --nameto _strimble --passto redacted

You can download it here. You’ll also need Net::Twitter and Getopt::Long.

Perl scripts make my twitter accounts much simpler to deal with. Let me know if you end up using this script too, and if it was helpful or not.

CSS Gradients

A linear CSS gradient.

A linear CSS gradient.

Recently, I changed the index page of my site. One thing I added was a CSS linear gradient to the boxes that display content. I had a lot of fun trying out the CSS gradients and learning how to use them. I also learned that they’re so easy to create. I believe that they’re much better than just using images as gradients. I’m not a big fan of images anyway, unless they’re photographs. I try to keep the amount of images to a minimum on my site. A few reasons that I prefer not to use many images are they take more time to display than just straight HTML and CSS, it’s another file to load on the client, you have to use Photoshop to create them, adding an image as a gradient sometimes adds more unnecessary markup to the page (think the sliding doors technique).

Currently, the only browser supporting the CSS gradients that I’m about to explain is Safari 4 beta. I’m hoping that more browsers will support them in the future though. Another nice thing is that they degrade nicely, since they’re CSS. So, if a browser doesn’t support CSS gradients, it just won’t display the gradient. Also, you can use an image of a gradient as the background for browsers that don’t support CSS gradients.

So, I’ll explain how I created the CSS gradients on my index page. First, I have some divs on the page and I gave them a class name of “feed_result”. Next, I added some CSS to make the box look like what I want it to look like.

#content .column .feed_container .feed_result {
    margin: 1px 0 0;
    color: rgb(56, 56, 56);
    background-color: #656565;
    border: 1px solid rgb(96, 96, 96);
    border-radius: 3px;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
}

Then, I added the gradient. I used the linear CSS gradient, although there is also a radial one. I started with figuring out the colors that I wanted to fade into each other. These ended up being the background color of the content section, #555555, and the background color that I already had on the result box, #656565. Then, since I wanted these boxes to have a straight, vertical, linear gradient, I set the start point and end point to left top and left bottom. Then, I figured out where I wanted the color on top to switch over to the color on the bottom. I wanted this to be right in the middle. So, 0.5 is where I added the color stop and I used #555555 as the color. I’m just using this as the color stop for the example, but say that you want a gradient where the color on the bottom doesn’t start until the color on the top is 70% of the box. To create this, all you would need to do is change the stop from 0.5 to 0.7. You can use a decimal or percentage value for the stop. Also, you might want to have three colors in the gradient. So, to add another color in the middle, changed the color in the color-stop from #555555 to whatever color you want, like white. So, now, you might be asking why I’m even using a color-stop in my CSS. Well, the reason is that it visually pushes the color on the bottom down a bit. So, my gradient looks like this…

-webkit-gradient(linear, left top, left bottom, from(#555), color-stop(0.5, #555), to(#656565));

I then added this to the background in the CSS. The gradient can be used anywhere that you would normally use an image. So, now my CSS looks like this…

#content .column .feed_container .feed_result {
    margin: 1px 0 0;
    color: rgb(56, 56, 56);
    background-color: #656565;
    background: -webkit-gradient(linear, left top, left bottom, from(#555), color-stop(0.5, #555), to(#656565));
    border: 1px solid rgb(96, 96, 96);
    border-radius: 3px;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
}

So, as you can see, CSS gradients are kind of awesome. They’re easy to create, great for optimization by reducing file request for images, and they don’t make you add unnecessary markup to your pages.

Silicon Valley Geek Dinner

Silicon Valley Geek Dinners Logo

Come join us for the next Silicon Valley Geek Dinner. This one will be at Pluto’s in Palo Alto. Originally, I wanted to have this one at uWink in Mountain View, but it seems that they’ve closed. I’m expecting this to be a pretty small gathering, since I’m organizing it on such short notice. If there’s any geek subject that you would like to specifically discuss, please let me know in the comments. I hope to see you there! :)