Steffen Hi, the name's Steffen and I'm writing about the Web, programming and all those things coming to my mind. Enjoy your stay.

I'm currently working on fabidoo.com - 3D Printing for everyone!

RUZEE.Borders - Round Corners with JavaScript

RUZEE.Borders has been replaced by RUZEE.ShadedBorder (which is currently in beta test - try it!).

RUZEE.Borders, formerly known as RuzeeBorders, is the first and only JavaScript library for rendering shadows and anti-aliased rounded corners inside a web browser without the designer needing to create background images. You can easily include it into your web site/blog etc. with only minor knowledge about JavaScript.

Features

  • Border style definition via CSS and CSS-like JavaScript
  • Anti-aliased rounded corners similar to the successors of Nifty Corners
  • A symmetric shadow similar to the ones of Mac OS X
  • Configurable border widths
  • Outside as well as inside background image support (gradients)
  • A kind of glow which is based on the shadow feature with a configurable color
  • A fade effect which lets the border color fade into the background color
  • All that at every edge/corner or only at some of them
  • Works on all modern browsers, even Opera and Konqueror
  • Colors and layout of the borders are defined in the CSS - if JavaScript is disabled your web site looks like its cubistic twin ;-)

Supported Browsers

These are the browser I’m testing my releases with (and others may work as well):

  • Internet Explorer 6.0
  • Firefox 1.5
  • Safari 2
  • Opera 9
  • KDE Konqueror 3.5

Usage

When rendering borders, CSS is used for all border style settings that can be definied via standard means. These are: border-width, border-color, etc. Here’s an example CSS fragment:

#myborder {
  border: 2px solid red;
  background: blue;
  padding: 10px;
}

If you have an HTML element with the ID ‘myborder’ in your HTML document, it will show up blue with a 3px wide, red solid border. This is the standard CSS way and works without RUZEE.Borders (of course ;-)). Make sure to always apply some padding, because RUZEE.Borders will “eat” some of it!

All style settings that are not available in the current CSS standard, i.e. everything that RUZEE.Borders need to round, shadow, glow, etc., have to be set via a CSS-like JavaScript code fragment in your page. Check out the tutorial or the example HTML file for details on how to do that (included in the download ZIP file). For now, have a look at the following examples - the borders you see are rendered by the definition inside - the value before the colon is a CSS selector, the values after it the style settings.

Draw a simple border with the corner radius of 4 pixels around the HTML element with the ID ‘rbex1′:

Draw a glowing border with an edge radius of 4 pixels and a blueish glow that has a width of 5 pixels around the elements with the ID ‘rbex2′:

Draw a border with a corner radius of 4 pixels and a shadow with the width of 5 pixels around the element with the ID ‘rbex3′:

Draw a border with a corner radius of 8 pixels that fades out to the color of the background around the element with the ID ‘rbex4′:

Draw a simple border, but round only the left/right bottom (’l'=left, ‘r’=right, ‘b’=bottom):

Draw a simple border, but round only the left on bottom/top bottom (’l'=left, ‘t’=top, ‘b’=bottom):

Draw a simple border, but round only the top left corner (’l'=left, ‘t’=top):

All borders can have a background image inside. It will be aligned to the top and can only be drawn inside the upper border. But together with repeat-x, this limitation isn’t as bad as it seems - it’s perfect for all those nifty Web 2.0 style gradients ;-). This example shows, that the image is really in the top of the border - clear gradients are possible this way.

Simple borders can be put on top of a background image using pseudo-transparency. I’ve decided to not use the CSS3 opacity or similar techniques due to the reasons described here, which means that you have to set a background color “similar” to the color of your background image:

As you can see, you do not need to specify any color values at all for the border, background, etc. only for the glow where there is no CSS style value that could be “misused” for that issue…

In the examples above, only ID based CSS selectors were used. The library included supports all the things you know from CSS and more (have a look at the credits section).

Download

Feel free to download RUZEE.Borders v0.16.1. The license is included in the archive, it’s MIT-style. Have fun playing around with the lib and if you have any suggestions, bug fixes, etc. please comment at the end of this page.

Credits

For the CSS-like selection of DOM elements, I’m using a CSS level 2 customized version of Dean Edwards’ cssQuery, thanks for this cool piece of code!

I’m also using a JavaScript CSS color converter that I stripped down to my requirements - cheers go to Stoyan Stefanov for that.

Tips and Tricks

To speed up the loading time of your page, especially if there are lots of images or IFRAME ads (e.g. Google), use the domload event of RUZEE.Events.

If you are using the Google Web Toolkit and want to add rounded corners to your site, check out the RUZEE.Borders based rounded corners component by Alexei Sokolov.

Internet Explorer displays a page very soon. If you want a blank page till RUZEE.Borders finished rendering, add the following lines to your page code:

CSS (hides HTML body when JavaScript is enabled):
* html body { display:expression('none'); }

HTML/body.onload():
RUZEE.Borders.render(function(){
  // rendering finished, now show the body
  document.body.style.display='block';
});

You can instantiate RUZEE.Borders.Border directly and call its render() method, if you know the DOM elements you want to put borders around. That’s ideal for 3rd party libs using RUZEE.Borders (e.g. GWT, hint!!! ;-)).

For “askew” corners, e.g. let only top-left and bottom-right be rounded, but the other two right angle, have a look at the Emad Workaround.

A nice design idea came from Simon - put your inner title on top of your corners.

Known Bugs and Limitations

Quirks Mode is not supported.

!!! Floated elements inside a rounded border have to be positioned relatively (i.e. style=”float:left; position:relative;”) !!! Not doing so will mess up Internet Explorer’s rendering completely (The Peekaboo Bug is responsible for that).

If you want a DIV with a fixed height, heights have to be specified via CSS and the height attribute of a RUZEE.Border (via RUZEE.Borders.add(...)), because the height of a DIV cannot be read from the CSS properties reliably on all browsers.

Konqueror has a bug when getting the styles for an element. RUZEE.Borders try to calculate the background color recursively until they reach an element with a non-transparent background. Konqueror unfortunately returns #000000 when the background is transparent. Hence #000000 is interpreted as transparent. If you want a black background using #010101 in your CSS is a possible workaround - assuming “almost black” is ok for you (see also my blog posting on that issue).

Version History

Check out the version history.

Tags: , , , , , , , , ,

211 Responses to “RUZEE.Borders - Round Corners with JavaScript”

< Prev 1 2 3 4 22 Next > Show All

  • 21
    Joern Says:

    This looks wonderful, here. I can’t get it to work with my blog.
    i included your script in my head.php but still no round corners. What is wrong?
    (I even tried to include your style.js too).

  • 22
    Steffen Says:

    Joern: You need some adaptations of the style.js - well basically your own style.js which renders all classes/IDs of your page with the right border settings. Some JavaScript knowledge is currently required for that. Have a look at the example.html file included in the release.

  • 23
    Dario Caregnato Says:

    Hi, what about adding a feature to set a background-gradient? Niiiiiiiiiiiiiice script! But put in evidence the possibility to set rounded corners by class ;) I didn’t notice it at first and was going to abandon your site! Ciao ciao

  • 24
    mike Says:

    Steffen, I found another difference between Safari and Firefox/I.E.

    I have a DIV that is set 0px to the right with a shadow border and no width specified. On Safari the script only makes the borders the width of the content but on Firefox and I.E. the script makes it the width of the whole page. I tried using both class and I.D. as well as different width attributes but can’t seem to get it to only box in the content.

    Here is my css

    #utility{
    font: tahoma;
    position: absolute;
    top: 5px;
    right: 0px;
    color: #666666;
    font-size: x-small;
    padding-right: 10px;
    background-color: #efefef;
    text-align: right;
    }

    Here is my site: http://www.hahncommunications.com/index.php

    Do you know of any why to work around this?
    Thanks
    -Mike

  • 25
    Steffen Says:

    Version 0.4 is out. It replaces 0.30… It includes Darios suggestion (official class support) and does no longer flip your page. Concerning mikes comment: There may be problems with absoulte positioned DIVs (or even not-static positioned). I’ll have a look on that.

  • 26
    Emad Says:

    Hi Steffen ,

    Good Job, thanks for this; I have two questions:-

    1-is it possible to round some of the four corners, e.g Left upper and left lower corners only ?

    2- is it possible to set the rounding radius ?

    thanx

  • 27
    Rick Says:

    Hi … Great Job!

    Don’t know if this is supposed to happen, but I discovered you can set a base class to set size and corners (e.g. rzCrShadowBorder(4,3).draw(’.someclass’)) and then use an additional class to set colors, width, etc … for each instance:

    .someclass { float: left; margin: 10px; padding: 0px; }
    .moreclass {width: 120px; background: white; border: 5px ridge red; }
    .lessclass {width: 160px; background: orange; border: 5px ridge blue; }

    Number 1 in someclass
    Number 2 in someclass

    The advantage, I think, is that one can set a consistent look (shadow, radius) with one onLoad call and then modify each instance in whatever way you want. Works in XP - Explorer 6, Firefox 1.5, Opera 8 …

    Any potential problems with this? Or, was this obvious to everyone but me in the fisrt place? :)

    thanks!

  • 28
    Rick Says:

    Oops …My ‘div’s’ got swallowed up (previous post). Here they are without their brackets …

    div class=”someclass moreclass”>Number 1 in someclass /div
    div class=”someclass lessclass”>Number 2 in someclass /div

    rick

  • 29
    Stephanie Says:

    Delightful script! One problem I’m having… my divs are in a container that has a background image, not a background colour, so the script is picking up the background of the div behind *that* for the space around the corners… which is not good :) Unfortunately it’s a multi-coloured background so there’s no real way for me to just set a background colour that closely matches or anything.

    Any ideas?

    Hope you had a nice vacation!

  • 30
    Steffen Says:

    Back from Bali - it was great!

    Emad: ad 1.: Planned for the next release, ad 2.: Can you explain what you mean with rounding radius? Cheers.

    Rick: I like that approach - it should always work - I expect no problems. Maybe it’s possible to generalize it even more, I’ll give it some thoughts…

    Stephanie: There are ways of doing it (via transparent colors), but one loses anti-alias support on some browsers. Maybe a future version will support borders over image backgrounds…

  • < Prev 1 2 3 4 22 Next > Show All

    Leave a Reply

67805