CSS3圆角属性

1. Basic shapes

A. Square

This not exactly css3 but I thought we need to cover all shapes and actually this is mostly where all the other shapes will start from. In fact all other shapes will be square with tweaked corners or borders .. you will get the idea later …

1 .square{
2 display:block;
3 width:100px;
4 height:100px;
5 background-color:#ccc;
6 }

 

B. Rounded rectangular/square

Using border-radius you can specify the radius of the corner

In css3 the property is border-radius which specify the amount of curve for all corners or for specific corner , however older browser support different form of this property:

  • -moz-border-radius in Firefox
  • -webkit-border-radius older safari and Chrome
  • For you know who!!! :( .. Internet explorer none .. but this is a hack that you can find here “Css curved corner ” @ Google code . I think it will be supported by the IE9.

More about browser compatibility and comparison can be found in this very informative post by Jerry Seeger .

Back to the fun part ..

1 .rounded_corner{
2 display:block;
3 width:100px;
4 height:100px;
5 background-color:#344567;
6 -moz-border-radius:20px;
7 -webkit-border-radius: 20px;
8 border-radius: 20px;
9 }

 

This will draw a square with border radius =20px .

1 .rounded_corner2{
2 display:block;
3 width:100px;
4 height:100px;
5 background-color:#344567;
6 -moz-border-radius:20px 40px 0 0;
7 -webkit-border-radius: 20px 40px 0 0;
8 border-radius: 20px 40px 0 0;
9 }

In the previous example you can see that we can change the radius for each corner individually (top/Left ,top/Right , bottom/Right ,bottom/left ) .

 

C. Circle

1 background-color: #c06;
2 height: 100px;
3 width: 100px;
4 display:block;
5
6 -moz-border-radius:75px;
7 -webkit-border-radius: 75px;
8 border-radius: 75px;

A high radius will make the corners blend into a circle .

 1 .half_circle {
2 background-color: blue;
3 height: 50px;
4 width: 100px;
5 display:block;
6
7 -moz-border-radius:75px 75px 0 0 ;
8 -webkit-border-radius: 75px 75px 0 0 ;
9 border-radius: 75px 75px 0 0 ;
10
11 }

The trick to make half circle is to make the height = 1/2 width or width =1/2 height depends on the orientation of the shape in the above example the height is 50 and the width is 100 . The top right and left corners are curved so we can achieve half circle effect …

1 .q_circle {
2 background-color: green;
3 height: 50px;
4 -moz-border-radius:75px 0 0 0 ;
5 -webkit-border-radius: 75px 0 0 0 ;
6 border-radius: 75px 0 0 0 ;
7 width: 50px;
8 display:block;
9 }

With the quarter square the width and height can still be the same and only one corner is curved … Cool isn’t it :)

 

D. Trianlge

I found this cool and simple trick at Jon Rohan blog ..

Few people realize when a browser draws the borders, it draws them at angles. This technique takes advantage of that. One side of the border is colored for the color of the arrow, and the rest are transparent


1 .triangle {
2 border-color: red transparent transparent transparent ;
3 border-style:solid;
4 border-width:40px;
5 width:0;
6 height:0;
7 }

As we all know we have 4 borders the first one is red the other 3 are transparent .

More examples

 1 .triangle2 {
2 border-color: transparent yellow transparent transparent ;
3 border-style:solid;
4 border-width:40px;
5 width:0;
6 height:0;
7 }
8 .triangle3 {
9 border-color: transparent transparent green transparent ;
10 border-style:solid;
11 border-width:40px;
12 width:0;
13 height:0;
14 }
15
16 .triangle4 {
17 border-color: transparent transparent transparent orange;
18 border-style:solid;
19 border-width:40px;
20 width:0;
21 height:0;
22 }

 

 

2. More tools

 

More tools to help you with your drawing process ..

 

a. Transparency

 1 .trans {
2
3 /* for IE */
4
5 filter:alpha(opacity=50);
6
7 /* FF */
8
9 -moz-opacity:0.5;
10 /* CSS3 standard */
11 opacity:0.5;
12
13 }

b. Rotation

Rotating an element can help you in creating a third one . For example rotating a square can create a diamond shape.

 1 /* for firefox, safari, chrome, and others . */
2
3 -webkit-transform: rotate(-90deg);
4
5 -moz-transform: rotate(-90deg);
6
7 /* for IE */
8
9 filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3);
10 <pre><code>

According to snook.ca

In order to perform a transformation, the element has to be set to

1
display:block

The rotation property of the BasicImage filter can accept one of four values: 0, 1, 2, or 3 which will rotate the element 0, 90, 180 or 270 degress respectively.

C. Gradient

