- CSS Tutorial
- CSS Introduction
- CSS Syntax
- CSS Comments
- CSS Selectors
- CSS Fonts
- CSS Colors
- CSS Backgrounds
- CSS Box Model
- CSS Borders
- CSS Margins
- CSS Padding
- CSS Text
- CSS Images
- CSS Links
- CSS Lists
- CSS Tables
- CSS Outline
- CSS Icons
- CSS Display
- CSS max-witdh
- CSS Position
- CSS z-index
- CSS Overflow
- CSS Float
- CSS Align
- CSS Opacity
- CSS Navigation Bar
- CSS Dropdowns
- CSS Forms
- CSS Units
- CSS !important
- CSS Specificity
- CSS Combinators
- CSS inline-block
- CSS Hover
- CSS Cursors
- CSS Selectors
- CSS Type Selector
- CSS Class Selector
- CSS ID Selector
- CSS Attribute Selector
- CSS Pseudo-class Selector
- CSS Pseudo-element Selector
- CSS Universal Selector
- CSS Advanced
- CSS Text Formatting
- CSS Gradients
- CSS Shadow
- CSS Rounded Corners
- CSS Text Effects
- CSS 2D Transform
- CSS 3D Transform
- CSS Border Images
- CSS Inherit
- CSS Transitions
- CSS Animations
- CSS Box Sizing
- CSS Tooltip
- CSS Masking
- CSS Pagination
- CSS Styling Images
- CSS object-fit
- CSS object-position
- CSS Buttons
- CSS Multiple Columns
- CSS Variables
- CSS Flexbox
- CSS Grid
- CSS Media Queries
CSS Gradients
Gradients in CSS are a powerful tool for creating smooth transitions between two or more specified colors. Rather than using heavy image files that slow down your page load times, gradients are rendered directly by the browser. This means they are infinitely scalable, crisp on high-resolution displays, and incredibly performant.
In the CSS object model, gradients are treated as <image> data types. This is why you apply them using the background-image or background properties, rather than the background-color property. CSS defines several distinct types of gradients to give you full control over the visual flow of your UI.
Linear Gradients: A linear gradient is created using the linear-gradient() function and transitions colors along a straight line. By default, it flows from top to bottom, but you can easily customize the direction using keywords (like to right or to bottom left) or specific angles (like 45deg or 180deg). You define "color stops" to tell the browser exactly where one color should begin blending into the next.
135deg) instead of keywords for more precise control over the diagonal flow of your design.
CSS
/* A simple vertical transition */
.simple-gradient {
background-image: linear-gradient(red, yellow);
}
/* A modern, professional diagonal gradient */
.hero-gradient {
background-image: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
background-image. If you try to use background-color: linear-gradient(...), the browser will ignore the rule entirely and your gradient won't show up.
Radial Gradients: Created with the radial-gradient() function, radial gradients start at a single point and radiate outward in a circular or elliptical shape. This is perfect for creating "glow" effects or simulating depth on a page. You can define the shape (circle or ellipse), the starting position (e.g., at center or at top left), and the size of the transition.
CSS
/* A centered circular gradient that creates a spotlight effect */
#grad {
background-image: radial-gradient(circle, #ffffff, #2c3e50);
}
/* A multi-color radial gradient with specific stops */
.depth-effect {
background-image: radial-gradient(circle at 30% 30%, red, yellow, green);
}
Conic Gradients: These gradients are rotated around a center point, much like how a cone’s surface would unwrap into a circle or how the hands of a clock move. The conic-gradient() function is used to create this effect. Unlike radial gradients where colors emerge from the center, conic gradients transition colors *around* the center point. This makes them the go-to choice for creating pie charts, color wheels, or shiny metallic effects.
CSS
/* A basic conic gradient perfect for a color wheel */
#grad {
background-image: conic-gradient(red, yellow, green, blue, red);
}
/* Creating a simple pie chart using hard color stops */
.pie-chart {
background-image: conic-gradient(#f06 0% 25%, #444 25% 100%);
border-radius: 50%;
}
Repeating Gradients: Sometimes you need a pattern, like stripes or checkers, rather than a single transition. Both linear and radial gradients can be repeated using the repeating-linear-gradient() and repeating-radial-gradient() functions. These functions will take the color stops you provide and tile them infinitely to fill the element.
CSS
/* Creating a striped "caution" bar effect */
.stripes {
background-image: repeating-linear-gradient(
45deg,
#f3c300,
#f3c300 10px,
#000000 10px,
#000000 20px
);
}
/* A repeating ripple effect */
.ripples {
background-image: repeating-radial-gradient(
circle,
#3498db,
#3498db 5px,
#2980b9 5px,
#2980b9 10px
);
}