JavaScript
  Home arrow JavaScript arrow Page 5 - Building Rounded Corners With CSS and Java...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Building Rounded Corners With CSS and JavaScript
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 38
    2005-01-03

    Table of Contents:
  • Building Rounded Corners With CSS and JavaScript
  • Our old friendly tables
  • The CSS approach
  • Working with fixed boxes
  • Nesting div elements
  • Using the DOM approach

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building Rounded Corners With CSS and JavaScript - Nesting div elements


    (Page 5 of 6 )

    If applying four backgrounds images is our objective, a logical solution would be nesting four <div> elements, each containing the proper background, corresponding to every rounded corner. Doing it that way surely solves our problem quickly, but it introduces a lot of additional markup without adding any structural value.

    Here’s the markup for doing so:

    <div class="rounded"><div><div><div>
    Content Goes Here
    </div></div></div></div>

    And, the CSS code:

    <style type="text/css">
    .rounded {
      width: 300px;
      background: #00f url(tl.gif) no-repeat top left;
    }
    .rounded div {
      background: transparent url(tr.gif) no-repeat top right;
    }
    .rounded div div {
      background: transparent url(bl.gif) no-repeat bottom left;
    }
    .rounded div div div{
      background: transparent url(br.gif) no-repeat bottom right;
      padding: 40px;
    }

    </style>

    It’s pretty easy to know what’s going on with this technique. Each of the four divs applies a rounded corner background image, positioned in the top left, top right, bottom left and bottom right corners respectively. Also, we’ve set the width of the containing <div> to be 300px. But it could be easily switched to another, different value, probably in percentage, being useful and flexible enough for use with “liquid design” approaches. Our rounded corners should work without having to worry about the width of the containing <div>.

    The visual output is the following:

    Building rounded corners with CSS and JavaScript

    As we’ve done previously, we’ll show a simple diagram to demonstrate how this technique works:

    Building rounded corners with CSS and JavaScript

    Finally, we have found an acceptable solution to the problem. In comparison to the table based example, it uses far less markup. So, we’re happy, right? Not quite yet. While this is an acceptable approach, certainly, it’s not very efficient. We’re still adding three additional divs to the document, with no useful value to its structure. We need a better way to tackle the problem, and the answer is to look at the DOM (Document Object Model) and JavaScript, for dynamically altering a Web document’s structures. That’s the subject for the next section.

    More JavaScript Articles
    More By Alejandro Gervasio


       · As I've seen trough this article, building rounded corners that way is quite easy...
       · Nice article.I can see the smoothness in using JavaScript to make rounded corners,...
       · I liked the break-down a lot, and even though i think I'm gonna stay with the...
       · Hello,I think your point is good. The reason of having that Javascript design is...
       · Hello,Thank you for the comments. I agree about using only CSS for background...
       · the article is fine ...except for the need for gifs for each corner...i am sure...
       · Hi, Thnk you for the comment! I could have included the corner images, but I...
       · he was talking about the fact that you need .gif files at all to create this rounded...
       · Don't mean to cause problem, but right before your entry in google is...
       · If it's a decoration that your IE users can do without then you can make this easy....
       · Thanks for the comments on the article.Regarding the article that you...
       · Hi, I didn´t know about this technique. Thank you for sharing it. I think that it...
       · would be nice if he posted a solution how to create rounded corners without...
       · > Thnk you for the comment! I could have included the corner images, > but I...
       · Thank you for posting your comments here, and I hope you enjoy working with rounded...
       · What if your background image has a graient?
       · From the time I wrote this article, a few rounded corners techniques came up, which...
     

    JAVASCRIPT ARTICLES

    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in
    - Active Client Pages at the Server
    - ACP Tab Web Page







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT