CSS Linear Gradients: Syntax and Practical Examples
Learn the CSS linear-gradient() syntax — directions, angles, color stops — with two worked examples you can paste into a page and test.
A flat background colour is safe, but it is also forgettable. A linear gradient — one colour fading smoothly into another along a straight line — is the smallest change that makes a hero banner, a button, or a card feel designed rather than default. It needs no image file, no extra download, and no JavaScript: one CSS declaration does the work.
This guide explains the linear-gradient() syntax piece by piece, then works through two exact examples you can paste into a page and test.
Linear-gradient basics
A linear gradient is a CSS image. That sounds odd — there is no file involved — but linear-gradient() is a function that generates an image value, which is why it is usually assigned to background-image rather than background-color. The browser paints it when it renders the element, so it scales to any size without losing quality and costs nothing to download.
The simplest possible gradient needs only two colours:
.banner {
background-image: linear-gradient(#2563EB, #7C3AED);
}
This paints a box that starts blue at the top and fades smoothly into violet at the bottom. No direction is given, so the browser uses the default: top to bottom. That default is worth remembering, because it explains the most common surprise beginners meet — they write a gradient expecting a left-to-right fade and get a vertical one instead.
Three rules govern the rest of this guide. A gradient needs at least two colour stops. The colours are painted along a straight line, called the gradient line, whose direction decides where each colour appears. And the transition between two stops is smooth by default — a sharp edge must be asked for explicitly, as the second worked example shows.
A large background gradient can be bold; one behind text must be subtle, with low contrast between its stops.
Write the syntax
The full formal syntax looks intimidating, but each part is optional except the colour stops:
linear-gradient(
[ [ <angle> | to <side-or-corner> ] || <color-interpolation-method> ]?,
<color-stop-list>
)
The first part, before the comma, sets the direction and optionally the colour space used for blending. The second part is the colours and where each one sits. Leave the first part out and you get the default direction (top to bottom) and the default blending.
Direction: keywords and angles
Direction can be given as keywords or as an angle, but not both. The keyword form starts with to followed by one or two sides. The keyword names the side the gradient travels towards — to right means the first colour is on the left and the last on the right.
background-image: linear-gradient(to right, #2563EB, #7C3AED);
background-image: linear-gradient(to bottom right, #2563EB, #7C3AED);
The angle form uses degrees, and the mapping is the part people get wrong: in CSS, 0deg points upward — equivalent to to top — and the angle increases clockwise. So 90deg is to right, 180deg is to bottom (the default), and 270deg is to left. A diagonal such as 135deg is the same as to bottom right.
Use keywords for a plain side or corner — they read clearly. Use angles for a direction keywords cannot express, such as 115deg, or when a design specifies an exact angle.
Colour stops: positions and hard edges
A colour stop is a colour plus an optional position: a percentage or a length along the gradient line that says where that colour is fully present. Between two stops, the browser blends smoothly from one colour to the next. Without a position, stops are spaced evenly.
/* Blue for the first 30%, then a blend into violet */
background-image: linear-gradient(to right, #2563EB 30%, #7C3AED);
/* Three stops: blue, then teal at the middle, then violet */
background-image: linear-gradient(to right, #2563EB, #25A8EB 50%, #7C3AED);
A stop can also carry two positions, which creates a hard edge instead of a blend. The colour holds solid between its two positions, and the next colour begins exactly where it ends:
/* A hard split: blue for the left half, violet for the right half */
background-image: linear-gradient(to right, #2563EB 50%, #7C3AED 50%);
The blue is told to reach 50% and the violet to start at 50%, so there is no space for a blend. This two-position trick builds striped and split-colour effects with a single gradient, and it is the technique behind the second worked example below.
One warning: a stop placed before the previous stop's position produces no error — the browser clamps it, usually creating an unintended hard edge.
Transparency and colour spaces
Stops can be transparent. Using transparent or an rgba() colour fades the gradient into whatever sits underneath — a photograph or the page background. This is the standard way to build the dark overlay that makes white headline text readable over a photo:
/* Fades from transparent at the top to dark at the bottom */
background-image: linear-gradient(to bottom, rgba(15, 23, 42, 0), rgba(15, 23, 42, 0.85));
The newest part of the syntax is the colour-interpolation method, written with the in keyword: linear-gradient(to right in oklch, #2563EB, #7C3AED). By default the browser blends in sRGB, which can pass through a dull greyish midpoint when the two colours are far apart on the wheel. Asking for in oklch or in lab blends in a perceptually uniform space, so the midpoint stays vivid. Older browsers ignore a declaration they do not understand, so put a plain two-colour gradient first as a fallback.
Worked example 1: a hero banner gradient
The first example is the most common real-world use: the background of a hero banner. The brief is simple — a blue-to-violet diagonal that feels bright but still lets white text sit on top of it.
Start with the flat version, so the difference is visible:
/* Before: flat background, no gradient */
.hero {
background-color: #2563EB;
color: #FFFFFF;
padding: 4rem 2rem;
}
Rendered, this is a solid medium-blue rectangle: readable, but nothing leads the eye from the headline to the button.
Now replace the flat colour with the gradient:
/* After: diagonal gradient, blue to violet */
.hero {
background-image: linear-gradient(135deg, #2563EB 0%, #7C3AED 100%);
color: #FFFFFF;
padding: 4rem 2rem;
}
Rendered, the banner now runs from blue in the top-left corner to violet in the bottom-right. The white headline and button sit on the same colours as before at the edges, so readability is unchanged, but the diagonal gives the section depth and a direction: the eye follows the colour change from the headline down towards the call to action.
The stops are written out explicitly (0% and 100%) even though those are the defaults, so a later edit can add a third colour without guessing hidden defaults.
Check readability at the lightest and darkest points, not the average. White text works here because both end colours are mid-dark; if either end were pale, darken that stop or add a dark overlay as shown above.
Worked example 2: a three-stop button with a hard edge
The second example is a button whose background is a gradient with a hard split: the left half one colour, the right half another, with a crisp line between them. This is a common pattern for buttons that show two states at once, such as a progress-style button or a before/after toggle.
/* Before: a plain two-colour blend (soft, no edge) */
.button-split {
background-image: linear-gradient(to right, #2563EB, #7C3AED);
color: #FFFFFF;
padding: 0.75rem 1.5rem;
border: none;
border-radius: 0.5rem;
}
Rendered, this button fades gently from blue on the left to violet on the right. The transition is so soft that the two colours are hard to tell apart in the middle, and the button reads as a single vaguely purple shape rather than two deliberate halves.
The fix is the double-position stop from the syntax section:
/* After: hard split at the middle, blue left, violet right */
.button-split {
background-image: linear-gradient(to right, #2563EB 50%, #7C3AED 50%);
color: #FFFFFF;
padding: 0.75rem 1.5rem;
border: none;
border-radius: 0.5rem;
}
Rendered, the button is now exactly half blue and half violet, divided by a sharp vertical line down the centre. Nothing else changed — same colours, same direction, same size — only the stop positions.
Both examples were built with FBSubNet's CSS Linear Gradient Builder, which previews a two-colour gradient and returns the declaration to paste into a stylesheet. Use it to test a combination before committing it to a page.
Test in a page
A gradient that looks right in a tool preview can still surprise you in a real page, because a gradient stretches to fill whatever it is applied to. Test it in a minimal page of your own: one HTML file, the gradient on a box the same size as the real one.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Gradient test</title>
<style>
.test-box {
width: 600px;
height: 200px;
background-image: linear-gradient(135deg, #2563EB, #7C3AED);
}
</style>
</head>
<body>
<div class="test-box"></div>
</body>
</html>
Open that file in a browser and check three things: the direction (mirrored means a to keyword or an angle was read backwards), the blend (a grey or muddy middle points at the colour-space issue above), and the edges (an unasked-for hard line points at a stop position out of order).
Then resize the window: a diagonal that looks gentle at desktop width can look steep on a narrow phone screen, so test at the widths the element will actually be seen at.
Finally, check the gradient against the real headline, in the real font size, at arm's length. If any word is hard to read, adjust the stops until the whole text area is safe. A related guide, How to Build a Consistent Website Color Palette, shows how to pick the colours and check every text pair first.
Common mistakes
The most frequent mistake is forgetting the default direction is top to bottom. If a gradient runs the wrong way, the direction — or its absence — is the first suspect.
The second mistake is choosing two colours far apart on the colour wheel and being surprised by the muddy middle. A red-to-green blend passes through a brownish grey in sRGB. Pick closer colours, add a middle stop, or blend in oklch.
The third mistake is using a gradient behind text without checking the lightest stretch. Check the text against the lightest part of the gradient, not the darkest, and if the lightest part fails, darken that stop or add a dark overlay.
The fourth mistake is writing stop positions out of order and getting a mystery hard edge. The browser does not warn you; it clamps the stop and paints the edge.
Frequently asked questions
What is the simplest linear-gradient syntax? linear-gradient(color1, color2) — two colours, top to bottom. Add to right or an angle after the opening parenthesis to change the direction.
Why does my gradient look grey in the middle? The default sRGB blend between two distant hues passes through a desaturated midpoint. Add a middle stop in the hue you want, or blend in oklch to keep the midpoint vivid.
Can I use a gradient on text? Yes, with background-clip: text and a transparent text colour. It works best on large, bold headings — thin text over a multi-colour gradient is often hard to read.
Do I need a fallback for older browsers? For current browsers, no. For very old ones, put a plain background-color first.
Putting it together
A linear gradient is one function with four ideas: a direction; a list of colour stops; hard edges from two-position stops; and, when the blend needs help, a colour space chosen with in. The two worked examples use only the first three, and they cover most of what a real page asks for.
Write the declaration, test it at the real size, and check the direction, the middle of the blend, and the text sitting on it.