CSS Gradients Guide: Linear/Radial/Conic & Visual Effects

Dev Tools

The Three Types of CSS Gradients

Type Syntax Visual effect
Linear gradient linear-gradient() Transitions along a straight line
Radial gradient radial-gradient() Radiates outward from a center
Conic gradient conic-gradient() Rotates around a center

Linear Gradients (linear-gradient)

Basic Syntax

/* Direction + color stops */
background: linear-gradient(direction, stop1, stop2, ...);

/* Examples */
background: linear-gradient(to right, #667eea, #764ba2);
background: linear-gradient(135deg, #f093fb, #f5576c);
background: linear-gradient(to bottom right, #4facfe, #00f2fe);

Direction Parameters

/* Keyword directions */
to top          /* Bottom to top */
to right        /* Left to right */
to bottom       /* Top to bottom (default) */
to left         /* Right to left */
to top right    /* Diagonal */

/* Angles (more precise) */
0deg    /* → to top */
90deg   /* → to right */
180deg  /* → to bottom */
45deg   /* → top-right diagonal */
135deg  /* → bottom-right diagonal */

Controlling Color Stops

/* Evenly distributed (positions computed automatically) */
background: linear-gradient(red, yellow, green);
/* red: 0%, yellow: 50%, green: 100% */

/* Explicit positions */
background: linear-gradient(red 0%, yellow 30%, green 100%);

/* Hard edge (no transition) */
background: linear-gradient(red 50%, blue 50%);

/* Multi-segment gradient */
background: linear-gradient(
  red 0%, red 20%,
  yellow 20%, yellow 40%,
  green 40%, green 60%,
  blue 60%, blue 80%,
  purple 80%, purple 100%
);

Radial Gradients (radial-gradient)

Basic Syntax

background: radial-gradient(shape size at position, stops...);

/* Examples */
background: radial-gradient(circle, #ff6b6b, #556270);
background: radial-gradient(ellipse at top, #ffd89b, #19547b);
background: radial-gradient(circle 100px at center, #fff, #000);

Shapes

circle    /* Circle */
ellipse   /* Ellipse (default) */

Size Keywords

closest-side     /* Up to the nearest side */
farthest-side    /* Up to the farthest side */
closest-corner   /* Up to the nearest corner */
farthest-corner  /* Up to the farthest corner (default) */

Position

at center           /* Centered (default) */
at top left         /* Top-left corner */
at 30% 70%          /* Percentage positioning */
at 100px 200px      /* Pixel positioning */

Conic Gradients (conic-gradient)

Basic Syntax

background: conic-gradient(from start-angle at position, stops...);

/* Examples */
background: conic-gradient(red, yellow, green, blue, red);
background: conic-gradient(from 45deg, #f00, #0f0, #00f, #f00);

In Practice: Pie Chart

.pie-chart {
  background: conic-gradient(
    #3b82f6 0% 35%,
    #ef4444 35% 55%,
    #22c55e 55% 80%,
    #f59e0b 80% 100%
  );
  border-radius: 50%;
}

In Practice: Color Wheel

.color-wheel {
  background: conic-gradient(
    hsl(0, 100%, 50%),
    hsl(60, 100%, 50%),
    hsl(120, 100%, 50%),
    hsl(180, 100%, 50%),
    hsl(240, 100%, 50%),
    hsl(300, 100%, 50%),
    hsl(360, 100%, 50%)
  );
  border-radius: 50%;
}

Advanced Visual Effects

1. Gradient Layering (Multiple Gradients)

.multi-gradient {
  background:
    linear-gradient(217deg, rgba(255,0,0,.8), rgba(255,0,0,0) 70.71%),
    linear-gradient(127deg, rgba(0,255,0,.8), rgba(0,255,0,0) 70.71%),
    linear-gradient(336deg, rgba(0,0,255,.8), rgba(0,0,255,0) 70.71%);
}

2. Grid Background

.grid-bg {
  background:
    linear-gradient(rgba(0,0,0,0.1) 1px, transparent 1px),
    linear-gradient(90deg, rgba(0,0,0,0.1) 1px, transparent 1px);
  background-size: 20px 20px;
}

3. Dot-Matrix Background

.dot-bg {
  background: radial-gradient(circle 2px, #999 100%, transparent 100%);
  background-size: 20px 20px;
}

4. Shine Effect

.shine-button {
  position: relative;
  overflow: hidden;
  background: #3b82f6;
}

.shine-button::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(
    105deg,
    transparent 40%,
    rgba(255,255,255,0.4) 45%,
    rgba(255,255,255,0.4) 55%,
    transparent 60%
  );
  animation: shine 3s infinite;
}

@keyframes shine {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(100%); }
}

5. Glassmorphism

.glass-card {
  background: linear-gradient(
    135deg,
    rgba(255, 255, 255, 0.1),
    rgba(255, 255, 255, 0.05)
  );
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 16px;
}

6. Gradient Text

.gradient-text {
  background: linear-gradient(135deg, #667eea, #764ba2);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

7. Gradient Borders

.gradient-border {
  position: relative;
  background: #1f2937;
  border-radius: 12px;
}

.gradient-border::before {
  content: '';
  position: absolute;
  inset: -2px;
  background: linear-gradient(135deg, #667eea, #764ba2, #f093fb);
  border-radius: 14px;
  z-index: -1;
}

8. Animated Gradients

.animated-gradient {
  background: linear-gradient(
    -45deg,
    #ee7752, #e73c7e, #23a6d5, #23d5ab
  );
  background-size: 400% 400%;
  animation: gradient-shift 8s ease infinite;
}

@keyframes gradient-shift {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

Repeating Gradients

/* Repeating linear gradient → stripes */
.repeating-linear {
  background: repeating-linear-gradient(
    45deg,
    #3b82f6,
    #3b82f6 10px,
    #60a5fa 10px,
    #60a5fa 20px
  );
}

/* Repeating radial gradient → bullseye */
.repeating-radial {
  background: repeating-radial-gradient(
    circle,
    #3b82f6,
    #3b82f6 10px,
    #60a5fa 10px,
    #60a5fa 20px
  );
}

/* Repeating conic gradient → radar */
.repeating-conic {
  background: repeating-conic-gradient(
    #3b82f6 0deg 30deg,
    #60a5fa 30deg 60deg
  );
}

Using the ToolsKu Gradient Generator

  1. Open the CSS Gradient Generator
  2. Choose a gradient type (linear/radial/conic)
  3. Add color stops and adjust their positions and colors
  4. Adjust the direction/angle
  5. Preview the result in real time
  6. Copy the generated CSS code

Gradient Performance Optimization

Tip Explanation
Avoid too many color stops 3-5 stops are ideal; more than 8 hurts rendering
Prefer hex over rgba #ff000080 is shorter than rgba(255,0,0,0.5)
Keep gradient areas small Large gradient areas increase paint cost
Animate background-position Don't animate color-stop values
will-change: background Add the hint for animated gradients

Browser Compatibility

Feature Chrome Firefox Safari
linear-gradient ✅ All ✅ All ✅ All
radial-gradient ✅ All ✅ All ✅ All
conic-gradient 69+ 83+ 12.1+
repeating-conic-gradient 69+ 83+ 12.1+
Multiple gradients ✅ All ✅ All ✅ All
background-clip: text ✅ 49+ ✅

Summary

CSS gradients are the Swiss Army knife of visual design — from simple two-color transitions to complex grid backgrounds, shine effects, and glassmorphism, gradients handle it all. Master the three types (linear/radial/conic) + color-stop control + multi-layer stacking, and you can achieve the vast majority of visual effects. The ToolsKu Gradient Generator lets you tweak parameters visually and copy the CSS code in one click.

#CSS渐变#渐变生成器#设计工具#UI设计#教程