Image to CSS Gradient
Turn a picture into a CSS linear-gradient that follows its colours across the frame, or blends its dominant colours, with every stop worked out and ready to copy.
How to Use
- Drop an image on the box, click to browse, or paste one. TIFF, PSD, HEIC, TGA and camera RAW files open too. A built-in sample is loaded so you can try the presets straight away.
- Choose a method: Follow the image averages strips across the picture in the direction of the gradient, so a sunset becomes sky above and sea below; Dominant colours blends the main colours from darkest to lightest.
- Set the number of stops and the angle: 180° runs top to bottom, 90° left to right. The preview sits beside the picture for comparison.
- Click Copy CSS and paste the line into your stylesheet. Show Work lists every stop: which strip or colour group it came from and where it sits.
- To use the gradient as an image instead, save it from the Save panel at the picture’s own size.
—
Worked Example
An 800 × 600 landscape, 5 stops at 180°. At 180° the gradient line is |800 × sin 180°| + |600 × cos 180°| = 0 + 600 = 600 px, straight down the picture. Five strips of 120 rows each are averaged: rows 0–119 give the first colour at 10%, rows 120–239 the second at 30%, and so on to 90%. The result reads like linear-gradient(180deg, #… 10%, #… 30%, #… 50%, #… 70%, #… 90%), sky at the top and ground at the bottom.
The same picture at 135°. Now the line runs from the top-left corner to the bottom-right: |800 × 0.7071| + |600 × −0.7071| = 989.95 px, so each of the 5 strips is a diagonal band 198 px wide. The ends of the line are placed so that 0% and 100% fall exactly on the two corners, as the CSS specification requires.
The common mistake: 0deg for top to bottom. In CSS, 0deg points up, so linear-gradient(0deg, …) puts the first stop at the bottom. Stops taken from the top of a picture then appear upside down, with the ground colour in the sky. Use 180deg (or to bottom) for a top-to-bottom gradient, which is what the angle box here assumes.
Show Work
Formulas
CSS Gradients and Median Cut
CSS gradients are defined in the W3C CSS Images Module Level 3. A linear gradient is drawn along a gradient line through the centre of the box: for an angle, 0deg points upward and positive angles turn clockwise, so 90deg points right. The line’s ends are chosen so that a line square to it passes exactly through the corners, which is why its length is |W sin A| + |H cos A| and why the first and last colours land precisely on opposite corners. Every point of the box takes the colour of the point on the gradient line square to it, which is also how this tool decides which strip each pixel belongs to.
The dominant-colour method uses median cut, the colour-quantisation algorithm Paul Heckbert described in 1982 in “Color Image Quantization for Frame Buffer Display” (ACM SIGGRAPH Computer Graphics, volume 16). It puts all the pixels in one group, finds the colour channel with the greatest range, and splits the group at the median of that channel, repeating until there are as many groups as colours wanted.
About This Tool
This tool builds a CSS linear-gradient() from a picture with 2 to 12 stops at any angle, either following the picture’s colours along the gradient direction or blending its dominant colours, previews it beside the picture, lists every stop’s source and position, copies the CSS, and saves the gradient as an image. Settings can be put in the page address, for example ?stops=6&angle=135.
Images are decoded in your browser by the engine behind the Universal Image Converter, so TIFF, PSD, HEIC, TGA and RAW files work too. Nothing is uploaded.
Related tools: Duotone Effect, Posterize (Color Levels), and Image Histogram Viewer.
Frequently Asked Questions
How does Follow the image work?
The picture is cut into equal strips square to the gradient line, the pixels in each strip are averaged, and that average becomes a stop placed at the middle of its strip. With 5 stops at 180° on an 800 × 600 picture, each strip is 120 rows, and the stops sit at 10%, 30%, 50%, 70% and 90%.
How are the dominant colours chosen?
By median cut: the pixels are split again and again, each time cutting the group whose widest colour channel has the biggest range at its median, until there are as many groups as stops. Each group’s average is a colour; they are ordered darkest to lightest, and each gets a share of the gradient equal to its share of the picture.
Why do the stops start at 10% instead of 0%?
Each stop stands for a whole strip, so it is placed at the strip’s centre. Before the first stop and after the last, CSS keeps the colour flat, which matches the picture: the top strip of a sky is one colour all the way to the edge.
What does the angle mean in CSS?
In linear-gradient(), 0deg points up and angles turn clockwise, so 90deg runs left to right and 180deg top to bottom. The gradient line through the centre is |W sin A| + |H cos A| long, chosen so the 0% and 100% colours land exactly on opposite corners.
Will the gradient fit boxes of other sizes?
Yes: stop positions are percentages of whatever box the gradient fills, so it stretches. A vertical (180°) gradient looks the same on any width. A diagonal one changes slightly when the box’s shape changes, because the length and corner geometry of the gradient line depend on the width and height.
How do I use the Image to CSS Gradient?
Just type your numbers. The answer shows up right away — there is no button to press. Change anything and it updates by itself.
Does it cost anything or need an account?
No. The tool is completely free, there is no account to create, and it keeps working offline after the page first loads.
Is anything I type uploaded?
No. The tool works entirely on your device, so the values you enter never leave your browser.
Common Use Cases
Hero background from a photo
A 5-stop 180° gradient from a landscape gives a sky-to-ground background that matches the picture beside it.
Image placeholder
Show the gradient while a large photo loads; it is a few dozen bytes of CSS instead of hundreds of kilobytes.
Brand palette from artwork
Dominant colours with 4 stops picks the main colours of a logo or illustration, darkest to lightest.
Email and banner backgrounds
Save the gradient as a PNG at the image’s own size where CSS gradients are not supported.
Last updated: