Draw a custom shape with radial-gradient in CSS
Gradients
In CSS, we can set multiple backgrounds to a DOM element by using CSS Gradients. There are 3 types of gradients:
- Linear Gradients (goes down/up/left/right/diagonally)
- Radial Gradients (defined by their center)
- Conic Gradients (rotated around a center point)
Example 1: Linear Gradients Demo
Example 2: Radial Gradients Demo
Example 3: Conic Gradients Demo
Example 4: Multiple layers of background Demo
Radial Gradients
Syntax:
background-image: radial-gradient(shape size at position, start-color, ..., last-color);
- shape:
circle
/ellipse
(default isellipse
) - size:
closest-side
/farthest-side
/closest-corner
/farthest-corner
(default isfarthest-corner
) - position: default is
center
- color: is a color value (red, #fff, rgba(0,0,0,0.5), etc.) followed by one or two top positions.
Example 5: Ellipse shape Demo
Example 6: Custom position Demo
Example 7: Color Demo
Draw a custom shape
Issue
In our situation, we have a scrollable content with red
color, there is a white sticky header with border radius. If scrolling down, we can see the background of the content is displayed at the corders of the header:
Solution
We can draw 2 custom shapes on top the the header to hide the red color at the corners.