Create rounded corners and different shapes using CSS3

How to make rounded corners and other shapes like circle, heart, triangle etc using CSS3
If you are designing/developing a website, then basic elements will be HTML and CSS for sure apart from other biggie like jQuery, PHP/Perl blah blah. And page load into the browser also matters which is being determined by the size of images being used, js files, css files etc.

I still remember those days when I used to put images for every small shape like circle, rectangle, chat bubble, quotes, rounded corners etc. But using CSS3 you can not only save disk space but web space too!
There are few attributes in CSS through which you can do amazing stuffs. One of them is “border-radius.  This property allows us to add rounded corners to elements.

The border-radius property is a shorthand property for setting the four border-*-radius properties which mean you can specifically use top-left, top-right, bottom-right, bottom-left instead of that *

Its usual syntax is:

<br />
	border-radius: <em>1-4 length</em>|<em>%</em> / <em>1-4 length</em>|<em>%</em>;&nbsp;&nbsp;&nbsp;&nbsp; /* Here 1-4 means four corners 😀 */<br />

Either we can use length that we define using em or px or we can use percentage. How we can achieve various shape using this property. Let’s see rounded corner stuff first.
CSS Code:

<br />
	&lt;style type=&quot;text/css&quot;&gt;<br />
	&nbsp; #round-corner{<br />
	&nbsp; padding:10px 40px;<br />
	&nbsp; background-color:#FF0000;<br />
	&nbsp; width:300px;<br />
	&nbsp; border-radius:25px;&nbsp;<br />
	}<br />
	&lt;/style&gt;<br />

You could write border-raidus in em or percentage too even.
Something like:

<br />
	border-radius: 2em 1em 4em / 0.5em 3em;<br />

which is equivalent to:

<br />
	border-top-left-radius: 2em 0.5em;<br />
	border-top-right-radius: 1em 3em;<br />
	border-bottom-right-radius: 4em 0.5em;<br />
	border-bottom-left-radius: 1em 3em;<br />

Then write below code inside body tag:

<br />
	&lt;div id=&quot;round-corner&quot;&gt;Isn&#39;t it amazing to use border-radius property instead of rounded images?&lt;/div&gt;<br />

How the browsers interpret which em for whom if we missed any of those four corners?

Answer is simple, follow this rule:
The four values for each radii are given in the order top-left, top-right, bottom-right, bottom-left. If bottom-left is omitted it is the same as top-right. If bottom-right is omitted it is the same as top-left. If top-right is omitted it is the same as top-left.

How about having circle or oval shape using border-radius property?
You just need to include height, width in proper ratio and then  border-radius would do the rest. What I meant is :

	<style type="text/css">
	#circle {
	height: 70px;
	width: 70px;
	border-radius: 35px; /* or 50% */
	background-color: #FF0000;
	#oval {
	width: 200px;
	height: 100px;<br />
	background: #FF0000;<br />
	border-radius: 100px / 50px; /* or 50% */
	}<br />

Please check this link for fully working code and resulted shapes from these code snippets.

One task for you people

Can you make these shapes using border-radius property?

 Once you will be familiar with this I would write a post on :before and :after pseudo CSS properties. Then we will be able to make heart shape or infinity shape or even yin-yang shape (something like Symantec logo 😀 )

Note: It may not work in some older version of browsers specially IE! So, Test these codes in chrome or Firefox or safari

Sanjeev Jaiswal

He loves web programming and security and co-founder of Alien Coders. He usually shares and helps engineering students and IT professionals in academics and jobs. An avid reader and quick learner.

Share your comment

%d bloggers like this: