一.CSS3 Rounded Corners
通過使用CSS3 border-radius屬性, 可以得到圓角的效果。
#rcorners {
border-radius: 25px;
background: #73AD21;
padding: 20px;
width: 200px;
height: 150px;
}

顯示效果如下,此時四個方向的圓角都會被設(shè)置。
如果想單獨設(shè)置4個方位的圓角:
- border-top-left-radius
- border-top-right-radius
- border-bottom-right-radius
- border-bottom-left-radius
二.CSS3 Border Images
通過使用CSS3 border-image 屬性,可以將一幅圖片設(shè)置成為一個元素的邊框。
看下面一段代碼:
#myDIV {
border: 10px solid transparent;
padding: 15px;
border-image: url("border.png") 30 round;
width: 100px;
height: 100px;
}
其中 boredr.png 是下面這幅圖,大小為81*81:

顯示效果如下:

注意:為了使 border-image 正常生效,需要設(shè)置元素的 border 屬性。
border-image 屬性是以下幾個屬性的簡寫:
- border-image-source:指定圖片的位置。
- border-image-slice:指定圖片的裁剪方式。
- border-image-width:指定圖片的顯示寬度。
- border-image-outset:指定圖片相對于邊框的偏移位置。
- border-image-repeat:指定四條邊框的圖片是拉伸還是重復(fù)。
其中 border-image-slice 可以參考下面這篇文章:
http://blog.sina.com.cn/s/blog_5f2389f90102vks0.html
三.CSS3 Backgrounds
- CSS3允許多個背景圖片,有以下兩種寫法:
- 一次性設(shè)置所有的背景圖片和屬性
#example1 {
background: url(img_flwr.gif) right bottom no-repeat, url(paper.gif) left top repeat;
}
- 單獨設(shè)置每個背景圖片的屬性
#example1 {
background-image: url(img_flwr.gif), url(paper.gif);
background-position: right bottom, left top;
background-repeat: no-repeat, repeat;
}
-
CSS3 Background Size
在 background-size 出現(xiàn)之前,背景圖片的大小是無法修改的。它是圖片的實際大小。
CSS3 background-size 屬性允許你設(shè)置背景圖片的大小。
#div1 {
background: url(img_flower.jpg);
background-size: 100px 80px;
background-repeat: no-repeat;
}
background-size 有兩個可選的值 contain 和 cover
contain:背景圖片的大小盡可能的放大,但是不會超過容器的大小,所以容器有一部分可能沒有被背景圖片覆蓋。
#myDIV {
border: 10px solid transparent;
padding: 15px;
border-image: url("border.png") 30 round;
width: 100px;
height: 50px;
background: url("border.png") no-repeat;
background-size: contain;
}

cover :背景圖片的大小盡可能的放大,放大到將容器完全覆蓋為止。
#myDIV {
border: 10px solid transparent;
padding: 15px;
border-image: url("border.png") 30 round;
width: 100px;
height: 50px;
background: url("border.png") no-repeat;
background-size: cover;
}

background-size 也可以同時指定多個背景圖片的大小:
#example1 {
background: url(img_flwr.gif) left top no-repeat, url(img_flwr.gif) right bottom no-repeat, url(paper.gif) left top repeat;
background-size: 50px, 130px, auto;
}
-
Full Size Background Image
如果我們想要實現(xiàn)一個網(wǎng)頁上固定的背景圖片,那該怎么辦呢?
html {
background: url(img_flower.jpg) no-repeat center fixed;
background-size: cover;
}
注意其中的 fixed,這個屬性是 background-attachment 屬性的一個取值。
-
CSS3 background-origin Property
background-origin 屬性指定背景圖片的位置,它又下面三種取值:
- border-box:背景圖片從左上角的邊框開始。

- padding-box(默認(rèn)):背景圖片從padding的左上角開始。

- content-box:背景圖片從內(nèi)容的左上角開始。

-
CSS3 background-clip Property
background-clip 屬性指定了背景圖片的填充區(qū)域,有以下三種取值:
- border-box(默認(rèn)):背景填充至邊框。
- padding-box:背景填充至padding。
- content-box:背景填充至content。
四.CSS3 Gradients
-
CSS3 Linear Gradients
語法:
background: linear-gradient(direction, color-stop1, color-stop2, ...);
Linear Gradient - Top to Bottom (this is default)
#myDIV {
width: 250px;
height: 200px;
background: red; /* For browsers that do not support gradients */
background: -webkit-linear-gradient(red, yellow); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(red, yellow); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(red, yellow); /* For Firefox 3.6 to 15 */
background: linear-gradient(red, yellow); /* Standard syntax */
}

Linear Gradient - Left to Right
#myDIV {
width: 250px;
height: 200px;
background: red; /* For browsers that do not support gradients */
background: -webkit-linear-gradient(left, red , yellow); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(right, red, yellow); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(right, red, yellow); /* For Firefox 3.6 to 15 */
background: linear-gradient(to right, red , yellow); /* Standard syntax */
}

