|
1 | | -<ComingSoon /> |
| 1 | +--- |
| 2 | +title: CSS Gradients |
| 3 | +description: "Learn how to use CSS gradients (linear and radial) to create smooth transitions between two or more colors without relying on image files." |
| 4 | +keywords: [CSS gradients, linear-gradient, radial-gradient, color-stop, background-image, web design effects, CodeHarborHub] |
| 5 | +tags: [CSS gradients, linear-gradient, radial-gradient, color-stop, background-image, web design effects] |
| 6 | +sidebar_label: Gradients |
| 7 | +--- |
| 8 | + |
| 9 | +CSS **Gradients** allow you to create smooth transitions between two or more specified colors. Crucially, these gradients are treated as CSS **background images**, but they are generated entirely by the browser using code, not by downloading a file. This results in faster load times and perfectly scalable graphics. |
| 10 | + |
| 11 | +Gradients are defined using two main functions: `linear-gradient()` and `radial-gradient()`. |
| 12 | + |
| 13 | +<AdsComponent /> |
| 14 | +<br /> |
| 15 | + |
| 16 | +## 1. `linear-gradient()`: Straight-Line Transitions |
| 17 | + |
| 18 | +A linear gradient transitions colors along a straight line. |
| 19 | + |
| 20 | +### Basic Syntax |
| 21 | + |
| 22 | +The function requires a direction or angle, followed by at least two color stops. |
| 23 | + |
| 24 | +```css title="styles.css" |
| 25 | +selector { |
| 26 | + background-image: linear-gradient(direction/angle, color-stop-1, color-stop-2, ...); |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +### Defining Direction |
| 31 | + |
| 32 | +You can specify the direction using keywords or an angle: |
| 33 | + |
| 34 | + * **Keywords:** `to top`, `to bottom` (default), `to left`, `to right`. |
| 35 | + * **Diagonal Keywords:** `to top left`, `to bottom right`. |
| 36 | + * **Angle:** Specified in degrees (`deg`). $0^{\circ}$ is up, $90^{\circ}$ is right, $180^{\circ}$ is down. |
| 37 | + |
| 38 | +### Example |
| 39 | + |
| 40 | +<Tabs> |
| 41 | +<TabItem value="css" label="styles.css"> |
| 42 | + |
| 43 | +```css |
| 44 | +.box-gradient { |
| 45 | + background-image: linear-gradient(to bottom, #1976d2, #d32f2f); |
| 46 | + height: '100px', |
| 47 | + borderRadius: '5px' |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +</TabItem> |
| 52 | +<TabItem value="html" label="index.html"> |
| 53 | + |
| 54 | +```html |
| 55 | +<div class="box-gradient"></div> |
| 56 | +``` |
| 57 | + |
| 58 | +</TabItem> |
| 59 | +</Tabs> |
| 60 | + |
| 61 | +<BrowserWindow url="http://127.0.0.1:5500/index.html"> |
| 62 | +<div style={{ |
| 63 | +background: 'linear-gradient(to bottom, #1976d2, #d32f2f)', |
| 64 | +height: '100px', |
| 65 | +borderRadius: '5px' |
| 66 | +}}></div> |
| 67 | +</BrowserWindow> |
| 68 | + |
| 69 | +<AdsComponent /> |
| 70 | +<br /> |
| 71 | + |
| 72 | +## 2. `radial-gradient()`: Circular Transitions |
| 73 | + |
| 74 | +A radial gradient transitions colors outward from a central point. |
| 75 | + |
| 76 | +### Basic Syntax |
| 77 | + |
| 78 | +The function requires a shape (optional), a position (optional), and at least two color stops. |
| 79 | + |
| 80 | +```css title="styles.css" |
| 81 | +selector { |
| 82 | + background-image: radial-gradient(shape size at position, color-stop-1, color-stop-2, ...); |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | + * **Shape:** `circle` or `ellipse` (default). |
| 87 | + * **Position:** Uses keywords like `at center` (default), `at top left`, or coordinates (`at 20% 80%`). |
| 88 | + |
| 89 | +### Example |
| 90 | + |
| 91 | +<Tabs> |
| 92 | +<TabItem value="css" label="styles.css"> |
| 93 | + |
| 94 | +```css |
| 95 | +.box-gradient { |
| 96 | + /* Starts at the center, circle shape, transitions from yellow to green */ |
| 97 | + background-image: radial-gradient(circle at center, #ffeb3b, #4caf50); |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +</TabItem> |
| 102 | +<TabItem value="html" label="index.html"> |
| 103 | + |
| 104 | +```html |
| 105 | +<div class="box-gradient"></div> |
| 106 | +``` |
| 107 | + |
| 108 | +</TabItem> |
| 109 | +</Tabs> |
| 110 | + |
| 111 | +<BrowserWindow url="http://127.0.0.1:5500/index.html"> |
| 112 | +<div style={{ |
| 113 | +background: 'radial-gradient(circle at center, #ffeb3b, #4caf50)', |
| 114 | +height: '100px', |
| 115 | +borderRadius: '5px' |
| 116 | +}}></div> |
| 117 | +</BrowserWindow> |
| 118 | + |
| 119 | +## Color Stops and Transparency |
| 120 | + |
| 121 | +A **color stop** is the point where a gradient changes to a specific color. You can specify the transition point using length units (`px`, `em`) or percentages (`%`). |
| 122 | + |
| 123 | +### Example: Hard Stop |
| 124 | + |
| 125 | +To create a sharp, striped transition (not smooth), set two colors to stop at the same point. |
| 126 | + |
| 127 | +```css title="styles.css" |
| 128 | +/* Color stops at 50% for both red and blue, creating a sharp line */ |
| 129 | +background-image: linear-gradient(to right, red 50%, blue 50%); |
| 130 | +``` |
| 131 | + |
| 132 | +### Using Transparency (RGBA/HSLA) |
| 133 | + |
| 134 | +Gradients work perfectly with transparent color values, allowing the background underneath to show through. |
| 135 | + |
| 136 | +<Tabs> |
| 137 | +<TabItem value="css" label="styles.css"> |
| 138 | +```css |
| 139 | +/* Fades from solid blue to a transparent blue */ |
| 140 | +.box { |
| 141 | + background-image: linear-gradient(to right, #2196f3, rgba(33, 150, 243, 0)); |
| 142 | +} |
| 143 | +``` |
| 144 | +</TabItem> |
| 145 | +<TabItem value="html" label="index.html"> |
| 146 | +```html |
| 147 | +<div class="box"></div> |
| 148 | +``` |
| 149 | +</TabItem> |
| 150 | +</Tabs> |
| 151 | + |
| 152 | + |
| 153 | +<BrowserWindow url="http://127.0.0.1:5500/index.html"> |
| 154 | +<div style={{background: 'linear-gradient(to right, #2196f3, rgba(33, 150, 243, 0))', |
| 155 | +height: '100px', borderRadius: '5px', backgroundImage: 'linear-gradient(to right, #2196f3, rgba(33, 150, 243, 0))', backgroundColor: 'pink'}}></div> |
| 156 | +</BrowserWindow> |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | +<AdsComponent /> |
| 161 | +<br /> |
| 162 | + |
| 163 | +## Advanced Features and Tips |
| 164 | + |
| 165 | +:::tip Use Angle for Predictability |
| 166 | +When using `linear-gradient()`, using an **angle** (e.g., `45deg`) is often more reliable and predictable than using diagonal keywords (e.g., `to top right`). |
| 167 | +::: |
| 168 | + |
| 169 | +### Repeating Gradients |
| 170 | + |
| 171 | +CSS provides two specialized functions to create seamless patterns: |
| 172 | + |
| 173 | + * `repeating-linear-gradient()` |
| 174 | + * `repeating-radial-gradient()` |
| 175 | + |
| 176 | +These functions take the same arguments as their standard counterparts, but they repeat the color-stop pattern infinitely. |
| 177 | + |
| 178 | +```css title="styles.css" |
| 179 | +/* Creates a repeating stripe pattern */ |
| 180 | +background-image: repeating-linear-gradient( |
| 181 | + 45deg, |
| 182 | + #f06292, /* Pink */ |
| 183 | + #f06292 10px, |
| 184 | + #ffcdd2 10px, /* Lighter Pink */ |
| 185 | + #ffcdd2 20px |
| 186 | +); |
| 187 | +``` |
| 188 | + |
| 189 | +### Performance Note |
| 190 | + |
| 191 | +Using CSS gradients (`background-image`) is almost always better for performance than using actual image files for simple transitions. The browser renders the colors directly on the GPU, minimizing file requests and bandwidth usage. |
| 192 | + |
| 193 | +## Interactive Gradient Demo |
| 194 | + |
| 195 | +Experiment with different directions, angles, and color stops in the live editor. |
| 196 | + |
| 197 | +<CodePenEmbed |
| 198 | + title="Interactive Gradient Demo" |
| 199 | + penId="emZGrXw" |
| 200 | +/> |
0 commit comments