CSS transforms and effects allow you to change the appearance and position of elements without affecting the document flow. This guide covers transforms, filters, clipping, masking, and blend modes.
CSS transforms allow you to rotate, scale, skew, or translate elements in 2D or 3D space.
.translate-xy:hover {
transform: translate(30px, 20px);
}
/* Individual axes */
.translate-x:hover {
transform: translateX(50px);
}
.translate-y:hover {
transform: translateY(20px);
}
.scale-xy:hover {
transform: scale(1.2, 1.2);
}
/* Individual axes */
.scale-x:hover {
transform: scaleX(1.5);
}
.scale-y:hover {
transform: scaleY(1.5);
}
.rotate-2d:hover {
transform: rotate(45deg);
}
.skew-xy:hover {
transform: skew(15deg, 10deg);
}
/* Individual axes */
.skew-x:hover {
transform: skewX(20deg);
}
.skew-y:hover {
transform: skewY(10deg);
}
.origin-top-left {
transform-origin: top left;
}
.origin-top-left:hover {
transform: rotate(30deg);
}
/* Other values */
.origin-center { /* Default */
transform-origin: center;
}
.origin-bottom-right {
transform-origin: bottom right;
}
.rotate-x:hover {
transform: rotateX(45deg);
}
.rotate-y:hover {
transform: rotateY(45deg);
}
.rotate-z:hover { /* Same as rotate() */
transform: rotateZ(45deg);
}
.rotate-3d:hover {
transform: rotate3d(1, 1, 1, 45deg);
}
.translate-z:hover {
transform: translateZ(50px);
}
.translate-3d:hover {
transform: translate3d(20px, 20px, 50px);
}
.multiple-transforms:hover {
transform: rotate(45deg) scale(1.2) translateX(20px);
}
Note: Transforms are applied from right to left.
.perspective-100 {
perspective: 100px;
}
Closer perspective creates more extreme 3D effects.
.perspective-500 {
perspective: 500px;
}
Medium perspective creates moderate 3D effects.
.perspective-1000 {
perspective: 1000px;
}
Distant perspective creates subtle 3D effects.
.backface-box {
backface-visibility: visible; /* Default */
}
.backface-front {
z-index: 2;
}
.backface-back {
transform: rotateY(180deg);
}
.backface-hidden .backface-box {
backface-visibility: hidden;
}
The back side of elements is invisible when rotated away from view.
.cube {
transform-style: preserve-3d;
}
/* Positioning cube faces */
.cube-front {
transform: translateZ(50px);
}
.cube-back {
transform: translateZ(-50px) rotateY(180deg);
}
.cube-right {
transform: translateX(50px) rotateY(90deg);
}
.cube-left {
transform: translateX(-50px) rotateY(-90deg);
}
.cube-top {
transform: translateY(-50px) rotateX(90deg);
}
.cube-bottom {
transform: translateY(50px) rotateX(-90deg);
}
Hover Me
This is the back of the card.
.flip-card {
perspective: 1000px;
width: 250px;
height: 150px;
}
.flip-card-inner {
width: 100%;
height: 100%;
position: relative;
transition: transform 0.6s;
transform-style: preserve-3d;
}
.flip-card:hover .flip-card-inner {
transform: rotateY(180deg);
}
.flip-card-front, .flip-card-back {
position: absolute;
width: 100%;
height: 100%;
backface-visibility: hidden;
}
.flip-card-back {
transform: rotateY(180deg);
}
CSS filters allow you to apply graphical effects to elements. Hover over each image to see the filter in action.
.filter-blur:hover {
filter: blur(5px);
}
.filter-brightness:hover {
filter: brightness(1.5);
}
.filter-contrast:hover {
filter: contrast(200%);
}
.filter-grayscale:hover {
filter: grayscale(100%);
}
.filter-hue-rotate:hover {
filter: hue-rotate(90deg);
}
.filter-invert:hover {
filter: invert(100%);
}
.filter-opacity:hover {
filter: opacity(50%);
}
.filter-saturate:hover {
filter: saturate(200%);
}
.filter-sepia:hover {
filter: sepia(100%);
}
.filter-shadow:hover {
filter: drop-shadow(5px 5px 5px rgba(0,0,0,0.5));
}
.filter-multiple:hover {
filter: contrast(150%) brightness(120%) sepia(30%);
}
Filters can be CPU-intensive. Consider the following tips:
will-change: filter;
for animations.opacity()
and brightness()
over complex ones like blur()
.The clip-path
property creates a clipping region that sets what part of an element should be shown.
.clip-circle {
clip-path: circle(50% at 50% 50%);
}
/* Hover animation */
.clip-circle:hover {
clip-path: circle(40% at 50% 50%);
}
.clip-polygon {
clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
}
/* Hover animation */
.clip-polygon:hover {
clip-path: polygon(50% 10%, 90% 50%, 50% 90%, 10% 50%);
}
.clip-ellipse {
clip-path: ellipse(50% 30% at 50% 50%);
}
.clip-inset {
clip-path: inset(20% 20% 20% 20% round 20px);
}
/* Hover animation */
.clip-inset:hover {
clip-path: inset(10% 10% 10% 10% round 40px);
}
The mask-image
property creates a mask layer that hides parts of an element.
.mask-demo {
background: linear-gradient(45deg, #3498db, #e74c3c);
mask-image: url('data:image/svg+xml;utf8,');
mask-size: cover;
}
Blend modes control how an element's content blends with the background or underlying content.
Controls how an element's content blends with the content of its parent and backdrop.
.blend-top {
mix-blend-mode: multiply;
}
.blend-top {
mix-blend-mode: screen;
}
.blend-top {
mix-blend-mode: overlay;
}
.blend-top {
mix-blend-mode: difference;
}
/* Basic blend modes */
mix-blend-mode: normal;
mix-blend-mode: multiply;
mix-blend-mode: screen;
mix-blend-mode: overlay;
/* Darkening blend modes */
mix-blend-mode: darken;
mix-blend-mode: color-burn;
/* Lightening blend modes */
mix-blend-mode: lighten;
mix-blend-mode: color-dodge;
/* Contrast blend modes */
mix-blend-mode: hard-light;
mix-blend-mode: soft-light;
/* Comparative blend modes */
mix-blend-mode: difference;
mix-blend-mode: exclusion;
/* Component blend modes */
mix-blend-mode: hue;
mix-blend-mode: saturation;
mix-blend-mode: color;
mix-blend-mode: luminosity;
Controls how an element's background images blend with each other and the background color.
.element {
background-image: url('image1.jpg'), url('image2.jpg');
background-blend-mode: multiply;
}
transform: translate()
and scale()
perform better than changing top
, left
, width
, or height
.will-change: transform;
helps browsers optimize for upcoming animations./* Ways to trigger hardware acceleration */
.accelerated {
transform: translateZ(0);
/* or */
transform: translate3d(0, 0, 0);
/* or */
will-change: transform;
}
Always check browser compatibility and consider providing fallbacks for critical functionality.