Linear Gradient - Diagonal(對角線)
#myDIV {
width: 250px;
height: 200px;
background: red; /* For browsers that do not support gradients */
background: -webkit-linear-gradient(left top, red, yellow); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(bottom right, red, yellow); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(bottom right, red, yellow); /* For Firefox 3.6 to 15 */
background: linear-gradient(to bottom right, red, yellow); /* Standard syntax */
}

為了更加靈活的控制漸變方向,我們還可以自定義漸變角度。
語法:
background: linear-gradient(angle, color-stop1, color-stop2);
#myDIV {
width: 250px;
height: 200px;
background: -webkit-linear-gradient(0deg, red, yellow); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(0deg, red, yellow); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(0deg, red, yellow); /* For Firefox 3.6 to 15 */
background: linear-gradient(0deg, red, yellow); /* Standard syntax (must be last) */
}

#myDIV {
width: 250px;
height: 200px;
background: -webkit-linear-gradient(90deg, red, yellow); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(90deg, red, yellow); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(90deg, red, yellow); /* For Firefox 3.6 to 15 */
background: linear-gradient(90deg, red, yellow); /* Standard syntax (must be last) */
}

不難發(fā)現(xiàn)角度的規(guī)律。

Using Transparency(透明度)
需要使用透明度的話,需要使用rgba()函數(shù)來定義顏色,該函數(shù)的最后一個參數(shù)用來指定顏色透明度。
#myDIV {
width: 250px;
height: 200px;
background: red; /* For browsers that do not support gradients */
background: -webkit-linear-gradient(left,rgba(255,0,0,0),rgba(255,0,0,1)); /*Safari 5.1-6*/
background: -o-linear-gradient(right,rgba(255,0,0,0),rgba(255,0,0,1)); /*Opera 11.1-12*/
background: -moz-linear-gradient(right,rgba(255,0,0,0),rgba(255,0,0,1)); /*Fx 3.6-15*/
background: linear-gradient(to right, rgba(255,0,0,0), rgba(255,0,0,1)); /*Standard*/
}

Repeating a linear-gradient
#myDIV {
width: 250px;
height: 200px;
background: red; /* For browsers that do not support gradients */
/* Safari 5.1 to 6.0 */
background: -webkit-repeating-linear-gradient(red, yellow 20%, green 30%);
/* Opera 11.1 to 12.0 */
background: -o-repeating-linear-gradient(red, yellow 20%, green 30%);
/* Firefox 3.6 to 15 */
background: -moz-repeating-linear-gradient(red, yellow 20%, green 30%);
/* Standard syntax */
background: repeating-linear-gradient(red, yellow 20%, green 30%);
}

其中顏色屬性中的百分比指的是顏色停止位置占容器長度的百分比值。
- CSS3 Radial(徑向) Gradients
語法:
background: radial-gradient(shape size at position, start-color, ..., last-color);
Radial Gradient - Evenly Spaced(間隔均勻) Color Stops (this is default)
#myDIV {
width: 250px;
height: 200px;
background: red; /* For browsers that do not support gradients */
background: -webkit-radial-gradient(red, yellow, green); /* Safari 5.1 to 6.0 */
background: -o-radial-gradient(red, yellow, green); /* For Opera 11.6 to 12.0 */
background: -moz-radial-gradient(red, yellow, green); /* For Firefox 3.6 to 15 */
background: radial-gradient(red, yellow, green); /* Standard syntax */
}

Radial Gradient - Differently Spaced Color Stops(自定義間隔)
#myDIV {
width: 250px;
height: 200px;
background: red; /* For browsers that do not support gradients */
background: -webkit-radial-gradient(red 5%, yellow 15%, green 60%); /* Safari 5.1-6.0 */
background: -o-radial-gradient(red 5%, yellow 15%, green 60%); /* For Opera 11.6-12.0 */
background: -moz-radial-gradient(red 5%, yellow 15%, green 60%); /* For Firefox 3.6-15 */
background: radial-gradient(red 5%, yellow 15%, green 60%); /* Standard syntax */
}

Set Shape(定義漸變中心形狀)
1.circle
2.ellipse(默認(rèn))
Repeating a radial-gradient
#grad {
background: red; /* For browsers that do not support gradients */
/* For Safari 5.1 to 6.0 */
background: -webkit-repeating-radial-gradient(red, yellow 10%, green 15%);
/* For Opera 11.6 to 12.0 */
background: -o-repeating-radial-gradient(red, yellow 10%, green 15%);
/* For Firefox 3.6 to 15 */
background: -moz-repeating-radial-gradient(red, yellow 10%, green 15%);
/* Standard syntax */
background: repeating-radial-gradient(red, yellow 10%, green 15%);
}
五.CSS3 Shadow Effects
- CSS3 Text Shadow
h2 {
text-shadow: 2px 20px;
}

可以看出第一個長度參數(shù)控制的是陰影的水平位置,第二個參數(shù)控制的是陰影的垂直位置。
h2 {
color: white;
text-shadow: 2px 2px 4px #000000;
}

第三個參數(shù)控制的是陰影的特效,第四個參數(shù)可以自定義陰影顏色。
多個陰影
h2 {
color: white;
text-shadow: 1px 1px 2px black, 0 0 25px blue, 0 0 5px darkblue;
}

h2 {
color: yellow;
text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
}

-
CSS3 box-shadow Property
語法:box-shadow: h-shadow v-shadow blur spread color inset
| 值 | 描述 |
|---|---|
| h-shadow | 必需,陰影的水平位置 |
| v-shadow | 必需,陰影的垂直位置 |
| blur | 可選。模糊距離。 |
| spread | 可選。陰影的尺寸。 |
| color | 可選,陰影的顏色。 |
| inset | 可選,將外部陰影 (outset) 改為內(nèi)部陰影。 |
用box-shadow實現(xiàn)一個常用的卡片效果
<div class="card">
<div class="header">
<h1>1</h1>
</div>
<div class="container">
<p>January 1, 2016</p>
</div>
</div>
div.card {
width: 250px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
text-align: center;
}
div.header {
background-color: #4CAF50;
color: white;
padding: 10px;
font-size: 40px;
}
div.container {
padding: 10px;
}
效果如下:

六.CSS3 Text
-
text-overflow
這個屬性控制當(dāng)你的文字超出容器時該如何顯示。
text-overflow: clip;

text-overflow: ellipsis;

-
word-wrap
word-wrap:break-word屬性使得一個很長的文字自動換行。 -
word-break
word-break屬性指定了換行的規(guī)則。
- keep-all:只在連字符處換行
- break-all:默認(rèn)換行。
七.CSS3 Web Fonts
Web字體允許Web設(shè)計人員使用未安裝在用戶計算機上的字體。
@font-face {
font-family: myFirstFont; //自定義字體名字
src: url(sansation_light.woff); //字體文件路徑
}
div {
font-family: myFirstFont;
}
八.CSS3 2D Transforms
- translate()
- rotate()
- scale()
- skewX()
- skewY()
- matrix()
1. The translate() Method
translate()方法可以使得元素相對于當(dāng)前的位置水平和垂直移動。
div {
-ms-transform: translate(50px, 100px); /* IE 9 */
-webkit-transform: translate(50px, 100px); /* Safari */
transform: translate(50px, 100px);
}
移動之后的元素還是占據(jù)其之前在文檔中的位置。
2.The rotate() Method
rotate()方法可以使得元素旋轉(zhuǎn)一定角度。
div {
-ms-transform: rotate(-20deg); /* IE 9 */
-webkit-transform: rotate(-20deg); /* Safari */
transform: rotate(-20deg);
}

3.The scale() Method
scale()方法可以使得元素在寬度和高度方向伸長或者縮小。
div {
-ms-transform: scale(2, 3); /* IE 9 */
-webkit-transform: scale(2, 3); /* Safari */
transform: scale(2, 3);
}
4.The skewX() Method
skewX()方法根據(jù)給定的角度使得元素在X軸方向傾斜。
div {
-ms-transform: skewX(20deg); /* IE 9 */
-webkit-transform: skewX(20deg); /* Safari */
transform: skewX(20deg);
}

5.The skewY() Method
skewY()方法根據(jù)給定的角度使得元素在Y軸方向傾斜。
6.The skew() Method
前兩個方法的結(jié)合
7.The matrix() Method
matrix()是將之前的六個方法結(jié)合在一起:
matrix(scaleX(),skewY(),skewX(),scaleY(),translateX(),translateY())
九.CSS3 3D Transforms
- rotateX():將元素沿X軸旋轉(zhuǎn)一定角度。
- rotateY():將元素沿Y軸旋轉(zhuǎn)一定角度。
- rotateZ():將元素沿Z軸旋轉(zhuǎn)一定角度。
詳細(xì)參考鏈接:https://www.w3schools.com/css/css3_3dtransforms.asp
十.CSS3 Transitions
CSS3 transitions 可以使的屬性的變換更加平滑。
div {
transition-property: width; //指定要應(yīng)用的屬性
transition-duration: 2s; //指定時間間隔
transition-timing-function: linear; //指定過度類型
transition-delay: 1s; //指定延遲時間
}
transition-timing-function取值如下:
- ease - specifies a transition effect with a slow start, then fast, then end slowly (this is default)
- linear - specifies a transition effect with the same speed from start to end
- ease-in - specifies a transition effect with a slow start
- ease-out - specifies a transition effect with a slow end
- ease-in-out - specifies a transition effect with a slow start and end