CSS3新特性

一.CSS3 Rounded Corners

通過使用CSS3 border-radius屬性, 可以得到圓角的效果。

#rcorners {
    border-radius: 25px;
    background: #73AD21;
    padding: 20px; 
    width: 200px;
    height: 150px; 
}
image.png

顯示效果如下,此時四個方向的圓角都會被設(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.png

顯示效果如下:

image.png

注意:為了使 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允許多個背景圖片,有以下兩種寫法:
  1. 一次性設(shè)置所有的背景圖片和屬性
#example1 {
    background: url(img_flwr.gif) right bottom no-repeat, url(paper.gif) left top repeat;
}
  1. 單獨設(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 有兩個可選的值 containcover

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;
        }
image.png

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;
        }
image.png

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 屬性指定背景圖片的位置,它又下面三種取值:
  1. border-box:背景圖片從左上角的邊框開始。
image.png
  1. padding-box(默認(rèn)):背景圖片從padding的左上角開始。
image.png
  1. content-box:背景圖片從內(nèi)容的左上角開始。
image.png
  • CSS3 background-clip Property
    background-clip 屬性指定了背景圖片的填充區(qū)域,有以下三種取值:
  1. border-box(默認(rèn)):背景填充至邊框。
  2. padding-box:背景填充至padding。
  3. 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 */
        }

image.png

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 */
        }

image.png

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 */
        }
image.png

為了更加靈活的控制漸變方向,我們還可以自定義漸變角度。
語法:
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) */
        }
image.png
#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) */
        }
image.png

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

image.png

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*/
        }

image.png

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%);
        }

image.png

其中顏色屬性中的百分比指的是顏色停止位置占容器長度的百分比值。

  • 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 */
        }

image.png

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 */
        }
image.png

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;
        }
image.png

可以看出第一個長度參數(shù)控制的是陰影的水平位置,第二個參數(shù)控制的是陰影的垂直位置。

h2 {
            color: white;
            text-shadow: 2px 2px 4px #000000;
        }
image.png

第三個參數(shù)控制的是陰影的特效,第四個參數(shù)可以自定義陰影顏色。

多個陰影

 h2 {
            color: white;
            text-shadow: 1px 1px 2px black, 0 0 25px blue, 0 0 5px darkblue;
        }
image.png
h2 {
            color: yellow;
            text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
        }
image.png
  • 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;
}

效果如下:

image.png

六.CSS3 Text

  • text-overflow
    這個屬性控制當(dāng)你的文字超出容器時該如何顯示。
  1. text-overflow: clip;
  1. text-overflow: ellipsis;
image.png
  • word-wrap
    word-wrapbreak-word屬性使得一個很長的文字自動換行。
  • word-break
    word-break屬性指定了換行的規(guī)則。
  1. keep-all:只在連字符處換行
  2. 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);
}
image.png

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);
}
image.png

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

十一.CSS3 Animations

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • CSS3 是最新的 CSS 標(biāo)準(zhǔn),并且完全向后兼容,不過目前W3C 仍然在對 CSS3 規(guī)范進(jìn)行開發(fā),雖然標(biāo)準(zhǔn)的規(guī)...
    颭夏閱讀 3,987評論 4 42
  • HTML5 1.HTML5新元素 HTML5提供了新的元素來創(chuàng)建更好的頁面結(jié)構(gòu): 標(biāo)簽描述 定義頁面獨立的內(nèi)容區(qū)域...
    L怪丫頭閱讀 2,901評論 0 4
  • 總覽 邊框border-color 屬性boder-image 屬性border-radius 屬性box-sha...
    DecadeHeart閱讀 1,116評論 0 9
  • 一、CSS3 邊框 在 css3 中新增的邊框?qū)傩匀缦拢?創(chuàng)建圓角邊框 示例 在CSS2中添加圓角很棘手,我們不得...
    于曉魚閱讀 3,853評論 0 3
  • 幾行簡單的 CSS 代碼便可實現(xiàn)一系列令人眼前一亮的效果,比用 JavaScript 去模擬這樣的效果要好得多,不...
    簡不簡單_都好閱讀 480評論 0 1

友情鏈接更多精彩內(nèi)容