XML technologies / CSS / CSS Cookbook / Images / Rounded corners with JavaScript

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

Add rounded corners automatically without excessive extra markup or images.

Solution

Use the JavaScript Nifty Corners Cube by Alessandro Fulciniti.

First download the required source code as a ZIP containing JavaScript and CSS files.

Upload the files for Nifty Corners Cube to your web server and include its JavaScript element script with the attribute src in the target page:

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

Note: Unlike usual practice, CSS is not referenced directly here: JavaScript loads it automatically. Our files are in the directory js inside the folder _assets.

Give each element that should have rounded corners a unique value for the attribute id (see the next figure):

<div id="box">
  <h2>Marquee selectus</h2>
  <p>...</p>
</div>

Default column display

Figure: Default column display.

Round the element by calling the function Nifty from the included script, passing its ID and size information (see the next figure):

<script type="text/javascript" src="niftycube.js"></script>
<script type="text/javascript">window.onload=function( ) {Nifty("div#box","big");}</script>

Rounded corners appear automatically

Figure: Rounded corners appear automatically.

Discussion

Since Nifty Corners Cube works almost automatically, it is more a tool than a technique.

Different colors

JavaScript detects colors automatically, matching corners to the backgrounds of the element and its parent, normally the page’s element body. Developers can focus on choosing elements and corner sizes rather than colors.

Different sizes

Corner sizes in the Nifty Corners Cube script use four keywords: none, small, normal (default) and big. The value small means 2 pixels, normal means 5 pixels and big means 10 pixels. The value none disables rounding for the element.

For small rounded corners, use a JavaScript call like this:

<script type="text/javascript">window.onload=function( ) {Nifty("div#box","small");}</script>

Different elements

Nifty Corners Cube supports many selectors, simplifying element selection. The next table lists them.

Table: Selectors supported by Nifty Corners Cube.

Selector

Example

Type selector

"div"
"h3"

ID selector

"div#box"
"h3#main"

Class selector

"div.box"
"h3.box"

Child selector with ID

"div#box h3"
"h3#main div"

Child selector with class

"div.box h3"
"h3.main div"

Grouped selectors

"div, h3"
"div, h3.main div, p"

Round several elements at once with this call:

<script type="text/javascript">window.onload=function( ) {Nifty("div, h3.main div, p","small");}</script>

Particular corners

The script Nifty Corners Cube also lets designers round only particular corners. The next table lists keywords for choosing individual corners.

Table: Nifty Corners Cube keywords for particular corners.

Keyword

Meaning

tl

Top-left corner

tr

Top-right corner

bl

Bottom-left corner

br

Bottom-right corner

top

Both top corners

bottom

Both bottom corners

left

Both left corners

right

Both right corners

all (default)

All corners

To round only the top corners of several elements, write:

<script type="text/javascript">window.onload=function( ) {Nifty("div, h3.main div, p","small top");}</script>

See also

More information about the Nifty Corners Cube script.

  

 

 

 

back to top