CSS Cookbook
Authors: Schmitt C.
Published year: 2006
Pages: 71-72/235
Buy this book on amazon.com >>

Recipe 3.16. Rounding Corners ( Mountaintop Technique)

Problem

You want to create one set of graphics for rounded graphics while being able to display many background colors within the column.

Solution

Use the Mountaintop technique that was popularized by web designer Dan Cederholm.

Create a small graphic that will act as basis for the rounded corners (see Figure 3-38).

Figure 3-38. The top-left corner graphic

Note that the black color in Figure 3-38 will be set to transparent when the image is exported as a GIF.


Export the image as a GIF with the filename of corner_tl.gif .

Then rotate the image 90 degrees (see Figure 3-39) and export as a GIF image, naming it corner_tr.gif . Repeat the last two steps to create the bottom corners, corner_br.gif and corner_bl.gif .

Figure 3-39. Rotating the image 90 degrees

Add additional div elements around the column content:

<div id="box">
 <div id="head_outer">
  <div id="head_inner">
   <h2>
    I Met a Girl I&#8217;d Like to Know Better
   </h2>
  </div>
 </div>
 <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam 
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat 
volutpat. Ut wisi enim ad minim veniam.</p>
</div>

Then place the four corner graphics within the id and p selectors (see Figure 3-40):

div#box {
 width: 55%;
 background-color: #999999;
 background-image: url(http://flylib.com/books/3/27/1/html/2/corner_bl.gif);
 background-repeat: no-repeat;
 background-position: bottom left;
}
#head_outer {
 background-image: url(http://flylib.com/books/3/27/1/html/2/corner_tl.gif);
 background-repeat: no-repeat;
}
#head_inner {
 background-image: url(http://flylib.com/books/3/27/1/html/2/corner_tr.gif);
 background-repeat: no-repeat;
 background-position: top right;
}
div p {
 margin: 0;
 padding: 7px;
 font-family: Verdana, Arial, Helvetica, sans-serif;
 font-size: 1.1em;
 background-image: url(http://flylib.com/books/3/27/1/html/2/corner_br.gif);
 background-position: bottom right;
 background-repeat: no-repeat;  
 color: #333333;
 font-size: .8em;
 line-height: 1.5;
}

Figure 3-40. Mountaintop corner example

Discussion

The beauty of the mountaintop technique rests in its simplicity. Four small graphics are made with low file-sizes thanks to the GIF compression algorithms that are placed in the background of four block-level elements.

Also, there isn't the need to expand a couple of images to make sure the design integrity is maintained because the column resizes such as in the Solution for Recipe 3.15.

Plus, the Mountaintop technique allows for quickly changing the column's background color without revising the corner graphics as you see in Figure 3-41. However, the corner graphics will need to be changed if the background color of the web page or column's parent element changes.

Figure 3-41. The column maintains integrity even with the color change and resizing

See Also

Recipe 3.17 for automatically adding corners on columns without custom-made images.



Recipe 3.17. Rounding Corners with JavaScript

Problem

You want to include rounded corners on elements without the hassle of introducing new markup or images manually.

Solution

Use Nifty Corners Cube code by Alessandro Fulciniti.

First down the components of the Nifty Corners Cube solution, which include one CSS and one JavaScript file, at http://www.html.it/articoli/niftycube/NiftyCube.zip.

Upload both the JavaScript and CSS files associated with the Nifty Corners Cube solution. Then link the JavaScript to the web page by using the src attribute in the script element:

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

You won't link directly to the CSS file because the JavaScript file does that. Also, make sure to reference the JavaScript correctly. In this example, the JavaScript is located in the js folder that is placed in the _assets folder.


Next modify the markup that will have rounded corners (see Figure 3-42) by giving it a unique value in the id attribute:

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

Figure 3-42. Default rendering of the column

Next, make a separate JavaScript call to tell the browser which element gets the rounded corners and then define the size of the rounded corners (see Figure 3-43):

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

Figure 3-43. The rounded corners appear

Discussion

Since it's almost a completely worry-free method for creating rounded corners, the Nifty Corners Cube solution has been called more of a tool than a technique.

Different colors

Colors are detected automatically. The JavaScript automatically changes the colors to match the background color within the element as well as its parent element (usually the body of the web page). This means a developer only has to be worried with setting which element gets the curves and the size.

Different sizes

There are four keywords sizes written in to the Nifty Corners Cube JavaScript: none, small, normal (default), and big. Small is equal to the value of 2 pixels, normal is 5 pixels and big is 10 pixels.

For example, to adjust the corners so that they are small, the JavaScript call would look like:

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

Different elements

Nifty Corners Cube accepts numerous selectors making it easier to dictate which elements should receive rounded corners; the selectors are listed in Table 3-1.

Table 3-1. Selectors understood by Nifty Corners Cube JavaScript
Selector Example
Type
"div"
"h3"

id

"div#box"
"h3#main"

class

"div.box"
"h3.box"

Descendent with id
"div#box h3"
"h3#main div"

Descendent with class
"div.box h3"
"h3.main div"

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


For example, to apply rounded corners to multiple elements, the JavaScript function may look like this:

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

Specific corners

The Nifty Corners Cube also makes allowances for developers who may not want to apply rounded edges to all the corners. Table 3-2 lists the keywords that allow developers to single out which corner or corners to round.

Table 3-2. Keywords understood by Nifty Corners Cube JavaScript
Keyword Meaning
tl

Top-left corner
tr

Top-right corner
bl

Bottom-left corner
br

Bottom-right corner
top

Upper corners
bottom

Lower corners
left

Left corners
right

Right corners
all (default)

All the corners

For example, to apply rounded corners to just the top corners of multiple elements within a web page, the JavaScript function may look like the following:

<script type="text/javascript">
 window.onload=function(  ) {
  Nifty(

"div, h3.main div, p","small top"

);
}
</script>

See Also

For more information about Nifty Corners Cube at http://www.html.it/articoli/niftycube/index.html.


CSS Cookbook
Authors: Schmitt C.
Published year: 2006
Pages: 71-72/235
Buy this book on amazon.com >>

Similar books on Amazon