|
1 | | -<ComingSoon /> |
| 1 | +--- |
| 2 | +title: "The Background Gradient" |
| 3 | +description: "Learn how to use CSS gradient functions (linear and radial) to create smooth, code-generated color transitions as an element's background without using image files." |
| 4 | +keywords: [CSS background gradient, linear-gradient, radial-gradient, background-image, color transition, CodeHarborHub] |
| 5 | +tags: [CSS background gradient, linear-gradient, radial-gradient, background-image, color transition] |
| 6 | +sidebar_label: "background-gradient" |
| 7 | +--- |
| 8 | + |
| 9 | +CSS **gradients** allow you to create smooth, code-generated transitions between two or more colors. Although they define colors, they are technically treated as **background images** by the browser and are applied using the `background-image` property (or the `background` shorthand). |
| 10 | + |
| 11 | +Since gradients are generated by code, they are perfectly scalable and load much faster than traditional image files. |
| 12 | + |
| 13 | +<AdsComponent /> |
| 14 | +<br /> |
| 15 | + |
| 16 | +## Applying a Gradient |
| 17 | + |
| 18 | +Gradients are functions of the `background-image` property: |
| 19 | + |
| 20 | +```css title="styles.css" |
| 21 | +selector { |
| 22 | + background-image: linear-gradient(...); |
| 23 | + /* OR */ |
| 24 | + background-image: radial-gradient(...); |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +## 1. `linear-gradient()`: Straight Transitions |
| 29 | + |
| 30 | +A linear gradient transitions colors along a straight line. You must define a direction or angle, and at least two color stops. |
| 31 | + |
| 32 | +### Syntax and Direction |
| 33 | + |
| 34 | +The direction can be set using keywords (`to top`, `to right`, `to bottom left`) or an angle in degrees (`deg`). |
| 35 | + |
| 36 | +```css title="styles.css" |
| 37 | +/* Direction keyword: transitions from left to right */ |
| 38 | +.box-1 { |
| 39 | + background-image: linear-gradient(to right, #00C9FF, #92FE9D); |
| 40 | +} |
| 41 | + |
| 42 | +/* Angle: transitions diagonally from bottom-left to top-right */ |
| 43 | +.box-2 { |
| 44 | + background-image: linear-gradient(45deg, #FF6B6B, #FFE66D); |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +<AdsComponent /> |
| 49 | +<br /> |
| 50 | + |
| 51 | +## 2. `radial-gradient()`: Circular Transitions |
| 52 | + |
| 53 | +A radial gradient transitions colors outward from a central point, radiating in a circular or elliptical shape. |
| 54 | + |
| 55 | +### Syntax and Shape |
| 56 | + |
| 57 | +You can optionally specify the shape (`circle` or `ellipse`) and the position where the center of the gradient starts. |
| 58 | + |
| 59 | +```css title="styles.css" |
| 60 | +/* Default: transitions outward from the center (yellow) to the edge (red) */ |
| 61 | +.box-3 { |
| 62 | + background-image: radial-gradient(circle at center, yellow, red); |
| 63 | +} |
| 64 | + |
| 65 | +/* Position: starts from the top-left corner */ |
| 66 | +.box-4 { |
| 67 | + background-image: radial-gradient(at top left, #53a388, #3e5060); |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +## Color Stops and Transparency |
| 72 | + |
| 73 | +You can define **color stops** to control where the color transition begins and ends, using percentages or length units. |
| 74 | + |
| 75 | +### Example: Sharp Stripes |
| 76 | + |
| 77 | +Setting two colors to stop at the same point creates a hard line or a stripe. |
| 78 | + |
| 79 | +```css title="styles.css" |
| 80 | +/* Creates a hard, vertical stripe at 50% */ |
| 81 | +.stripe { |
| 82 | + background-image: linear-gradient(to right, navy 50%, white 50%); |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +### Using Transparency |
| 87 | + |
| 88 | +Using RGBA or HSLA color formats allows you to fade a color to complete transparency, revealing the element's `background-color` or whatever is behind the element. |
| 89 | + |
| 90 | +```css title="styles.css" |
| 91 | +/* Fades from solid black to completely transparent */ |
| 92 | +.fade-out { |
| 93 | + background-image: linear-gradient(to bottom, black, rgba(0, 0, 0, 0)); |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +<AdsComponent /> |
| 98 | +<br /> |
| 99 | + |
| 100 | +## Interactive Gradient Demo |
| 101 | + |
| 102 | +Experiment with different gradient types and settings in the live editor. |
| 103 | + |
| 104 | +<CodePenEmbed |
| 105 | + title="Interactive Gradient Demo" |
| 106 | + penId="RNajVBM" |
| 107 | +/> |
0 commit comments