ShadedBorder - JavaScript Round Corners with Drop Shadow

You should also check out my script Liquid Canvas which uses HTML canvas to render rounded corners with drop shadows.

Rounding corners with JavaScript has a long history. Everything started on 16th of March 2005 with Nifty Corners and loads of other libs followed.
RUZEE.ShadedBorder itself is the successor of RUZEE.Borders, which was the most feature-rich round corners library out there. But it wasn't quite easy to use, was slow, had some browser compatibility problems, etc.
ShadedBorders removes most of these limitations and comes with the following features:

  • JavaScript-only Photoshop(tm)-like rendering without external images
  • Round corners
  • Drop shadows
  • Glow effects
  • Gradient backgrounds
  • Graceful degradation - will look ok if JavaScript is turned off
  • Borders with different widths and semi-transparency
  • Full support for liquid designs
  • Anti-Aliasing
  • On-hover support
  • Disable some of the corners, e.g. bottom-left
  • Change borders on-the-fly
  • Real transparency - looks perfect on any background
  • Cross-Browser: Firefox, Internet Explorer (>=6.0), Safari, Opera (>=9.0)
  • Non-obstrusive
  • Leight-weight (8.8KB uncompressed)
  • Fast (0.5s for the example on a 2.2GHz machine)
  • No JavaScript library dependencies

Have a look at the full-featured example, the simple example, or just download the whole library as a ZIP archive (MIT licensed).

Comments have been closed - please use the Forum.

Also check out my Liquid Canvas script - it uses HTML canvas to do rounded corners and more and will be the successor of ShadedBorder once.

Version History

  • 2009-06-20 - v0.6.2: Fix 1px overlap on IE7 + IE8, speed-up
  • 2007-12-04 - v0.6.1: Fix transparency on IE7
  • 2007-12-02 - v0.6: Semi-transparent fat borders (thanks Ryan) and IE6 hover fixes (thanks NewTrax)
  • 2007-10-23 - v0.5: More robust DIV creation, add split borders example for headline/content (for Virsir)
  • 2007-10-21 - v0.4: Add graceful degradation, simplify usage, fix IE7(?)
  • 2007-10-20 - v0.3: Gradient background support, speed improvements
  • 2007-03-27 - v0.2: Fix IE: check boxes inside tables were disappearing
  • 2007-03-25 - v0.1: Initial release

How-to

Some steps are required to make your page use ShadedBorder:

Include ShadedBorder to your HTML header section:

<script src="shadedborder.js" type="text/javascript"></script>

Next, define an element you want to round. It is important that it does not directly contain text nodes, i.e. text must always be wrapped inside an HTML element (the "p" in this example):

<div id="round_me">
   <p>I want to be rounded!</p>
</div>

Then, in the header section, create the border object. You can define the radius of the round corners, the radius of the drop shadow and the width of the border.

var border = RUZEE.ShadedBorder.create({ corner:8, shadow:16,  border:2 });

Note that all parameters are optional, as long as you at least specify one of them. You may also define, which corners/edges should not be rounded. Have a look at the simple example source code for details.

The last thing to do is, to add some JavaScript code directly at the end of the HTML Body which will render the border around the element with the ID "round_me":

...  
<script type="text/javascript">
  border.render('round_me');
</script>

The radiuses and widths you specified for the border in the header section are one way of customization. The other way is to change colors. You do this in your CSS file:

#round_me, #round_me .sb-inner { background:red; }
#round_me .sb-shadow { background:blue; }
#round_me .sb-border { background:green; }

This will give your border a red "inner-style", a blue shadow and a green border - don't ever use this color combination - it will look extremely ugly ;-)
The first line is important for graceful degradation (new from v0.4 on): the background for your .sb-inner class must also be set on the element itself - but only for .sb-inner, NOT for .sb-shadow or .sb-border!

Adding some hover effect also happens in your CSS file. The following code snippet will change the "inner-style" of your border to purple, once you hover with your mouse over the #round_me element (hey purple will make this beast look even uglier - yuk!).

#round_me:hover, #round_me:hover .sb-inner { background:purple; }

Of course, this will work with ".sb-border" and ".sb-shadow" as well.

Tips

You can use RUZEE.ShadedBorder with the JavaScript library of your choice to get support for CSS-Selectors (e.g. using Prototype, jQuery, etc.). Here's an example using the $$ function of Prototype to round all child elements of the element with the ID "header" that have the class "tab":


border.render($$('#header .tab'));

Limitations

  • Safari 2 seems to get pretty slow when showing ShadedBorder. I'm looking for people with a Mac and some JavaScript knowledge to speed things up. If you feel like you can help, please contact me via my contact form. Thanks!
  • Since a ShadedBorder adds DIV elements to the element you round, those elements cannot be UL, OL, TABLE, TR, etc.. Allowed are DIV, LI, TD, TH, i.e. those elements that are allowed to directly contain DIV elements.

Comments have been closed - please use the Forum.

Version 0.6.1 fixes the transparency

Version 0.6.1 fixes the transparency problems on IE7

Does changing "isie6" to "isie" in the

Does changing "isie6" to "isie" in the op() function solve the problem? If so, I'll release a fix asap.

Hi Nick - right, the gradient is not

Hi Nick - right, the gradient is not supported on the bottom and they're only supported top to bottom... in any case make sure the gradient ends before the bottom and work with the background color (white in your case).

I've just released version 0.3 which

I've just released version 0.3 which now "officially" supports background gradients - get it while it's hot ;-)

@mytradex2: Thanks alot for your

@mytradex2: Thanks alot for your suggestions! Performance is one issue - usability is another. As for the while loop, that removes the generated elements I think that it shouldn't cost that much performance. It will just iterate over the direct child elements of the element to round - which are usually 1-20 when there are no borders and 7 more (the generated children), if there are borders already. Hence I don't recommend your change for usability reasons - so the developer doesn't have to care about whether she already rounded an element or not.

Btw.: I'm still looking for a way to speed up Safari... and I'm still looking for some hints... help! ;-)

Thanks all - especially mytradex2 - for

Thanks all - especially mytradex2 - for your comments. Setting height to 100% will also not work in Safari (comment 43). Your speed improvements are nice - how much performance do you gain?

I did some more investigations with Safari and it looks like it's a problem with the performance of Safari when rendering sites that contain a lot of opacity (which is the case for pages using ShadedBorder).

This thread sound pretty familiar: http://forums.macnn.com/82/applications/331952/safari-myspace-problem/

And this is the myspace page they took as example: http://www.myspace.com/index.cfm?fuseaction=user.viewprofile&friendID=11...

@MArcio Amorim: Version 0.2 should fix

@MArcio Amorim: Version 0.2 should fix that problem.

@mytradex2: the render() method is

@mytradex2: the render() method is synchronous. You don't need a callback.

@Valerio Schiavoni: There are obviously issues with Safari...

@Chris: Yep, that's on the roadmap. I'm

@Chris: Yep, that's on the roadmap. I'm right now searching for a simple solution for the site designer. Everything I have in mind right now is a bit ... too much "writing efforts".

corner and shadow are different in

corner and shadow are different in ShadedBorder - I think I need to do a tutorial for that - both start at the center so if you set both to 8 they'll be "the same" - maybe this causes the problem. I need to check that. Try to set the shadow to about twice the radius of the corner and see if this fixes things. Oh and I've seen that my previous hint did the trick, right?

I'm doing my tests with Safari 2.0

I'm doing my tests with Safari 2.0 (412) - works ok. Can anyone confirm that 2.0.4 has problems?

Hi mytradex2, cool that you give the

Hi mytradex2,

cool that you give the "new RUZEE.Borders" a try ;-) And good to hear that $$ also works with mootools.

Please try to add the class "sb" to your subtitle as well, e.g. <div class="subtitle sb"> ... </div>, this should do the trick.

The glow border can be done, e.g. via var glowBorder = RUZEE.ShadedBorder.create({ shadow:10 });, i.e. simply don't specify any border or corner.