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>; /* Here 1-4 means four corners :D */<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 /> <style type="text/css"><br /> #round-corner{<br /> padding:10px 40px;<br /> background-color:#FF0000;<br /> width:300px;<br /> border-radius:25px; <br /> }<br /> </style><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 /> <div id="round-corner">Isn't it amazing to use border-radius property instead of rounded images?</div><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 /> </style>
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?
- Triangle with different point of origins
- Circle with two different colors , one quarter filled with x color and other quarter filled with y alternatively
- Parallelogram/Trapezoid
- Bookmark-ribbon with one side red color and other side black color (Hint: it can be done without border-radius)
Note: It may not work in some older version of browsers specially IE! So, Test these codes in chrome or Firefox or safari