Ciri clip-path

Ciri clip-path menentukan kawasan paparan elemen, memotongnya mengikut bentuk yang ditentukan. Ia menerima fungsi pemotongan atau laluan SVG sebagai parameter.

Sintaks

csspemilih {
  clip-path: fungsi pemotongan;
}

Nilai

Nilai Penerangan
circle Memotong elemen dalam bentuk bulatan. Parameter: jejari dan kedudukan pusat.
ellipse Memotong elemen dalam bentuk elips. Parameter: jejari X/Y dan kedudukan pusat.
polygon Memotong elemen dalam bentuk poligon. Parameter: koordinat titik dipisahkan koma.
url Menggunakan laluan SVG untuk pemotongan. Parameter: ID laluan dalam SVG.
path Memotong elemen mengikut laluan SVG menggunakan rentetan data laluan. Parameter: rentetan laluan SVG.
inset Memotong elemen dengan segi empat tepat dengan keupayaan untuk membulatkan sudut. Parameter: jarak atas, kanan, bawah, kiri dan jejari pembulatan.
margin-box Menggunakan jarak luar (margin) elemen sebagai kawasan pemotongan.
border-box Menggunakan sempadan (border) elemen sebagai kawasan pemotongan.
padding-box Menggunakan jarak dalam (padding) elemen sebagai kawasan pemotongan.
content-box Menggunakan kandungan (content) elemen sebagai kawasan pemotongan.
none Mematikan pemotongan (nilai lalai).

Contoh

Mari potong elemen dalam bentuk heksagon:

html<div id="hexagon"></div>
css#hexagon {
  width: 200px;
  height: 200px;
  background: #3498db;
  clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
}

Результат:

Contoh

Mari potong elemen dalam bentuk bulatan:

html<div id="circle"></div>
css#circle {
  width: 200px;
  height: 200px;
  background: #2ecc71;
  clip-path: circle(40% at 50% 50%);
}

Результат:

Contoh

Mari potong elemen dalam bentuk elips:

html<div id="ellipse"></div>
css#ellipse {
  width: 200px;
  height: 150px;
  background: #9b59b6;
  clip-path: ellipse(25% 40% at 50% 50%);
}

Результат:

Contoh

Mari potong elemen dengan sudut bulat:

html<div id="inset"></div>
css#inset {
  width: 200px;
  height: 200px;
  background: #e67e22;
  clip-path: inset(20% 15% 10% 5% round 10px);
}

Результат:

Contoh

Mari potong elemen mengikut laluan:

html<div id="path"></div>
css#path {
  width: 200px;
  height: 200px;
  background: #3498db;
  clip-path: path('M20,20 L180,20 L160,180 L40,180 Z');
}

Результат:

Contoh

Menggunakan laluan SVG untuk pemotongan kompleks:

html<svg width="0" height="0">
  <clipPath id="star-path">
    <path d="M50 0 L61 35 L98 35 L68 57 L79 92 L50 70 L21 92 L32 57 L2 35 L39 35 Z"/>
  </clipPath>
</svg>
<div id="star"></div>
css#star {
  width: 100px;
  height: 100px;
  background: #e74c3c;
  clip-path: url(#star-path);
}

Результат:

Contoh . Segi Tiga

Mari potong dalam bentuk segi tiga:

html<div id="triangle"></div>
css#triangle {
  width: 200px;
  height: 200px;
  background: #e74c3c;
  clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
}

Результат:

Contoh . Bentuk Gabungan

Membentuk bentuk kompleks:

html<div id="combined"></div>
css#combined {
  width: 250px;
  height: 250px;
  background: #2ecc71;
  clip-path: polygon(
    0% 0%, 100% 0%, 100% 75%,
    75% 75%, 75% 100%, 50% 75%,
    0% 75%
  );
}

Результат:

Contoh . Teks Dipotong

Mari potong blok teks:

html<div id="text-clip">
  <p>CSS clip-path creates amazing effects!</p>
</div>
css#text-clip {
  width: 300px;
  padding: 20px;
  background: #3498db;
  color: white;
  font-size: 24px;
  clip-path: ellipse(120px 80px at 50% 50%);
}

Результат:

Contoh . Pemotongan Bulat Imej

Memotong imej dalam bentuk bulatan:

html<div class="image-clip-circle">
  <img src="img.png">
</div>
css.image-clip-circle img {
  width: 200px;
  height: 200px;
  clip-path: circle(50% at center);
  object-fit: cover;
}

Результат:

Contoh . Pemotongan Berbentuk Bintang Imej

Memotong imej dalam bentuk bintang lima segi:

html<div class="image-clip-star">
  <img src="img.png">
</div>
css.image-clip-star img {
  width: 200px;
  height: 200px;
  clip-path: polygon(
    50% 0%,
    61% 35%,
    98% 35%,
    68% 57%,
    79% 91%,
    50% 70%,
    21% 91%,
    32% 57%,
    2% 35%,
    39% 35%
  );
  object-fit: cover;
}

Результат:

Contoh . Pemotongan Heksagon Imej

Memotong imej dalam bentuk heksagon:

html<div class="image-clip-hexagon">
  <img src="img.png">
</div>
css.image-clip-hexagon img {
  width: 200px;
  height: 200px;
  clip-path: polygon(
    50% 0%,
    100% 25%,
    100% 75%,
    50% 100%,
    0% 75%,
    0% 25%
  );
  object-fit: cover;
}

Результат:

Contoh . Pemotongan Berbentuk Hati

Memotong imej dalam bentuk hati:

html<div class="image-clip-heart">
  <img src="img.png">
</div>
css.image-clip-heart img {
  width: 200px;
  height: 180px;
  clip-path: polygon(
    50% 15%,
    35% 5%,
    15% 15%,
    10% 35%,
    25% 60%,
    50% 80%,
    75% 60%,
    90% 35%,
    85% 15%,
    65% 5%
  );
  object-fit: cover;
}

Результат:

Contoh . Pemotongan Imej pada Hover

Mengubah bentuk pemotongan ketika hover:

html<div class="image-clip">
  <img src="img.png">
</div>
css.image-clip {
  display: inline-block;
}
.image-clip img {
  width: 200px;
  height: 200px;
  clip-path: circle(40% at 50% 50%);
  object-fit: cover;
  transition: clip-path 0.5s ease;
}
.image-clip:hover img {
  clip-path: polygon(
    50% 0%,
    90% 20%,
    100% 60%,
    75% 100%,
    25% 100%,
    0% 60%,
    10% 20%
  );
}

Результат:

Lihat Juga

  • ciri clip,
    membolehkan pemotongan segi empat tepat
  • ciri mask,
    membolehkan penciptaan topeng kompleks untuk elemen
  • ciri shape-outside,
    menentukan bentuk pengaliran teks
  • ciri filter,
    menggunakan kesan grafik pada elemen