Another cool touches to your master piece .Here is a quick way

 1 Div{
2
3 /* For Older Browsers */
4
5 background: #fff;
6
7 /* For IE */
8
9 filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#000000');
10
11 /* For webkit Browser (Safari , Chrome .. etc) */
12
13 background: -webkit-gradient (linear, left top, left bottom, from(#fff), to(#000));
14
15 /* For firefox 3.6 and more */
16
17 background: -moz-linear-gradient(top, #fff, #000);
18
19 }

For older browsers that don't support css3 there will be one solid color .

filter:  progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff',  endColorstr='#000000');

For IE :) it's not css3 but it's the 2nd best thing .. A way to apply the gradient using filters .

background: -webkit-gradient (linear, left top, left bottom,  from(#fff), to(#000));

For safari and chrome .. etc use -webkit-gradient

  • we specified the gradient as a liner at the first parameter .
  • Left top : are the starting point
  • Left bottom : are the ending points .
  • from (#fff) : the first color
  • to(#000) : the 2nd color
background: -moz-linear-gradient(top,  #fff,  #000);

with firefox it's quite different

  • -moz-linear-gradient : This is means start a linear gradient
  • top: is the starting position .
  • #fff : is the first color.
  • #000 : is the last color

More info and options about the gradient property visit the art of web .

Examples

Those are fairly simple examples for demonstration purpose .

Let's have some fun..Happy face...

I haven't done stunning drawings like the Steve Dennis " Fail whale " but ..

Just like " Hello world " codes .. I will do happy face example .. which was 1 large circle head , 2 small Circles for the eyes and half circle for the mouth .

 1 .happyface {
2 background-color: yellow;
3 height: 100px;
4 -moz-border-radius:75px;
5 -webkit-border-radius: 75px;
6 border-radius: 75px;
7 width: 100px;
8 text-align :center;
9 position :relative;
10 }
11 .happyface b {
12 background-color: black;
13 height: 20px;
14 -moz-border-radius:75px;
15 -webkit-border-radius: 75px;
16 border-radius: 75px;
17 width: 20px;
18 display:block;
19 float:left;
20 margin-left:20px;
21 margin-top:25px;
22 }
23 .happyface i {
24 background-color: red;
25 height: 30px;
26 -moz-border-radius:0 0 75px 75px ;
27 -webkit-border-radius: 0 0 75px 75px;
28 border-radius: 0 0 75px 75px;
29 width: 50px;
30 display:block;
31 position :absolute;
32 bottom:12px;
33 left:24px;
34 }

and the css is

1 <div><strong> </strong><strong> </strong>
2 <em> </em></div>

the B tags are where the eyes are and the I tage is where the mouth is .

One more please .. My house ..

Another very basic example that I did was a house .OK this is the  Mom side of me .. when my kids start drawing I first teach them how to draw a face .. then a house .. then a car .This is just a habit that I can't break ..sorry!

The house is triangle  on top or a square .. 2 squares for the windows and a rectangle for the door and a shining sun  

 1 .frame{
2 background:blue;
3 padding:10px 10px 0 10px;
4 width:300px;
5 margin:0 auto;
6 text-align :center;
7 background: -moz-linear-gradient(top, #7AA9DD, #D9D9F3);
8 min-height:200px;
9 position:relative;
10 }
11 .house {
12 position:absolute ;
13 margin:0 auto;
14 display:block;
15 width:100px;
16 height:100px;
17 background: -moz-linear-gradient(top, #8B5A2B, #8B5A2B);
18 bottom:0;
19 left:100px;
20 }
21 .house:before {
22 border-color: transparent transparent #603311 transparent ;
23 border-style:solid;
24 border-width:50px;
25 width:0;
26 height:0;
27 display:block;
28 content:"\00a0";
29 position:absolute;
30 z-index:1;
31 top:-100px;
32
33 }
34 .house span{
35 display:block;
36 width:30px;
37 height:30px;
38 background-color:#FEE5AC;
39 float:right;
40 margin-right:14px;
41 margin-top:6px;
42 filter:alpha(opacity=50);
43 -moz-opacity:0.5;
44 -kcss-opacity: 0.5;
45 opacity: 0.5;
46
47 }
48 .house b{
49 display:block;
50 width:40px;
51 height:60px;
52 background-color:#FEE5AC;
53 position :absolute;
54 bottom:0;
55 right:30px;
56 filter:alpha(opacity=50);
57 -moz-opacity:0.5;
58 -kcss-opacity: 0.5;
59 opacity: 0.5;
60
61 }
62 .sun {
63 background-color: yellow;
64 height: 70px;
65 -moz-border-radius:75px;
66 -webkit-border-radius: 75px;
67 border-radius: 75px;
68 width: 70px;
69 display:block;
70 }

and the css

1 <div class="frame">
2
3 <span class="sun"> </span>
4 <div class="house"><span class="window"> </span>
5 <span class="window"> </span>
6 <strong> </strong></div>
7 </div>

































 

posted on 2012-03-24 23:05  咖啡戏  阅读(356)  评论(0编辑  收藏  举报