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!

ShadedBorder - JavaScript Round Corners with Drop Shadow

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).

Version History

  • 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.

Tags: , , ,

307 Responses to “ShadedBorder - JavaScript Round Corners with Drop Shadow”

< Prev 1 28 29 30 31 Next > Show All

  • 281
    Mike V Says:

    My site has a div called “div_stage” that is populated by an AJAX call via the onLoad event of the page. Typically, the div is filled with a Login box. This login box has an id=”div_loginBox”.

    What is the proper way to apply the rounded corners Javascript to an element that has been loaded into a div way AFTER the original page has loaded? In my example, I would like to apply it to ‘div_loginBox’ which is loaded into ‘div_stage’ via an AJAX request.

    Thanks for sharing your creation with us and thanks in advance for any help you might be able to resolve my ignorance!
    (sorry, still a bit of a Javascript newbie…)

  • 282
    ebarra Says:

    I am using scriptaculous drag and drop and when I added ruzee the drag and drop stopped working.
    I have seen you had the same problem with ruzee.border but solved it, may be it is the same problem
    in the other hand your library is very good
    thank you

  • 283
    Adam Says:

    So, I’ve used this script for various small page sidebars and it’s worked great when i have only a few components like that on a page.

    Recently, however, I tried to put my entire page’s content area within a rounded corner DIV, and it is very slow loading and causes “flickering” in IE6 (especially when you hover over a link that changes color via CSS).

    So, I don’t think the script will work well if you want to put your entire page in a content area with rounded corners, namely for the reason in Post #228:

    “The div covers the entire content area and Firefox is obviously considering this div being made of hardened glass or something…”

    Basically, we’re creating a HUGE div and using up a lot of browser resources.

  • 284
    John Bruso Says:

    Hi All,

    I hope this hasn’t been asked a hundred times already, but I’m trying to figure out a way to use multiple boxes with shaddows on the same page. Can you tell me if this is possible? Sorry if it’s been covered already and I’m just overlooking it in the history.

    Thanks,

    John

  • 285
    Piotr Says:

    @John Bruso

    Look at the full featured example page’s source. It has multiple boxes…
    You can also use the $$ function of Prototype to create boxes having the same css class selectors. (See howto from Steffen on top of this page).

    Piotr

  • 286
    Piotr Says:

    For those (above) who have problems aligning elements vertically inside tables, that are somewhere deeper within the rendered element: Setting the element that is within the table cell explicitly to ‘position: static’ works for me.

    Piotr

  • 287
    Dannii Says:

    Hi all, well there so many comments here… can you believe I read through the whole lot to try and find a solution to my problem? And no one else had this problem. I have emailed Steffen but after reading all these comments and seeing that he wasn’t replying to alot of problems and others were that didn’t actually create the script I felt that it was probably best to ask here.

    On my website I want to use tabbed navigation. I tried Nifty Corners Cube and it wouldn’t allow me to have my own background. If I wanted a solid color background it may have worked. BUT my problem with this script (which I do love by the way!) is that I cannot find a way to have each tab on hover to have a different image to the last. (Did that sound right?)

    Basically you have the tabs - plain white in color (rounded obviously), on hover I want a different image for each tab.

    Now I believe it has something to do with the CSS and the ul id for the tabs.

    I have tried so many alternatives and still haven’t found a solution.
    here is the test url: http://deliciouz.org/blogtest.html

    It has a background image rounded for each tab but it hasn’t defined each image to each tab and plus there is a problem with the hover images on the tabs - theres a triangle in the right corners of a darker color…. please take a look.

    I hope someone can help me! Thank you in advanced.

    Dannii

  • 288
    Dannii Says:

    I forgot I uploaded a different file… the tabs are defined properly with rounded corners but the hove is rounded at all BUT has the different backgrounds that I would like rounded. Can anyone help?

  • 289
    Dannii Says:

    correction
    *But the hover isn’t rounded at all….

  • 290
    Dave Says:

    First of all, excellent bit of coding. I love the transparency (compared to NiftyCube, for example). I am having one problem (of course) with the tabs. I would like to apply the :hover style to a.active as well, so the the current page link can be displayed with the same background color as the hover link. The result, instead, is that the corners disappear (leaving a rectangular tab instead of a rounded one). I can set the font color, text decoration, etc, but setting background-color seems to switch the corners off. Any ideas (and thanks, either way)?

  • < Prev 1 28 29 30 31 Next > Show All

    Leave a Reply

53269