首頁 >web前端 >html教學 >CSS(四):背景+變形

CSS(四):背景+變形

WBOY
WBOY原創
2016-08-15 16:49:501403瀏覽
  • background-origin

設定元素背景圖片的原始起始位置。必須保證背景是background-repeat為no-repeat
此屬性才會生效。
可取值有 border-box | padding-box | content-box

border-box:設定背景圖片原點在邊框的最左上角

<span style="color: #008080;">1</span> <span style="color: #000000;">.img{
</span><span style="color: #008080;">2</span> <span style="color: #000000;">    background-image:url(1.jpg);
</span><span style="color: #008080;">3</span> <span style="color: #000000;">    background-repeat:no-repeat;
</span><span style="color: #008080;">4</span> <span style="color: #000000;">    background-origin:border-box;
</span><span style="color: #008080;">5</span> <span style="color: #000000;">}
</span><span style="color: #008080;">6</span> 
<span style="color: #008080;">7</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="img"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>

padding-box:設定背景圖片原點在內邊距的最左上角

<span style="color: #008080;">1</span> <span style="color: #000000;">.img{
</span><span style="color: #008080;">2</span> <span style="color: #000000;">    background-image:url(1.jpg);
</span><span style="color: #008080;">3</span> <span style="color: #000000;">    background-repeat:no-repeat;
</span><span style="color: #008080;">4</span> <span style="color: #000000;">    background-origin:padding-box;
</span><span style="color: #008080;">5</span> <span style="color: #000000;">}
</span><span style="color: #008080;">6</span> 
<span style="color: #008080;">7</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="img"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>

content-box:設定背景圖片原點在內容區域的最左上角

<span style="color: #008080;">1</span> <span style="color: #000000;">.img{
</span><span style="color: #008080;">2</span> <span style="color: #000000;">    background-image:url(1.jpg);
</span><span style="color: #008080;">3</span> <span style="color: #000000;">    background-repeat:no-repeat;
</span><span style="color: #008080;">4</span> <span style="color: #000000;">    background-origin:content-box;
</span><span style="color: #008080;">5</span> <span style="color: #000000;">}
</span><span style="color: #008080;">6</span> 
<span style="color: #008080;">7</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="img"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
  • background-clip

設定背景影像向外裁切的區域。
可取值有 border-box | padding-box | content-box

border-box:設定裁切區域為邊框以內,包含邊框

<span style="color: #008080;">1</span> <span style="color: #000000;">.img{
</span><span style="color: #008080;">2</span> <span style="color: #000000;">    background-image:url(1.jpg);
</span><span style="color: #008080;">3</span> <span style="color: #000000;">    background-clip:border-box;
</span><span style="color: #008080;">4</span> <span style="color: #000000;">}
</span><span style="color: #008080;">5</span> 
<span style="color: #008080;">6</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="img"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>

padding-box:設定裁切區域為內邊距以內,包含內邊距

<span style="color: #008080;">1</span> <span style="color: #000000;">.img{
</span><span style="color: #008080;">2</span> <span style="color: #000000;">    background-image:url(1.jpg);
</span><span style="color: #008080;">3</span> <span style="color: #000000;">    background-clip:padding-box;
</span><span style="color: #008080;">4</span> <span style="color: #000000;">}
</span><span style="color: #008080;">5</span> 
<span style="color: #008080;">6</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="img"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>

content-box:設定裁切區域為內容區域

<span style="color: #008080;">1</span> <span style="color: #000000;">.img{
</span><span style="color: #008080;">2</span> <span style="color: #000000;">    background-image:url(1.jpg);
</span><span style="color: #008080;">3</span> <span style="color: #000000;">    background-clip:content-box;
</span><span style="color: #008080;">4</span> <span style="color: #000000;">}
</span><span style="color: #008080;">5</span> 
<span style="color: #008080;">6</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="img"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
  • transform

改變元素的大小,透明,旋轉角度,扭曲度等。
語法:
transform:none |  +
transform-function包含下列幾種方法:
translate(): 指定物件的2D translation(2D平移)。第一個參數對應X軸,第二個參數對應Y軸。如果第二個參數未提供,則預設值為0
transform-origin:任何一個元素都有一個中心點,預設值為0
transform-origin:任何一個元素都有一個中心點,預設值之下,其中心點是居於元素X軸和Y軸的50%處。
translateX(): 指定物件X軸(水平方向)的平移
translateY():指定物件Y軸(垂直方向)的平移
rotate(): 指定物件的2D rotation(2D旋轉),需先有 屬性的定義
scale(): 指定物件的2D scale(2D縮放)。第一個參數對應X軸,第二個參數對應Y軸。如果第二個參數未提供,則預設取第一個參數的值
skew(): 指定物件skew transformation(斜切扭曲)。第一個參數對應X軸,第二個參數對應Y軸。如果第二個參數未提供,則預設值為0

translate3d(): 指定物件的3D位移。第1個參數對應X軸,第2個參數對應Y軸,第3個參數對應Z軸,參數不允許省略

translate:向X軸和Y軸分別偏移元素自身的50%
<span style="color: #008080;">1</span> <span style="color: #000000;">.img{
</span><span style="color: #008080;">2</span> <span style="color: #000000;">    width:200px;
</span><span style="color: #008080;">3</span> <span style="color: #000000;">    height:200px;
</span><span style="color: #008080;">4</span> <span style="color: #000000;">    background-image:url(1.jpg);
</span><span style="color: #008080;">5</span> <span style="color: #000000;">    transform: translate(50%,50%);
</span><span style="color: #008080;">6</span> <span style="color: #000000;">}
</span><span style="color: #008080;">7</span> 
<span style="color: #008080;">8</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="img"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>

transform-origin:把元素的中心點設定在0 0位置,即左上角
<span style="color: #008080;">1</span> <span style="color: #000000;">.img{
</span><span style="color: #008080;">2</span> <span style="color: #000000;">    width:200px;
</span><span style="color: #008080;">3</span> <span style="color: #000000;">    height:200px;
</span><span style="color: #008080;">4</span> <span style="color: #000000;">    background-image:url(1.jpg);
</span><span style="color: #008080;">5</span> <span style="color: #000000;">    transform-origin:0 0;
</span><span style="color: #008080;">6</span> <span style="color: #000000;">    transform: translate(50%,50%);
</span><span style="color: #008080;">7</span> <span style="color: #000000;">}
</span><span style="color: #008080;">8</span> 
<span style="color: #008080;">9</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="img"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>

translateX:僅設定X軸的偏移
<span style="color: #008080;">1</span> <span style="color: #000000;">.img{
</span><span style="color: #008080;">2</span> <span style="color: #000000;">    width:200px;
</span><span style="color: #008080;">3</span> <span style="color: #000000;">    height:200px;
</span><span style="color: #008080;">4</span> <span style="color: #000000;">    background-image:url(1.jpg);
</span><span style="color: #008080;">5</span> <span style="color: #000000;">    transform: translateX(50%);
</span><span style="color: #008080;">6</span> <span style="color: #000000;">}
</span><span style="color: #008080;">7</span> 
<span style="color: #008080;">8</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="img"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>

translateY:僅設定Y軸的偏移
<span style="color: #008080;">1</span> <span style="color: #000000;">.img{
</span><span style="color: #008080;">2</span> <span style="color: #000000;">    width:200px;
</span><span style="color: #008080;">3</span> <span style="color: #000000;">    height:200px;
</span><span style="color: #008080;">4</span> <span style="color: #000000;">    background-image:url(1.jpg);
</span><span style="color: #008080;">5</span> <span style="color: #000000;">    transform: translateY(50%);
</span><span style="color: #008080;">6</span> <span style="color: #000000;">}
</span><span style="color: #008080;">7</span> 
<span style="color: #008080;">8</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="img"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>

rotate:設定元素進行2D旋轉,接受的參數為角度(deg)
<span style="color: #008080;">1</span> <span style="color: #000000;">.img{
</span><span style="color: #008080;">2</span> <span style="color: #000000;">    width:200px;
</span><span style="color: #008080;">3</span> <span style="color: #000000;">    height:200px;
</span><span style="color: #008080;">4</span> <span style="color: #000000;">    background-image:url(1.jpg);
</span><span style="color: #008080;">5</span> <span style="color: #000000;">    transform: rotate(180deg);
</span><span style="color: #008080;">6</span> <span style="color: #000000;">}
</span><span style="color: #008080;">7</span> 
<span style="color: #008080;">8</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="img"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>

scale:設定元素縮放3倍,接受的參數為數值,表示放大的倍數
<span style="color: #008080;">1</span> <span style="color: #000000;">.img{
</span><span style="color: #008080;">2</span> <span style="color: #000000;">    width:200px;
</span><span style="color: #008080;">3</span> <span style="color: #000000;">    height:200px;
</span><span style="color: #008080;">4</span> <span style="color: #000000;">    background-image:url(1.jpg);
</span><span style="color: #008080;">5</span> <span style="color: #000000;">    transform: scale(3);
</span><span style="color: #008080;">6</span> <span style="color: #000000;">}
</span><span style="color: #008080;">7</span> 
<span style="color: #008080;">8</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="img"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>

skew:設定元素的傾斜角度
<span style="color: #008080;">1</span> <span style="color: #000000;">.img{
</span><span style="color: #008080;">2</span> <span style="color: #000000;">    width:200px;
</span><span style="color: #008080;">3</span> <span style="color: #000000;">    height:200px;
</span><span style="color: #008080;">4</span> <span style="color: #000000;">    background-image:url(1.jpg);
</span><span style="color: #008080;">5</span> <span style="color: #000000;">    transform: skew(60deg);
</span><span style="color: #008080;">6</span> <span style="color: #000000;">}
</span><span style="color: #008080;">7</span> 
<span style="color: #008080;">8</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="img"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>

translate3d:設定元素的3d位移
<span style="color: #008080;">1</span> <span style="color: #000000;">.img{
</span><span style="color: #008080;">2</span> <span style="color: #000000;">    width:200px;
</span><span style="color: #008080;">3</span> <span style="color: #000000;">    height:200px;
</span><span style="color: #008080;">4</span> <span style="color: #000000;">    background-image:url(1.jpg);
</span><span style="color: #008080;">5</span> <span style="color: #000000;">    transform: translate3d(50px,50px,1px);
</span><span style="color: #008080;">6</span> <span style="color: #000000;">}
</span><span style="color: #008080;">7</span> 
<span style="color: #008080;">8</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="img"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>

 
🎜
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn