How to Create CSS Gradients

Featured graphic for “How to Create CSS Gradients” showing color swatches and HEX codes

CSS gradients let you blend colors smoothly without image files. They load instantly, scale to any screen size and are easy to change. This guide covers the syntax you need, with examples you can copy.

Linear Gradients

A linear gradient blends colors along a straight line:

background: linear-gradient(135deg, #6C5CE7, #22D3EE);

Direction and angle

  • to right or 90deg — left to right
  • to bottom or 180deg — top to bottom (the default)
  • 135deg — top-left to bottom-right
  • 0deg — bottom to top

Color Stops

Add as many colors as you like, and control where each one sits with a percentage:

background: linear-gradient(90deg, #F97316 0%, #F43F5E 50%, #8B5CF6 100%);

Placing two stops at the same position creates a hard edge, useful for stripes:

background: linear-gradient(90deg, #111827 50%, #F8FAFC 50%);

Radial Gradients

A radial gradient spreads outward from a center point:

background: radial-gradient(circle, #FDE68A, #F59E0B);

You can move the center: radial-gradient(circle at top left, ...).

Gradients With Transparency

Fading to transparent is perfect for image overlays so white text stays readable:

background:
  linear-gradient(to top, rgba(17,24,39,.85), rgba(17,24,39,0)),
  url(photo.jpg) center / cover;

Gradient Text

.headline {
  background: linear-gradient(90deg, #6C5CE7, #4F46E5);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

Tips for Better Gradients

  • Use neighboring hues. Opposite hues (like red to cyan) can pass through a dull grey middle. Add a middle stop to keep it vivid.
  • Add a solid fallback. Put a plain background color before the gradient line.
  • Keep text readable. Check contrast against both the lightest and darkest part of the gradient with the Contrast Checker.
  • Subtle is often better. Two close shades of one hue look premium and calm.

Build Gradients Visually

Instead of writing code by hand, use our CSS Gradient Generator: choose colors, drag the angle, add up to six stops and copy the finished CSS — or download a PNG.

Conic Gradients

Linear and radial gradients are the most common, but CSS also supports conic gradients. A conic gradient rotates colors around a center point, like the hands of a clock, instead of moving outward from it. This makes it perfect for pie charts, color wheels, loading spinners and decorative backgrounds.

.wheel {
  background: conic-gradient(red, yellow, lime, aqua, blue, magenta, red);
  border-radius: 50%;
}

Ending with the same color you started with prevents a hard seam at the top of the circle. You can also control the starting angle and the center position:

background: conic-gradient(from 90deg at 30% 50%, #FF6B6B, #4ECDC4, #FF6B6B);

Pie charts with pure CSS

Conic gradients with hard color stops create simple pie charts without images or JavaScript:

.pie {
  background: conic-gradient(#3A86FF 0 40%, #FFBE0B 40% 70%, #FB5607 70% 100%);
  border-radius: 50%;
}

Here the blue slice takes 40%, the yellow slice 30% and the orange slice the remaining 30%. Because two percentages are given for each color, there is no blending between slices.

Repeating Gradients and Patterns

Each gradient type has a repeating version: repeating-linear-gradient(), repeating-radial-gradient() and repeating-conic-gradient(). They repeat the defined color stops to fill the element, which lets you build stripes, rings and checkerboards using only CSS.

Diagonal stripes

.stripes {
  background: repeating-linear-gradient(
    45deg,
    #F1F5F9 0 10px,
    #E2E8F0 10px 20px
  );
}

This creates soft diagonal stripes often used for placeholder areas, progress bars and “under construction” sections.

Checkerboard

.checker {
  background: repeating-conic-gradient(#ddd 0 25%, #fff 0 50%) 0 0 / 40px 40px;
}

This pattern is commonly used behind transparent images to show which areas are see-through.

Concentric rings

.rings {
  background: repeating-radial-gradient(circle, #E0F2FE 0 12px, #BAE6FD 12px 24px);
}

Layering Multiple Gradients

The background property accepts a comma-separated list of layers. The first layer sits on top, and each following layer sits below it. Combining several semi-transparent gradients produces rich effects that a single gradient cannot create.

.hero {
  background:
    radial-gradient(circle at 20% 20%, rgba(255, 107, 107, .45), transparent 40%),
    radial-gradient(circle at 80% 30%, rgba(78, 205, 196, .45), transparent 40%),
    radial-gradient(circle at 50% 90%, rgba(255, 190, 11, .40), transparent 45%),
    #0F172A;
}

This is the technique behind the popular “mesh gradient” or “aurora” backgrounds seen on modern landing pages. Each radial gradient creates a soft glowing blob, and the dark base color fills the rest of the area.

Gradient overlays on images

Layering also lets you place a gradient over a photo to improve text readability:

.banner {
  background:
    linear-gradient(to top, rgba(0,0,0,.7), rgba(0,0,0,0) 60%),
    url("photo.jpg") center / cover;
}

The dark gradient at the bottom ensures white headings remain readable no matter what colors appear in the image.

Gradient Borders and Buttons

Gradient border

The border property cannot take a gradient directly, but you can achieve the effect with two background layers and a transparent border:

.gradient-border {
  border: 3px solid transparent;
  border-radius: 12px;
  background:
    linear-gradient(#fff, #fff) padding-box,
    linear-gradient(135deg, #8B5CF6, #EC4899) border-box;
}

The first layer paints the inside white, limited to the padding box. The second layer paints the gradient across the border box, so only the border area shows the gradient.

Gradient buttons with hover effects

Gradients cannot be animated smoothly with transition in most browsers, but you can fake the effect by making the background larger than the button and moving it on hover:

.btn {
  background: linear-gradient(90deg, #6366F1, #EC4899, #6366F1);
  background-size: 200% 100%;
  transition: background-position .4s ease;
}
.btn:hover { background-position: 100% 0; }

Because the gradient begins and ends with the same color, the shift looks seamless.

Choosing Colors That Blend Well

Not every pair of colors creates a pleasing gradient. Some combinations produce a gray or muddy band in the middle. This happens because CSS blends colors in the sRGB color space by default, and the straight line between two opposite hues passes through gray.

  • Use neighboring hues. Blue to purple, orange to pink and yellow to green blend cleanly because they are close on the color wheel.
  • Add a middle stop. When going from blue #2563EB to yellow #FACC15, insert a green such as #22C55E in between to avoid the gray center.
  • Keep lightness similar. Colors with similar brightness transition more smoothly than a very dark color next to a very light one.
  • Try modern color spaces. Newer browsers support interpolation in other color spaces, for example linear-gradient(in oklch, #2563EB, #FACC15), which keeps the middle vibrant.

Sunset: #FF7E5F to #FEB47B. Ocean: #2193B0 to #6DD5ED. Berry: #8E2DE2 to #4A00E0. Mint fresh: #43E97B to #38F9D7. Peach soft: #FFECD2 to #FCB69F. Night sky: #0F2027 to #2C5364.

Performance and Browser Support

CSS gradients are generated by the browser, so they do not require extra downloads. This makes them faster than background images and keeps them sharp on every screen size and resolution. A full-screen gradient background weighs only a few bytes of CSS compared with tens or hundreds of kilobytes for an image.

Linear, radial and repeating gradients are supported in all modern browsers. Conic gradients are supported in current versions of Chrome, Edge, Firefox and Safari. Interpolation in color spaces such as OKLCH is newer, so include a standard gradient first as a fallback:

.box {
  background: linear-gradient(90deg, #2563EB, #FACC15);
  background: linear-gradient(in oklch 90deg, #2563EB, #FACC15);
}

Browsers that do not understand the second line simply ignore it and keep the first.

Avoiding banding

On large areas, a gradient between two very similar colors can show visible steps called banding. To reduce it, use slightly more contrast between the stops, add a middle stop or place a very subtle noise texture over the gradient.

Accessibility Tips for Gradients

  • Check text contrast against the lightest and darkest parts of the gradient, not just the average color.
  • Do not rely on a gradient alone to communicate meaning, such as showing good or bad values. Add labels or icons too.
  • Avoid fast animated gradients behind text; they can distract users and affect people sensitive to motion. Respect the prefers-reduced-motion setting.
  • Keep gradient text for large headings only, and always set a solid fallback color in case the clip effect is not supported.

Real-World Gradient Examples

Hero section

.hero {
  min-height: 70vh;
  background: linear-gradient(135deg, #667EEA 0%, #764BA2 100%);
  color: #fff;
}

Subtle card background

.card {
  background: linear-gradient(180deg, #FFFFFF 0%, #F8FAFC 100%);
  border: 1px solid #E2E8F0;
}

Progress bar

.progress-fill {
  background: linear-gradient(90deg, #22C55E, #84CC16);
  width: 65%;
}

Small, purposeful gradients like these add depth and polish without overwhelming the content.

Understanding Gradient Direction and Angles

Direction is one of the most important choices in a linear gradient, and it is also one of the most misunderstood. CSS gives you two ways to set it: keywords and angles.

Keywords such as to right, to bottom left or to top describe where the gradient ends. They are easy to read and adapt automatically to the shape of the element. For example, to bottom right always runs exactly from the top-left corner to the bottom-right corner, even if the box is very wide or very tall.

Angles give you precise control. In CSS, 0deg points upward, 90deg points to the right, 180deg points down and 270deg points to the left. This is different from the mathematical convention many people learned in school, where 0 degrees points right. If your gradient seems to go the wrong way, this difference is usually the reason.

A useful rule of thumb: diagonal gradients at around 120 to 150 degrees feel natural to most people because light in the real world usually comes from above. Gradients that get darker toward the bottom look grounded and stable, while gradients that get darker toward the top can feel heavy or unusual.

Color Stop Positions Explained in Depth

Every color stop can have a position written as a percentage or a length. If you leave the positions out, the browser spreads the colors evenly. Adding positions lets you control how quickly one color changes into the next.

  • Close stops create a fast, sharp transition. Two colors at 49% and 51% look almost like a hard line.
  • Distant stops create a long, gentle blend. A color at 0% and the next at 100% produces the softest possible change.
  • Identical positions create hard edges with no blending at all. This is how flags, stripes and progress indicators are made.
  • Color hints are a single percentage placed between two colors without a color. They move the midpoint of the blend, letting you push the transition closer to one side.

Experimenting with stop positions is the fastest way to turn a basic two-color gradient into something that looks custom-designed. Even small changes, such as moving the first color to 20% instead of 0%, can make a hero background feel more balanced.

Gradients in Branding and Marketing

Many well-known digital products use gradients as a core part of their visual identity. A brand gradient works like a signature: once people have seen it a few times, they recognize it immediately on social media, packaging and app icons.

If you want to create a brand gradient, start with your primary brand color and choose one neighboring hue that shares a similar lightness. Test the gradient at different sizes, because a gradient that looks rich on a large banner may look flat on a small icon. Write the exact HEX codes, direction and stop positions in your brand guidelines so designers and developers reproduce it the same way every time.

In marketing, gradients help draw attention to key areas. A gradient background behind a pricing table, a newsletter signup box or a special offer makes that section stand out from the rest of the page without needing loud images. Used sparingly, they guide the eye toward the action you want visitors to take.

Common Gradient Mistakes to Avoid

  1. Too many colors. Five or six colors in one gradient often look chaotic. Two or three well-chosen colors usually look more professional.
  2. Muddy middles. Blending complementary colors directly, such as red to green, creates a brownish center. Add a bridge color or choose closer hues.
  3. Poor text contrast. Text that is readable at one end of a gradient may disappear at the other end.
  4. Using gradients everywhere. When every button, card and section has a gradient, nothing stands out. Reserve them for important elements.
  5. Ignoring dark mode. A bright gradient that looks great on a white page can feel harsh on a dark theme. Provide a softer variation for dark mode.
  6. Forgetting fallbacks. Always set a solid background-color before the gradient so the element still looks acceptable if the gradient fails to load or is overridden.

Step-by-Step: Designing a Gradient from Scratch

  1. Define the purpose. Decide whether the gradient is a background, a button, a border or a text effect. This affects how bold it can be.
  2. Choose a starting color. Use your brand color or a color that matches the mood of the page.
  3. Choose an ending color. Pick a neighbor on the color wheel for a smooth result, or a more distant hue for a bolder look.
  4. Set the direction. Try 135 degrees as a default and adjust based on the layout.
  5. Fine-tune stops. Move the stops or add a color hint until the transition feels balanced.
  6. Check contrast and readability. Place real text and buttons on top and test them.
  7. Copy the code and save it. Store the final gradient as a CSS custom property so you can reuse it across the website.

Saving and Reusing Gradients with CSS Variables

Once you have a gradient you like, store it in a custom property instead of copying the full code into every rule. This keeps your stylesheet short and makes design updates fast.

:root {
  --brand-gradient: linear-gradient(135deg, #667EEA, #764BA2);
  --soft-gradient: linear-gradient(180deg, #FFFFFF, #F1F5F9);
}
.hero   { background: var(--brand-gradient); }
.button { background: var(--brand-gradient); }
.panel  { background: var(--soft-gradient); }

If the marketing team later decides to shift the brand toward teal, you edit one line and every hero, button and banner updates instantly. You can also redefine the variables inside a dark mode media query, so the same class automatically uses a darker, softer gradient when visitors prefer a dark theme.

This approach also helps teams stay consistent. Designers can reference the variable names in their style guide, and developers know exactly which gradient belongs to which component, avoiding slightly different copies of the same colors spread across the code.

Frequently Asked Questions

Are CSS gradients supported in all browsers?

Yes. linear-gradient() and radial-gradient() work in all modern browsers without vendor prefixes.

Are gradients better than images?

For simple color blends, yes. They are lighter, sharper on every screen and easier to edit.

Can I animate a gradient?

You can animate background-position on an oversized gradient. Use it sparingly and respect users who prefer reduced motion.

Written by the Mage Color Picker Team

We build free online color tools and write practical guides on color theory, color codes and accessible design for designers and developers.

Scroll to Top