Rounded Corners With jQuery & CSS

There is a time just around the corner (so to speak), when we will take a shameful glance at how we used to fudge, hack and force rounded corners into our web pages. That time is not today. So I’m going to show you how I fudge corners, using a bit of jQuery, some CSS and a .gif image.

Creating a rounded corner using pure CSS is covered in detail here. However IMO using rules that are specific to individual browsers, isn’t a real step forward, and sets a dangerous precedent, (imagine if Microsoft started to introduce CSS for just Internet Explorer), so whilst I appreciate the effort Mozilla and the guys behind Webkit (the engine behind Safari and Chrome) have gone to, I’m going to wait for an accepted standard rule before I start using CSS to style my corners.

In the mean time, Here’s an Example What We’re Going to Make:

three boxes with different background colors, and different sizes, all with neat little rounded corners, and here’s how to do it:

Step 1 – Create Your Corner

  1. Open Fireworks, or your favourite graphic app
  2. Create a new file, 10px x 10px.
  3. Make a circle 10px x 10px, and give it a hard stroke of 1px in #ccc (or whatever colour you want your border to be), do not fill the circle – leave it empty.
  4. Next, fill the background of your image with the background colour of your page – Do not fill the space inside the circle
  5. Finally export your image as a gif called corner.gif (or whatever you prefer), making sure the transparency is set to ‘index’.

Step 2 – the CSS

So that jQuery can select all the elements on the page that we want to give rounded corners to, we’re going to create a class called ‘corners’, and give it the CSS the property:


.corners {
   position:relative;
   }

Giving it relative position will allow us to absolutely position the corners inside the element as oppose to inside the document.

Next, create rules for the divs that will become the corners, I’ve used abbreviations of top Left = tl etc:


.tl, .tr, .br, .bl {
   position:absolute;
   width:5px;
   height:5px;
   /* ! the width & height is half the width of our circle gif,
    showing only a quarter of the image */

   overflow:hidden; /* ! Important for IE6 */
   background-image:url(../yourimagefolder/corner.gif);
   }

/* ! position each corner, and its background image */

 .tl {top:-1px;left:-1px;background-position:0 0 ;}

 .tr {top:-1px;right:-1px;background-position:-5px 0;}

 .br {right:-1px;bottom:-1px;background-position:-5px -5px;}

 .bl {left:-1px;bottom:-1px;background-position:0 -5px;}

Finally, (making sure you have linked to the jQuery library) a small amount of jQuery will assign four divs, with classes of tl, tr, br and bl to any element with a class of ‘corners’:


<script type="text/javascript">
    $(document).ready(function() {
    $('.corners').append('<div class="tl"></div><div class="tr">
    </div><div class="br"></div><div class="bl"></div>');
});
</script>

And that’s it, until you start using CSS to childproof your corners, have fun fudging them with css & jQuery.

Non-Cross Browser Compatible Billboards

Here at Binary-Scenery, I take every care to ensure that all my work is cross-browser compliant… that doesn’t mean I don’t have my favourite browsers though. So I found myself very excited to see this billboard on the main road near my house yesterday:

The Google Chrome Advertising Campaign

The Google Chrome Advertising Campaign

The truth is that there are thousands of forum threads out there all dealing with a single topic: How do I make my website work in Internet Explorer? For many people this comes as a surprise, but Microsoft don’t make amazing browsers. While most web designers can breath a sigh of relief that the current version (IE8) is finally compliant with CSS 2.1, the rest of the browsers are busily implementing support for CSS3.

A simple example of where you will see this difference is in a wordpress dashboard. If you are using Firefox, Chrome or Safari, you will notice that all the corners have nice little rounded edges. If you are working in Internet Explorer you will notice all the corners are hard angles. Why? because to achieve rounded corners in Internet Explorer means using a tiny picture of a rounded corner and setting it as the background of a small box. On every corner of the page. All the other browsers respond to a variation of the command: ‘border-radius:4px;’.

So, after all this time IE8 is still two steps behind, and the web design community waits patiently for IE9. So why do so many people still use Internet Explorer? All of the above browsers are completely free, have a much better history of security, support cutting edge web standards and show the user a better looking web. The answer is simply that Internet Explorer comes in the box, and the majority of people aren’t web designers and don’t really think about their browser as a choice.

Google to the rescue?

Because of the above, Sony have taken the decision not to fit their laptops with Internet Explorer (source), which is a great step forwards, but I still believe that if Firefox, Chrome and Safari want to make web standards something that really matters, it isn’t enough to simply make a great product and market it on the web, it needs to be marketed where people who aren’t web designers, and don’t participate in forum threads with titles like “Which browser has the best support for CSS3″. It needs to reach the wider public.

So I’m applauding Google for putting a billboard outside my house. The more people that see it the better. As for my two pence on which browser to use, well it depends what you want from the web. If your a mac user, Safari is a great product, if your a web designer Firefox not only has the strictest adherence to modern CSS standards, but also comes with powerful add-ons like Firebug that will show you how the web is put together, and if your a general browser nothing beats the incredibly intuitive design and sheer speed of Google Chrome, which seems to open before I’ve finished clicking on my home machine.

So if your still using Internet Explorer, consider one of these instead, it won’t cost you a penny, and will make a web designer somewhere in the world sleep a little easier: Google Chrome, Mozila Firefox, Safari

Finally Going Freelance!

Earlier this year I decided to leave the teaching profession in order to pursue a freelance career as a web designer, and I’ve finally made it! My stationery has arrived from the bank (complete with the wrong business name), the Binary-Scenery site is almost finished, and my fingers are itching to get coding. My first job has been with the wonderful guys at Elite Business Services, where I have been busily re-building and designing their site to modern standards. I’ve had a great time with Elite, it was a job I was almost sorry finish as the guys and gals in the company are a pleasure to work with. On the plus side it really re-affirmed my ambitions as a web designer, and made me hungry for the next big challenge.

It’s been slightly daunting moving from the routine and solidity of education into the perilous world of the freelancer, especially as the financial situation isn’t exactly rosey just yet, but so far I’ve loved every minute. My next hurdle is to finish off the Binary Scenery website so I can start marketing proper, and drum up more business, so if you need a website…