Click here to Skip to main content
Licence CPOL
First Posted 20 Jul 2011
Views 7,157
Downloads 479
Bookmarked 26 times

How to Round Corners Compatible with All Browsers (Intenet Explorer, Firefox, Chrome, Opera) easily

By | 20 Jul 2011 | Article
A very easy code that allows you to round corners
patt.JPG

Introduction

Recently, I downloaded an ASP.NET open source CMS that was very efficient and powerful.

Unfortunately, the provided HTML templates were so bland or strict. I would find an easy solution by adding the least code possible.

Finally, I found the brilliant work of Dave Methvin: the jQuery Corner library (based on Jquery framework library) which enables compatibility with all browsers.

Go to http://jquery.malsup.com/corner/.

I attached this page in PDF format at the root of the demo website.

This library is excellent because it gives you the possibility to put lots of beautiful shapes in the corner.

Background

I thought about how to use this library most easily?

JQuery is a very powerful framework that is able to read any tags and read all its attributes.

So my idea was to create a new attribute: "corner" to add to tags I want to round (divs tag in my examples). The possible values of this attribute are all the possible parameter values of its corner() method : "Bevel", "notch", "bite", "cool", etc.

So I added this attribute in all tags that I wanted:

<div id="div0" class="headermenu" corner="notch">
	Hi , 
	This is a simple test to see the corners with specials shapes.
	Hi , 
	This is a simple test to see the corners with specials shapes
</div>
		

<div id="div1" class="headermenu" corner="bite">
	Hi , 
	This is a simple test to see the corners shapes.<
	i , 
	This is a simple test to see the corners shapes
</div>
		

<div id="div2" class="headermenu" corner="10px">
	Hi , 
	This is a simple test to see the corners rounded.<br />
	i , 
	This is a simple test to see the corners rounded
</div>

Now I needed to program a simple function which gets only all the div tags of the HTML document with the "corner" attribute in order to apply the jquery corner() method.

So I created the file jquery.corner.executor.js to include:

//do this task when the DOM is loaded
$(document).ready(function() {

    // Watch all div in document (you can do the same for table tags)
    $('div').each(function(index) {
        
		//Is there a corner attribute in the current div tag ?
		var attr = $(this).attr('corner');

        //if current div has attribute corner , get the value
        if (typeof attr !== 'undefined' && attr !== false) 
		{
            //get current div id
            var cur_id = $(this).attr('id');

            //get the value : style to apply
            var cur_value = $(this).attr('corner');

            //set property value
            $('#' + cur_id).corner(cur_value);
        }
    });
});

Using the Code

We saw that it is very easy to use this plugin.

To summarise:

  1. Include this JavaScript in the header in good order:
    <!-- The older versions of jquery works too -->	
    <script type="text/javascript" src="js/jquery-1.4.min.js" />  
    <script type="text/javascript" src="js/jquery.corner.js" />   
    <script type="text/javascript" src="js/jquery.corner.executor.js" />
  2. Add the attribute corner="good_style" to your div.
  3. Thank Dave Methvin for his great job ;)

Points of Interest

It's always very interesting to do all the possibilities with jquery and all the great open source libraries built around it. It enables complex things to be done in an easy manner.

History

  • 19th July, 2011: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

David Zenou

Software Developer
Several
France France

Member

I am a Fan of dotnet technologies. I began to work with Java j2ee technologies and today i made my choice : .Net Framework is very pleasant , has excellents article codes and documentation.
I found very useful articles in codeproject , and today i want to give my contribution , and i hope that my articles will be useful.
 
Go to see my blog : http://davidzenou.blogspot.com/2009/01/david.html

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionNice additions Pinmemberjimibt0:30 27 Jul '11  
AnswerRe: Nice additions PinmemberDavid Zenou0:52 27 Jul '11  
QuestionWhy not use a css class? PinmemberDaniel Gidman7:18 20 Jul '11  
AnswerRe: Why not use a css class? PinmemberDavid Zenou13:38 20 Jul '11  
GeneralRe: Why not use a css class? PinmemberGary Noter11:12 25 Jul '11  
QuestionRewrote JS Pinmemberthinkdevcode3:56 20 Jul '11  
AnswerRe: Rewrote JS PinmemberDavid Zenou13:42 20 Jul '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 20 Jul 2011
Article Copyright 2011 by David Zenou
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid