UI Code Generator
Gradient Generator
Gradient Generator
Create vibrant, multi-colored transitions for backgrounds or buttons with this visual, interactive CSS gradient generator. Adjust colors, angles, and types (such as linear or radial) and instantly copy clean CSS code. This immediate visual feedback eliminates the need to manually write complex gradient syntax.
CSS
DETAILS EXPLAINED**
Introduction
Historically, CSS Levels 1 and 2 restricted image values—such as those inside the background-image property—to a single URL value. However, this module introduces advanced methods for representing 2D images, including programmatically generated gradients.
Beyond these fresh image formats, several new properties manipulate raster images, allowing developers to size or position replaced elements within the bounding box determined by CSS layout algorithms. Finally, a universal, generic sizing algorithm now governs images and other similar replaced elements across the entire specification.
What is a Gradient?
Fundamentally, a gradient forms an image that smoothly fades from one color to another. Consequently, authors commonly use these transitions for subtle shading in background images, buttons, and various user interface components. The gradient functions in this section simplify this workflow by utilizing a terse syntax. Ultimately, this brief markup enables the User Agent (UA) to generate the image automatically during page rendering.
Specifically, the syntax of a <gradient>:
<linear-gradient()> | <repeating-linear-gradient()> |
<radial-gradient()> | <repeating-radial-gradient()>
Example 1
As with the other <image> types defined in this specification, gradients can be used in any property that accepts images. For example:
background: linear-gradient(white, gray);list-style-image: radial-gradient(circle, #006, #00a 90%, #0000af 100%, white 100%);
It is drawn into a box with the dimensions of the concrete object size, referred to as the gradient box. However, the gradient itself has no natural dimensions.
Example 2
For instance, when a developer applies a gradient as a background, the rendering engine automatically draws the image inside a gradient box that matches the size of the element’s padding box. Applying a gradient as a list-style-image instead prompts the system to scale the box to a 1em square, which represents the default object size for that specific property.
To construct a gradient, developers define the starting and ending points of a gradient line, which may geometrically form a line, a ray, or a spiral depending on the gradient type. Each gradient type applies its unique rendering logic to translate the line’s colors into the final, complete gradient image.
Linear Gradients: the linear-gradient() notation
A linear gradient is created by specifying a straight gradient line, and then several colors placed along that line. This produces a smooth fade from each color to the next, progressing in the specified direction.
linear-gradient() Syntax:
<linear-gradient()> = linear-gradient( [ <linear-gradient-syntax> ] )
<linear-gradient-syntax> = [ <angle> | to <side-or-corner> ] ? . <color-stop-list>
<side-or-corner> = [ left | right ] | | [ top | bottom ]
To establish a direction, developers use the first argument of the function to specify the gradient line and determines how color-stops are positioned. It may be omitted; if so, it defaults to to bottom.
The gradient line’s direction may be specified in two ways:
Using <angle>
For the purpose of this argument, 0deg points upward, and positive angles represent clockwise rotation, so 90deg point toward the right.
The unit identifier may be omitted if the <angle> is zero.
keycolor-words
Alternatively, applying the arguments to top, to right, to bottom, or to left sets the angle of the gradient line to 0deg, 90deg, 180deg, or 270deg, respectively.
When the argument specifies a corner instead—such as to top left—the gradient line must point into the same quadrant as that corner. It must also run perpendicular to a line intersecting the two neighboring corners of the gradient box, ensuring that a color-stop at 50% intersects both neighboring corners.
Applying these specific arguments allows developers to control exactly how the colors transition across the element.
Starting from the center of the gradient box, extend a line at the specified angle in both directions. The ending point sits where a line drawn perpendicular to the gradient line intersects the corner of the gradient box in the specified direction. To determine the starting point, apply this exact same logic in the opposite direction.
NOTE:
It is expected that the next level of this module will provide the ability to define the gradient’s direction relative to the current text direction and writing-mode.
Example 3
This example illustrates visually how a browser calculates the gradient line using the rules above. Both the starting and ending points appear on the map alongside the final gradient image to clarify this behavior. Ultimately, an element executing the style rule background: linear-gradient(45deg, white, black); produces this exact visual layout
Notice how the browser positions the starting and ending points outside of the element’s box boundary. Remarkably, the rendering engine aligns these markers so precisely that the gradient shifts to pure white exactly at one corner and pure black exactly at the opposite corner.

GIVEN:
- A the angle (in any quadrant) defining the gradient line’s direction such that 0 degrees points upwards and positive angles represent clockwise rotation,
- W the width of the gradient box,
- H the height of the gradient box,
The length of the gradient line (between the starting point and ending point) is:
abs(W * sin(A)) + abs(H * cos(A))
Typically, browsers place a gradient’s color-stops along the gradient line between the starting and ending points. However, the rendering engine does not require this specific boundary because the gradient line extends infinitely in both directions. Functionally, the starting and ending points serve merely as arbitrary location markers. Specifically, the starting point defines the baseline location for values like 0% or 0px, while the ending point marks the location for 100%. Consequently, developers can freely assign color-stop positions that fall before 0% or after 100%.
Radial Gradients: the radial-gradient() notation
In a radial gradient, rather than colors smoothly fading from one side of the gradient box to the other as with linear gradients, they instead emerge from a single point and smoothly spread outward in a circular or elliptical shape.
radial-gradient() Syntax:
<radial-gradient()> = radial-gradient( [ <radial-gradient-syntax> ] )
<radial-gradient-syntax> = [ <radial-shape> || <radial-size> ] ? [ at <position> ] ? . <color-stop-list> )
<radial-size> = <radial-extend> | <length [ 0, ∞ ]> | <length-percentage [ 0, ∞ ]> {2}
<radial-extend> = closest-corner | closest-side | farthest-corner | farthest-side
<radial-shape> = circle | ellipse
Example 4
Here is an example of a circular radial gradient 5em wide and positioned with its center in the top left corner:
radial-gradient(5em circle at top left, yellow, blue)
The arguments are defined as follows:
position
Determines the center of the gradient. The value type (which is also used for background-position) is defined in [CSS-VALUES-3], and is resolved using the center-point as the object area and the gradient box as the positioning area. If this argument is omitted, it defaults to center.
radial-shape
The <radial-shape> can be defined as either circle or ellipse to determine whether the browser draws a circular or elliptical gradient. If the developer omits this property, the rendering engine defaults the ending shape to a circle, provided that the <radial-size> contains a single <length> value. Otherwise, the rendering engine automatically defaults the final configuration to an ellipse
radial-size
First, this parameter determines the size of the gradient’s ending shape. If the developer omits this value, the system defaults the size to farthest-corner. To define this size, developers can use either explicit values or specific keywords. Furthermore, keyword definitions require the system to treat the gradient box edges as infinite lines rather than finite line segments.
Whenever the ending shape forms an ellipse, its dimensions align perfectly with the horizontal and vertical orientations.
closest-side
Specifically, the system sizes the ending shape so that it exactly meets the side of the gradient box closest to the gradient’s center. If the shape forms an ellipse, it must exactly meet the closest side in both the horizontal and vertical dimensions.
farthest-size
This keyword operates identically to closest-side. However, the system sizes the ending shape based on the farthest side or sides, rather than the closest.
closest-corner
The system sizes the ending shape so that it passes through the corner of the gradient box closest to the gradient’s center. Furthermore, if the shape forms an ellipse, the system assigns it the exact same aspect ratio it would receive under the closest-side specification
farthest-corner
This keyword operates identically to closest-corner. However, the system sizes the ending shape based on the farthest corner instead of the closest. Elliptical shapes will also adopt the exact same aspect ratio they would receive under the farthest-side specification.
Placing Color Stops
Color-stops are placed on a ray-shaped gradient line. This ray originates at the center of the gradient and extends infinitely to the right, matching the behavior of linear gradients. Its ending point is established at the exact intersection with the ending shape.
Furthermore, developers can place a color-stop before 0%. Even though rendering engines never directly consult this negative region, these stops still matter. Through interpolation or repetition, they actively alter the color of non-negative locations.
Radial Gradient Examples
All of the following examples are applied to a box that is 200px wide and 100px tall.
Example 5
These examples demonstrate different ways to write the basic syntax for radial gradients:
radial-gradient(5em circle at top left, yellow, blue)radial-gradient(5em circle at top left, yellow, blue)radial-gradient(5em circle at top left, yellow, blue)

