26
loading...
This website collects cookies to deliver better user experience
clip-path()
property. By the end of it, you will have a thorough understanding of CSS animation and how to manipulate any clip-path()
code you might come across.@keyframes
animation
properties@keyframes
is an at-rule in CSS. It is used to define the stages and styles of the animation. It comprises the name of the animation, the keyframe block, and the stages of the animation.@keyframes
. It's also called the keyframes name.popIn
.@keyframes popIn {}
@keyframes popIn {
/* This is the keyframes block */
from
or to
blocks, comma-separated percentage values, or a combination of both.@keyframes popIn {
0% {
transform: scale(0.1);
opacity: 0;
}
60% {
transform: scale(1.2);
opacity: 1;
}
100% {
transform: scale(1);
}
}
from
or to
block with the comma-separated percentage values, meaning you can write the previous code block as such:@keyframes popIn {
from {
transform: scale(0.1);
opacity: 0;
}
60% {
transform: scale(1.2);
opacity: 1;
}
to {
transform: scale(1);
}
}<
from
is equivalent to 0%
and to
is equivalent to 100%
.60%
in the previous code block as 60
, the browser will ignore the animation at that stage, which can lead to hard-to-trace bugs.@keyframes
to the elements you wish to animate, and they also define how the element is animated. They are necessary for the animation to function.animation-name
: this is the animation name defined in the keyframesanimation-duration
: this tells the animation how long it will occur for and is specified in seconds or milliseconds.popIn
animation to your CSS filebody
tags.<div>
<p class="hello-world">Hello World!!!</p>
</div>
@keyframes
you have in the CSS file, type the following:.hello-world {
animation-name: popIn;
animation-duration: 3s;
}
.hello-world {
animation: popIn 3s;
}
animation
property is shorthand for all other animation properties listed in the next section (excluding the animation
property itself).animation
animation-delay
animation-direction
animation-fill-mode
animation-iteration-count
animation-play-state
animation-timing-function
clip-path()
property to create some cool effects.clip-path()
property.clip-path()
is a property that allows you to create a clipping region from an element. The region within this clipped part is shown, while the rest are hidden.clip-path()
might be daunting to understand, but once you understand the geometry behind it, it becomes easy to use.clip-path()
allows you to create clipping regions of shapes when you specify their shape and position on a coordinate system.x0, y0
. When you move clockwise, the final position would still be x0, y0
.CSS clip-path()
to create and position shapes on this coordinate system. This is possible thanks to its array of accepted values.clip-path()
accepted values are functions that accept parameters. These parameters dictate the appearance and position of the clipped region.inset()
circle()
ellipse()
path()
polygon()
inset()
function allows you to clip an element from all four sides of the coordinate system. The result is a visible rectangle.inset()
is similar to the way you assign values to the margin
or padding
property..selector {
clip-path: inset(20px 30px 35px 20px);
}
inset()
comes with additional powers! You can specify the border-radius as one of its parameters. You'll have to use the round
keyword followed by the border-radius
value, as depicted in the next code block..selector {
clip-path: inset(20px 30px 35px 20px round 20px);
}
round
keyword..selector {
clip-path: inset(20px 30px 35px 20px round 20px 50px);
}
.selector {
clip-path: circle(100px at 40%)
}
100px
and its position is 40%
on the x- and y-axis.at
keyword), then the clipping position.center
or percentage
. In the next code block, 50% 50%
is equivalent to the center
keyword..selector {
clip-path: ellipse(80px 60px at 50% 50%);
}
path()
function, which allows you to use an SVG path to create a clipping region.url()
function as the value of clip-path()
.<div class="myClip">
<svg>
<clipPath id="clipPath">
<path d="M10,10 q60,60 100,0 q50,50 50,50 l40,0 l-40,40 l-100,-20"/>
</clipPath>
</svg>
</div>
.myClip {
background-color: #1560bd;
clip-path: url("#clipPath");
}
polygon()
value, you can create complex shapes. Here is what we'll do: I will walk you through the process of plotting some shapes on the coordinate system. Afterward, you'll pass these coordinates as parameters to the polygon()
function in your CSS file.x20, y0
x80, y0
x70, y100
x10, y100
x20, y0
polygon()
function with one tiny but crucial modification: the percentage sign (%)..selector {
background-color: #1560bd;
clip-path: polygon(20% 0%, 80% 0%, 70% 100%, 10% 100%);
}
x0, y0
x100, y0
x50, y100
x0, y0
.selector {
background-color: #1560bd;
clip-path: polygon(0% 0%, 100% 0%, 50% 100%);
}
.selector {
background-color: #1560bd;
clip-path: polygon(50% 0%, 100% 45%, 80% 100%, 20% 100%, 0% 45%);
}
.selector {
background-color: #1560bd;
clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
}
polygon()
function, you are only limited by your imagination 😊.clip-path()
.clip-path()
to create a clipping region during the stages of the animation, creating the illusion that the element is indeed changing its shape.clip-path()
property exclusively. The rest are considered cosmetics.clip-path()
.circle()
at different stages of the animation. When you add it to the element you wish to animate, you ensure the animation is running at all times.animation-iteration-count
, and specify its value as infinite
.<main>
<div class="circles"></div>
</main>
main {
display: grid;
place-items: center;
padding: 2em;
}
.circles {
background: #1a1a1a;
clip-path: circle(80px at 50% 50%);
height: 200px;
width: 200px;
border-radius: 50%;
animation-name: infiniteCircle;
animation-duration: 3s;
animation-timing-function: ease-in;
animation-iteration-count: infinite;
}
@keyframes infiniteCircle {
from {
background: #f00;
clip-path: circle(50%);
}
50% {
background: #ffd700;
clip-path: circle(35%);
}
to {
background: #f0f;
clip-path: circle(75%);
}
}
polygon()
, then plot varieties of shapes closely related to the square at different stages of the animation.div
inside main
to squares
.main {
display: grid;
place-items: center;
padding: 2em;
}
.squares {
width: 250px;
height: 250px;
background-image: linear-gradient(to right, #000000, #e74c3c);
clip-path: polygon(20% 0%, 80% 0%, 100% 100%, 0% 100%);
animation-name: dancingSquares;
animation-duration: 4s;
animation-direction: alternate-reverse;
animation-iteration-count: infinite;
}
@keyframes dancingSquares {
25% {
background-image: linear-gradient(to right, #f0c27b, #4b1248);
clip-path: polygon(20% 0%, 100% 40%, 70% 100%, 10% 100%);
}
50% {
background-image: linear-gradient(to right, #c21500, #ffc500);
clip-path: polygon(0% 45%, 100% 20%, 50% 75%, 0% 100%);
}
75% {
background-image: linear-gradient(to right, #00d2ff, #3a7bd5);
clip-path: polygon(100% 38%, 100% 38%, 66% 100%, 0% 53%);
}
}
clip-path()
around an element, and afterward, flip it into another shape during the animation stages.<main>
<img src="path-to-your-image" alt="The image all text" />
</main>
main {
display: grid;
place-items: center;
padding: 2em;
}
img {
width: 600px;
clip-path: polygon(50% 0%, 100% 38%, 82% 100%, 18% 100%, 0% 38%);
animation-name: flippingShapes;
animation-duration: 3s;
animation-iteration-count: infinite;
}
@keyframes flippingShapes {
from {
clip-path: polygon(50% 0%, 100% 38%, 82% 100%, 18% 100%, 0% 38%);
}
50% {
clip-path: polygon(50% 50%, 90% 88%, 80% 10%, 20% 10%, 8% 90%);
}
to {
clip-path: polygon(50% 0%, 100% 38%, 82% 100%, 18% 100%, 0% 38%);
}
}
clip-path()
. During the animation stages, you'll create more desired shapes.animation-iteration-count
to infinite.<main>
<div class="container">
<img src="multi.jpg" alt="Two brown Tiger cubs">
</div>
</main>
from
and to
are mixed with comma-separated percentage values.main {
display: grid;
place-items: center;
padding: 2em;
}
img {
max-width: 100%;
}
.container {
width: 30em;
clip-path: polygon(20% 0%, 80% 0%, 100% 100%, 0% 100%);
animation-name: changeShapes;
animation-duration: 5s;
animation-iteration-count: infinite;
}
@keyframes changeShapes {
from {
clip-path: polygon(25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%, 0% 50%);
}
50% {
clip-path: polygon(25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%, 0% 50%);
}
75% {
clip-path: polygon(0% 15%, 15% 15%, 15% 0%, 85% 0%, 85% 15%, 100% 15%, 100% 85%, 85% 85%, 85% 100%, 15% 100%, 15% 85%, 0% 85%);
}
to {
clip-path: polygon(25% 0%, 100% 0%, 75% 100%, 0% 100%);
}
}
3.3%
, and when the animation runs for 10 seconds, the CPU utilization increases to 24.5%
.animation-iteration-count
to infinite
, so the animation will run at all times until you close the active browser window.animation-iteration-count
.