CSS Transforms and Effects Guide

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.

1. CSS Transforms

CSS transforms allow you to rotate, scale, skew, or translate elements in 2D or 3D space.

2D Transforms

translate()
Hover Me
.translate-xy:hover {
  transform: translate(30px, 20px);
}

/* Individual axes */
.translate-x:hover {
  transform: translateX(50px);
}

.translate-y:hover {
  transform: translateY(20px);
}
scale()
Hover Me
.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()
Hover Me
.rotate-2d:hover {
  transform: rotate(45deg);
}
skew()
Hover Me
.skew-xy:hover {
  transform: skew(15deg, 10deg);
}

/* Individual axes */
.skew-x:hover {
  transform: skewX(20deg);
}

.skew-y:hover {
  transform: skewY(10deg);
}
transform-origin
Top Left
.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;
}

3D Transforms

rotateX(), rotateY(), rotateZ()
Hover Me
.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);
}
translateZ() and translate3d()
Hover Me
.translate-z:hover {
  transform: translateZ(50px);
}

.translate-3d:hover {
  transform: translate3d(20px, 20px, 50px);
}
Multiple Transforms
Hover Me
.multiple-transforms:hover {
  transform: rotate(45deg) scale(1.2) translateX(20px);
}

Note: Transforms are applied from right to left.

Perspective

perspective: 100px
Hover Me
.perspective-100 {
  perspective: 100px;
}

Closer perspective creates more extreme 3D effects.

perspective: 500px
Hover Me
.perspective-500 {
  perspective: 500px;
}

Medium perspective creates moderate 3D effects.

perspective: 1000px
Hover Me
.perspective-1000 {
  perspective: 1000px;
}

Distant perspective creates subtle 3D effects.

Backface Visibility

backface-visibility: visible
Front
Back
.backface-box {
  backface-visibility: visible; /* Default */
}

.backface-front {
  z-index: 2;
}

.backface-back {
  transform: rotateY(180deg);
}
backface-visibility: hidden
Front
Back
.backface-hidden .backface-box {
  backface-visibility: hidden;
}

The back side of elements is invisible when rotated away from view.

3D Space

transform-style: preserve-3d
Front
Back
Right
Left
Top
Bottom
.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);
}

Practical Example: Flip Card

Hover Me

Hello!

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

2. CSS Filters

CSS filters allow you to apply graphical effects to elements. Hover over each image to see the filter in action.

blur()
Placeholder
.filter-blur:hover {
  filter: blur(5px);
}
brightness()
Placeholder
.filter-brightness:hover {
  filter: brightness(1.5);
}
contrast()
Placeholder
.filter-contrast:hover {
  filter: contrast(200%);
}
grayscale()
Placeholder
.filter-grayscale:hover {
  filter: grayscale(100%);
}
hue-rotate()
Placeholder
.filter-hue-rotate:hover {
  filter: hue-rotate(90deg);
}
invert()
Placeholder
.filter-invert:hover {
  filter: invert(100%);
}
opacity()
Placeholder
.filter-opacity:hover {
  filter: opacity(50%);
}
saturate()
Placeholder
.filter-saturate:hover {
  filter: saturate(200%);
}
sepia()
Placeholder
.filter-sepia:hover {
  filter: sepia(100%);
}
drop-shadow()
Placeholder
.filter-shadow:hover {
  filter: drop-shadow(5px 5px 5px rgba(0,0,0,0.5));
}
Multiple Filters
Placeholder
.filter-multiple:hover {
  filter: contrast(150%) brightness(120%) sepia(30%);
}

Filter Performance

Filters can be CPU-intensive. Consider the following tips:

3. Clipping and Masking

clip-path

The clip-path property creates a clipping region that sets what part of an element should be shown.

circle()
Circle Clip
.clip-circle {
  clip-path: circle(50% at 50% 50%);
}

/* Hover animation */
.clip-circle:hover {
  clip-path: circle(40% at 50% 50%);
}
polygon()
Polygon Clip
.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%);
}
ellipse()
Ellipse Clip
.clip-ellipse {
  clip-path: ellipse(50% 30% at 50% 50%);
}
inset()
Inset Clip
.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);
}

mask-image

The mask-image property creates a mask layer that hides parts of an element.

SVG Mask
.mask-demo {
  background: linear-gradient(45deg, #3498db, #e74c3c);
  mask-image: url('data:image/svg+xml;utf8,');
  mask-size: cover;
}

4. Blend Modes

Blend modes control how an element's content blends with the background or underlying content.

mix-blend-mode

Controls how an element's content blends with the content of its parent and backdrop.

mix-blend-mode: multiply
.blend-top {
  mix-blend-mode: multiply;
}
mix-blend-mode: screen
.blend-top {
  mix-blend-mode: screen;
}
mix-blend-mode: overlay
.blend-top {
  mix-blend-mode: overlay;
}
mix-blend-mode: difference
.blend-top {
  mix-blend-mode: difference;
}

Available Blend Modes

/* 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;

background-blend-mode

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

5. Performance Considerations

Transform Performance

Triggering Hardware Acceleration

/* Ways to trigger hardware acceleration */
.accelerated {
  transform: translateZ(0);
  /* or */
  transform: translate3d(0, 0, 0);
  /* or */
  will-change: transform;
}

Filter Performance

6. Browser Support

Always check browser compatibility and consider providing fallbacks for critical functionality.