radial-gradient(circle, yellow, green);

radial-gradient(circle, yellow, green);

Example 5
This image shows a gradient originating from somewhere other than the center of the box:
radial-gradient(farthest-side at left bottom, red, yellow 50px, green);

Example 5
Here we illustrate a closest-side gradient.
radial-gradient(closest-side at 20px 30px, red, yellow, green);radial-gradient(20px 30px at 20px 30px, red, yellow, green);

radial-gradient(closest-side circle at 20px 30px, red, yellow, green);radial-gradient(20px 20px at 20px 30px, red, yellow, green);

Repeating Gradients: the repeating-linear-gradient() and repeating-radial-gradient() notations
In addition to linear-gradient() and radial-gradient(), this specification defines repeating-linear-gradient() and repeating-radial-gradient() values. These notations take the same values and are interpreted the same as their respective non-repeating siblings defined previously.
< repeating-linear-gradient() > = repeating-linear-gradient( [ linear-gradient-syntax ] )
< repeating-radial-gradient() > = repeating-radial-gradient( [ radial-gradient-syntax ] )
Rendering
During rendering, color-stops repeat infinitely in both directions. Multiples of the difference between the final and initial positions dictate how these points shift. To illustrate this, repeating-linear-gradient(red 10px, blue 50px) matches linear-gradient(…, red -30px, blue 10px, red 10px, blue 50px, red 50px, blue 90px, …). Sharp transitions occur because the last and first color-stops always meet at the boundary of each group, unless the gradient starts and ends with the identical color.
Example 6
Repeating gradient syntax is identical to that of non-repeating gradients:
repeating-linear-gradient(red, blue 20px, red 40px)

repeating-radial-gradient(red, blue 20px, red 40px)

repeating-radial-gradient(circle closest-side at 20px 30px, red, yellow, green 100%, yellow 150%, red 200%)

Average Color Fallback
If a tiny, non-zero distance separates the first and last color-stops, the output device may lack the physical resolution to faithfully render the gradient. In this scenario, the implementation must calculate the average color of the gradient. Consequently, the system must render a solid-color image using that calculated average.
Handling Zero-Length Gradients
The system must create a temporary reference gradient whenever the distance between the first and last color-stops equals zero (or rounds to zero due to implementation limits). To do this, the software separates the first and last color-stops by an arbitrary non-zero distance while spacing the remaining stops equally between them. Calculating the average color of this new gradient configuration serves as the next step. Ultimately, the engine renders a solid-color image using that exact average.
Rendering Zero-Height Radial Gradients
Whenever a repeating radial gradient has a non-zero width but a height of zero, the device may struggle to display it. Similarly, the same problem occurs if the height drops so close to zero that the output device lacks the physical resolution to faithfully render the gradient. In either scenario, the implementation calculates the average color of the gradient. The system then uses this calculated average to render a solid-color image.
Copyright © 2023 World Wide Web Consortium. This work is published under the W3C Document License.

You must be logged in to post a comment.