The clip-path property

The clip-path property defines the visible region of an element by clipping it to a specified shape. It accepts a clipping function or SVG path as a parameter.

Syntax

cssselector {
  clip-path: clipping-function;
}

Values

Value Description
circle Clips the element to a circle. Parameters: radius and center position.
ellipse Clips the element to an ellipse. Parameters: X/Y radii and center position.
polygon Clips the element to a polygon. Parameters: comma-separated point coordinates.
url Uses an SVG path for clipping. Parameter: path ID in SVG.
path Clips the element to an SVG path using path data string. Parameter: SVG path string.
inset Clips the element to a rectangle with optional rounded corners. Parameters: top, right, bottom, left offsets and border radius.
margin-box Uses the element's margin box as the clipping area.
border-box Uses the element's border box as the clipping area.
padding-box Uses the element's padding box as the clipping area.
content-box Uses the element's content box as the clipping area.
none Disables clipping (default value).

Example

Clip an element to a hexagon:

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

Результат:

Example

Clip an element to a circle:

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

Результат:

Example

Clip an element to an ellipse:

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

Результат:

Example

Clip an element with rounded corners:

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

Результат:

Example

Clip an element to a circle:

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

Результат:

Example

Using SVG path for complex clipping:

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

Результат:

Example . Triangle

Clip to a triangle shape:

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

Результат:

Example . Combined shapes

Creating a complex shape:

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

Результат:

Example . Text clipping

Clip a text block:

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

Результат:

Example . Circular image clipping

Clip an image to a circle:

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

Результат:

Example . Star-shaped image clipping

Clip an image to a five-pointed star:

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

Результат:

Example . Hexagonal image clipping

Clip an image to a hexagon:

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

Результат:

Example . Heart-shaped clipping

Clip an image to a heart shape:

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

Результат:

Example . Hover-based image clipping

Change clipping shape on 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%
  );
}

Результат:

See also

  • the clip property,
    which allows cropping a rectangle
  • mask property,
    which allows creating complex masks for elements
  • shape-outside property,
    defining text wrapping shapes
  • filter property,
    applying graphical effects to elements