CSS中的clip-path區(qū)域裁剪屬性使用教程
Haorooms 發(fā)布時(shí)間:2016-06-28 11:44:45 作者:Aaron
我要評(píng)論

clip-path能根據(jù)設(shè)定的大小將頁面的元素劃分出所要顯示的區(qū)域,或者說相當(dāng)于遮罩而不是真正地裁去某個(gè)部分,這里我們就來看一下CSS中的clip-path區(qū)域裁剪屬性使用教程
CSS中的clip-path能夠讓你指定一個(gè)網(wǎng)頁元素的顯示區(qū)域,而不是缺省的顯示全部。
CSS Code復(fù)制內(nèi)容到剪貼板
- .clip-me {
- /* 已被標(biāo)志為不推薦使用的寫法 */
- position: absolute; /* 需要 absolute 和 fixed 定位 */
- clip: rect(110px, 160px, 170px, 60px); /* 或 "auto" */
- /* 值描述的是一個(gè) top/left 點(diǎn)和一個(gè) bottom/right 點(diǎn) */
- /* 最新規(guī)范寫法 (沒有定位要求), */
- clip-path: inset(10px 20px 30px 40px); /* or "none" */
- /* 值指的是 top, right, bottom, left 四個(gè)點(diǎn) */
- }
在clip-path的屬性值中的inset()函數(shù)中有四個(gè)值,分別表達(dá)著top/left和bottom/right四個(gè)點(diǎn),圈出一個(gè)矩形面積。這個(gè)矩形面積外的部分都將被裁剪隱藏掉。
需要注意的是,數(shù)值中間是用空格分割的,而老式的是用逗號(hào)。
例子:
看這個(gè)效果,對(duì)這個(gè)DIV進(jìn)行了裁剪。
代碼如下:
XML/HTML Code復(fù)制內(nèi)容到剪貼板
- <div class="haorooms-small" style="background-image: url('http://sandbox.runjs.cn/uploads/rs/216/0y89gzo2/idtga8h3.png');">
- </div>
CSS Code復(fù)制內(nèi)容到剪貼板
- .haorooms-small {
- background-size: cover;
- width: 300px;
- height: 300px;
- -webkit-clip-path: polygon(0% 50%, 25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%);
- clip-path: polygon(0% 50%, 25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%);
- }
clip-path的inset屬性應(yīng)用
CSS Code復(fù)制內(nèi)容到剪貼板
- <img class="clip-me" src="thing-to-be-clipped.png">
- .clip-me {
- /* 最新規(guī)范寫法 (沒有定位要求), */
- clip-path: inset(10px 20px 30px 40px); /* or "none" */
- /* 值指的是 top, right, bottom, left 四個(gè)點(diǎn) */
- }
在clip-path的屬性值中的inset()函數(shù)中有四個(gè)值,分別表達(dá)著top/left和bottom/right四個(gè)點(diǎn),圈出一個(gè)矩形面積。這個(gè)矩形面積外的部分都將被裁剪隱藏掉。
clip-path的其他屬性應(yīng)用
CSS Code復(fù)制內(nèi)容到剪貼板
- .clip-me {
- /* 引用一個(gè)內(nèi)聯(lián)的 SVG <clipPath> 路徑*/
- clip-path: url(#c1);
- /* 引用一個(gè)外部的 SVG 路徑*/
- clip-path: url(path.svg#c1);
- /* 多邊形 */
- clip-path: polygon(5% 5%, 100% 0%, 100% 75%, 75% 75%, 75% 100%, 50% 75%, 0% 75%);
- /* 圓形 */
- clip-path: circle(30px at 35px 35px);
- /* 橢圓 */
- clip-path: ellipse(65px 30px at 125px 40px);
- /* inset-rectangle() 將會(huì)替代 inset() ? */
- /* rectangle() 有可能出現(xiàn)于 SVG 2 */
- /* 圓角 */
- clip-path: inset(10% 10% 10% 10% round 20%, 20%);
- }
SVG 裁剪路徑樣例:
CSS Code復(fù)制內(nèi)容到剪貼板
- <clipPath id="clipping">
- <circle cx="150" cy="150" r="50" />
- <rect x="150" y="150" width="100" height="100" />
- </clipPath>
相關(guān)文章
- 這篇文章主要介紹了CSS中clip-path屬性的使用詳解,clip-path屬性可以創(chuàng)建一個(gè)只有元素的部分區(qū)域可以顯示的剪切區(qū)域。非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-10-16