試一試
clip-path: circle(40%);
clip-path: ellipse(130px 140px at 10% 20%);
clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
clip-path: path("M 0 200 L 0,75 A 5,5 0,0,1 150,75 L 200 200 z");
clip-path: rect(5px 145px 160px 5px round 20%);
clip-path: xywh(0 5px 100% 75% round 15% 0);
<section class="default-example" id="default-example">
<div class="example-container">
<img
class="transition-all"
id="example-element"
src="/shared-assets/images/examples/balloon-small.jpg"
width="150" />
We had agreed, my companion and I, that I should call for him at his house,
after dinner, not later than eleven o’clock. This athletic young Frenchman
belongs to a small set of Parisian sportsmen, who have taken up “ballooning”
as a pastime. After having exhausted all the sensations that are to be found
in ordinary sports, even those of “automobiling” at a breakneck speed, the
members of the “Aéro Club” now seek in the air, where they indulge in all
kinds of daring feats, the nerve-racking excitement that they have ceased to
find on earth.
</div>
</section>
section {
align-items: flex-start;
}
.example-container {
text-align: left;
padding: 20px;
}
#example-element {
float: left;
width: 150px;
margin: 20px;
}
語法
/* Keyword values */
clip-path: none;
/* <clip-source> values */
clip-path: url("resources.svg#c1");
/* <geometry-box> values */
clip-path: margin-box;
clip-path: border-box;
clip-path: padding-box;
clip-path: content-box;
clip-path: fill-box;
clip-path: stroke-box;
clip-path: view-box;
/* <basic-shape> values */
clip-path: inset(100px 50px);
clip-path: circle(50px at 0 100px);
clip-path: ellipse(50px 60px at 10% 20%);
clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
clip-path: path(
"M0.5,1 C0.5,1,0,0.7,0,0.3 A0.25,0.25,1,1,1,0.5,0.3 A0.25,0.25,1,1,1,1,0.3 C1,0.7,0.5,1,0.5,1 Z"
);
clip-path: rect(5px 5px 160px 145px round 20%);
clip-path: shape(from 0% 0%, line to 100% 0%, line to 50% 100%, close);
clip-path: xywh(0 5px 100% 75% round 15% 0);
/* Box and shape values combined */
clip-path: padding-box circle(50px at 0 100px);
/* Global values */
clip-path: inherit;
clip-path: initial;
clip-path: revert;
clip-path: revert-layer;
clip-path: unset;
clip-path 屬性可以指定為以下列出的一個值或多個值的組合。
值
<clip-source>-
引用 SVG
<clipPath>元素的<url>。 <basic-shape>-
一個形狀,其大小和位置由
<geometry-box>值定義。如果未指定幾何框,則將使用border-box作為參考框。其中之一是: <geometry-box>-
如果與
<basic-shape>結合指定,此值定義了基本形狀的參考框。如果單獨指定,它將導致指定框的邊緣(包括任何角部形狀,例如border-radius)作為裁剪路徑。幾何框可以是以下值之一:margin-box-
使用 margin box 作為參考框。
border-box-
使用 border box 作為參考框。
padding-box-
使用 padding box 作為參考框。
content-box-
使用 content box 作為參考框。
fill-box-
使用物件邊框作為參考框。
stroke-box-
使用描邊邊框作為參考框。
view-box-
使用最近的 SVG 視口作為參考框。如果為建立 SVG 視口的元素指定了
viewBox屬性,則參考框位於viewBox屬性建立的座標系原點,並且參考框的大小尺寸設定為viewBox屬性的寬度和高度值。
none-
不建立裁剪路徑。
正式定義
正式語法
clip-path =
<clip-source> |
[ <basic-shape> || <geometry-box> ] |
none
<clip-source> =
<url>
<geometry-box> =
<shape-box> |
fill-box |
stroke-box |
view-box
<shape-box> =
<visual-box> |
margin-box |
half-border-box
<visual-box> =
content-box |
padding-box |
border-box
示例
形狀和幾何框
在此示例中,透過在 <div> 元素上將 polygon() 定義為剪裁路徑來建立兩個三角形。每個都具有純色背景和粗 border。第二個 <div> 元素的參考框設定為 content-box
HTML
<div></div>
<div></div>
CSS
div {
height: 200px;
width: 200px;
box-sizing: border-box;
background-color: rebeccapurple;
border: 20px solid magenta;
clip-path: polygon(50% 0, 100% 100%, 0 100%);
}
div:last-of-type {
clip-path: content-box polygon(50% 0, 100% 100%, 0 100%);
}
結果
對於第一個三角形,我們沒有指定參考框;因此它預設為 border-box,0% 和 100% 的位置位於邊框的外邊緣。在第二個示例中,我們將 <geometry-box> 設定為 content-box,這意味著基本形狀的參考框是內容區域的外邊緣,它位於內邊距框內部。因為我們的示例沒有 padding,所以這是邊框的內邊緣。
shape() 與 path() 函式
在前面的示例的基礎上,我們使用不同的 <basic-shape> 值建立相同的三角形,演示 shape() 和 path() 函式也可以用於建立裁剪路徑,其中 shape() 是一個更靈活的解決方案。
我們使用 path() 定義第一個元素的裁剪路徑,並使用 shape() 定義第二個元素的裁剪路徑,兩者都使用預設的 border-box 作為它們的參考框
div {
clip-path: path("M100 0 L200 200 L0 200 Z");
}
div:last-of-type {
clip-path: shape(from 50% 0, line to 100% 100%, line to 0 100%, close);
}
結果是,使用 shape() 函式定義的路徑會隨元素增長,而 path() 版本則不會
因為 shape() 函式允許使用 <percentage> 值(以及 自定義屬性),所以它更健壯。
我們將透過增加底層元素的大小來證明這一點
div {
width: 250px;
height: 250px;
}
在由 shape() 函式定義的裁剪路徑示例中,四個邊框的可見性,或者至少是部分可見性,是由於百分比值允許路徑隨元素增長。在 path() 版本中,元素增長了,但形狀沒有。結果,頂部和左側邊框部分可見,而右側和底部邊框被剪裁掉了。
SVG 作為裁剪源
在此示例中,我們定義 SVG <clipPath> 元素用作 clip-path 源。
HTML
我們包含兩個 <div> 元素和一個包含兩個 <clipPath> 元素的 <svg> 元素。其中一個 <clipPath> 包含四個 <rect> 元素,它們共同定義了窗格,在中間留下了一個空白的十字形,另一個包含兩個交叉的 <rect> 元素。
<svg height="0" width="0">
<defs>
<clipPath id="window">
<rect y="0" x="0" width="80" height="80" />
<rect y="0" x="120" width="80" height="80" />
<rect y="120" x="0" width="80" height="80" />
<rect y="120" x="120" width="80" height="80" />
</clipPath>
<clipPath id="cross">
<rect y="0" x="80" width="40" height="200" />
<rect y="80" x="0" width="200" height="40" />
</clipPath>
</defs>
</svg>
<div class="window">Window</div>
<div class="cross">Cross</div>
CSS
我們使用 彈性盒佈局,如果空間允許,使我們的元素並排排列,並留有間隙。我們在兩個 <div> 元素上定義了一個 conic-gradient() 背景影像,提供了一個有趣的視覺效果來裁剪,並帶有 border。
body {
display: flex;
gap: 20px;
flex-flow: row wrap;
font: 2em sans-serif;
}
div {
width: 200px;
height: 200px;
background-image: conic-gradient(
at center,
rebeccapurple,
green,
lightblue,
rebeccapurple
);
border: 5px solid;
box-sizing: border-box;
}
然後我們將 <clipPath> 的 id 設定為 <clip-source>。在 cross 示例中,我們使用 align-content 垂直居中文字,否則文字將被裁剪,就像在 window 示例中發生的那樣。
.window {
clip-path: url("#window");
}
.cross {
clip-path: url("#cross");
align-content: center;
}
結果
元素,包括它們的邊框和文字,都被裁剪,只有與 <clipPath> 元素重疊的部分被繪製到頁面上。
各種值型別
此示例演示 clip-path 屬性的各種值裁剪 HTML <img>。
HTML
HTML 包含一個將被裁剪的 <img>、一個星形 <clipPath> 和一個 <select> 元素,用於選擇 clip-path 屬性值。
<img
id="clipped"
src="https://mdn.github.io/shared-assets/images/examples/progress-pride-flag.jpg"
alt="Pride flag" />
<svg height="0" width="0">
<defs>
<clipPath id="star">
<path d="M100,0 42,180 196,70 4,70 158,180z" />
</clipPath>
</defs>
</svg>
<select id="clipPath">
<option value="none">none</option>
<option value="circle(100px at 110px 100px)">circle</option>
<option value="url(#star)" selected>star</option>
<option value="inset(20px round 20px)">inset</option>
<option value="rect(20px 150px 200px 20px round 10%)">rect</option>
<option value="xywh(0 20% 90% 67% round 0 0 5% 5px)">xywh</option>
<option value="path('M 0 200 L 0,110 A 110,90 0,0,1 240,100 L 200 340 z')">
path
</option>
</select>
CSS
初始渲染包括星形作為 clip-path 源。
#clipped {
margin-bottom: 20px;
clip-path: url("#star");
}
JavaScript
當您從 <select> 選單中選擇一個新選項時,事件處理程式會更新 <img> 上設定的 clip-path 值。
const clipPathSelect = document.getElementById("clipPath");
clipPathSelect.addEventListener("change", (evt) => {
const path = evt.target.value;
document.getElementById("clipped").style.clipPath = path;
log(`clip-path: ${path};`);
});
結果
選擇不同的選項以更改 clip-path 值。
注意: 雖然可以定義文字路徑,但如果您想將背景影像裁剪到文字而不是形狀,請參閱 background-clip 屬性。
規範
| 規範 |
|---|
| CSS 蒙版模組 Level 1 # the-clip-path |
瀏覽器相容性
載入中…