How to Create CSS Gradient Shadows
Adarsh M
Posted On: September 24, 2024
191471 Views
12 Min Read
Modern websites use contrasting colors, typography, and shadows to create a user-friendly experience. A key trend is mimicking natural elements for a more organic look that uses shadows, depth, and movement.
CSS shadows are essential for adding depth to UI elements, and using gradient shadows takes this natural effect even further. Gradient shadows create smoother, more natural transitions of light and shadow, enhancing the overall design.
In this blog, we’ll explore how to create gradient shadows to elevate your web content and improve your UI’s visual appeal.
- What Is Gradient Shadow in CSS?
- Gradient Shadows Using linear-gradient()
- Gradient Shadows Using conical-gradient()
- Gradient Shadows Using filter()
- Gradient Shadows Using radial-gradient()
- Gradient Shadows Using clip-path()
- Gradient Shadows Using Animation
- Best Practices for Creating Gradient Shadows
- Frequently Asked Questions (FAQs)
What Is Gradient Shadow in CSS?
The gradient shadow in CSS creates a more natural look with varying intensities, whereas, in solid shadows, the shadow’s intensity is uniform throughout, which can sometimes look flat and less realistic.
Usually, we have solid shadows, but sometimes we need colorful gradient shadows.
Gradient shadows, like the one you see in the above example, can create a sense of depth and dimension by using different shades and colors in the gradient.
For more information on creating shadows, refer to our blog on CSS box shadows.
There are many ways to create gradient shadows. Let’s look into each of them in detail.
Gradient Shadows Using linear-gradient()
When you think about creating gradient shadows, the linear-gradient() property might be the first option that comes to mind, and it’s the easiest way to achieve them. All you have to do is create a background using the linear-gradient() property and then add some blur effects to the element.
Syntax:
1 |
background: linear-gradient(direction, color-stop1, color-stop2, ...); |
- direction: This value defines the direction of the gradient. It accepts values in degrees or using keywords like to right, to left, to bottom left, etc.
- color-stop1, color-stop2: These are the colors at specific points in the gradient. You can specify the position of each color stop as a percentage or length.
CSS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
.box::before { content: ""; position: absolute; inset: -5px; background: linear-gradient( 40deg, #fafa00, #f79d03, #ee6907, #e6390a, #de0d0d, #d61039, #cf1261, #c71585, #cf1261, #d61039, #de0d0d, #ee6907, #f79d03, #ffd700, #ffd700, #ffd700 ); |
See the Pen
Lambdatest info card with shadows by adarsh (@adarsh-gupta101)
on CodePen.
Here, in the example, the linear-gradient() property is used to create a gradient shadow effect. If we were to apply the linear-gradient() directly to the background of the element, it would change the entire background to a gradient color. However, the goal here is to create a gradient shadow while keeping the element’s background intact.
To achieve the background shadow, we have used the ::before pseudo selector. This pseudo-element allows us to insert content before the actual content of the element, in this case, a gradient shadow layer.
CSS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
.box::before { content: ""; position: absolute; inset: -2px; background: linear-gradient( 40deg, #fafa00, #f79d03, #ee6907, #e6390a, #de0d0d, #d61039, #cf1261, #c71585, #cf1261, #d61039, #de0d0d, #ee6907, #f79d03, #ffd700, #ffd700, #ffd700 ); filter: blur(4px); transform: translate3d(0px, 0px, -10px); border-radius: inherit; pointer-events: none; transition: transform 0.3s ease; } |
In the code, we also applied a blur effect so that the linear-gradient will have softer shadow edges. The ::before pseudo-element is positioned absolutely within the .box element so that it will align with the dimensions of the .box element.
Here, we have also added a small CSS inset property to the element so the .box element does not entirely cover the shadow. To make the shadow appear below the element, we use the translate3d property and set the z-value as negative.
Test your gradient shadows across 3000+ real browsers. Try LambdaTest Today!
Gradient Shadows Using conic-gradient()
Just like the linear-gradient() function in CSS, the conic-gradient() function is another gradient function that creates gradients around the center of an element and can also be used for gradient shadow effects.
Syntax:
1 |
conic-gradient([from angle] [at position], color-stop1, color-stop2, ...) |
- from angle: The from angle defines the starting angle of the gradient.
- at position: The at position defines the center position of the gradient.
- color-stop: The color-stop defines the colors and their positions in the gradient.
The conic-gradient() function is capable of creating a wide variety of conical gradients. For instance, take a look at the following example.
CSS:
1 2 3 4 5 6 7 8 9 10 11 |
background: conic-gradient( red 6deg, orange 6deg 18deg, yellow 18deg 45deg, green 45deg 110deg, blue 110deg 200deg, purple 200deg, white 200deg 240deg, rgb(114, 24, 39) 240deg 300deg, rgb(248, 33, 33) 300deg 360deg ); |
Output:
You can create accurate color steps using the conic-gradient() function, allowing for precise control over the gradient transitions.
Let’s now soften this gradient shadow by adding blur to it. We will use a similar approach as we did with the linear-gradient() function, using the ::before pseudo-element selector.
CSS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
button::before { content: ""; position: absolute; inset: 5px; background: conic-gradient( red 6deg, orange 6deg 18deg, yellow 18deg 45deg, green 45deg 110deg, blue 110deg 200deg, purple 200deg, white 200deg 240deg, rgb(114, 24, 39) 240deg 300deg, rgb(248, 33, 33) 300deg 360deg ); |
See the Pen
Gradient Shadow using Conical Gradient Property by adarsh (@adarsh-gupta101)
on CodePen.
Here, the conic-gradient() function is applied to the ::before pseudo-element of the button, creating a gradient shadow effect. Here, we have also used the translate3d property to stack the shadow behind the button.
In the example, we have used various color stops to create a gradient that transitions from a set of colors. Along with that, the blur property is used to soften the edges of the gradient.
Gradient Shadows Using filter()
CSS filter() is a property that can modify the appearance of an element by applying effects like blur, brightness, contrast, and more. The filter() property can be used to create gradient shadows and can be combined with other properties to enhance shadow effects.
To create gradient shadow effects, you can use the drop-shadow filter combined with the blur() property. Let’s see an example:
CSS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
.gradient-shadow-filter { width: 300px; height: 300px; background: rgb(238, 238, 238); padding: 20px; position: relative; border-radius: 10px; margin: 25px; } .gradient-shadow-filter::before { content: ""; position: absolute; inset: 25px; background: white; border-radius: 10px; filter: drop-shadow(0 0 20px rgb(218, 135, 11)) drop-shadow(0 0 10px rgb(243, 74, 220)) blur(5px); z-index: -1; } |
See the Pen
Gradient shadow with drop shadow by adarsh (@adarsh-gupta101)
on CodePen.
In the example, the element is given a drop-shadow() function with two different colors. Their combination creates a gradient effect. Along with the blur() property, it makes the shadow spread evenly and gives it smoothness around the edges.
Here, we have multiple drop-shadow filters, and each drop-shadow() function will define a separate shadow layer, which can give a sense of gradient shadow.
Gradient Shadows Using radial-gradient()
The CSS radial-gradient() function allows you to create smooth transitions between two or more specified colors. A radial gradient radiates from a central point outwards in a circular or elliptical shape.
Syntax:
1 |
background: radial-gradient(shape size at position, start color, ..., last color); |
- shape: It specifies the shape of the gradient (e.g., circle or ellipse).
- size: It specifies the size of the gradient (e.g., closest-side, farthest-corner).
- position: It specifies the position of the gradient (e.g., center, top left).
- start color: It specifies the start color is the color at the starting point of the gradient.
- last color: It specifies the last color is the color at the ending point of the gradient.
The idea behind using the radial-gradient() function is similar to how the linear-gradient() function is used for shadows. The pseudo-selector ::before is used and styled with a background of radial-gradient to create the shadow-like effect.
CSS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
.btn-spotlight::before { content: ""; position: absolute; top: -50%; left: -50%; width: 200%; height: 200%; background: radial-gradient( circle, rgba(255, 255, 255, 0.3) 0%, transparent 60% ); opacity: 0; transition: opacity 0.3s ease; } |
See the Pen
Untitled by adarsh (@adarsh-gupta101)
on CodePen.
In the above example, there are five different buttons, each with a different shadow effect created using the radial-gradient() function. The spotlight button uses the radial-gradient() function, and its width and height are double the element’s width to create a spotlight effect when hovered over.
The soft-shadow button uses the radial-gradient() function for a subtle shadow effect, and the ::before pseudo-element is used here to target the shadow.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
.btn-soft-shadow::before { content: ""; position: absolute; inset: 1px; background: radial-gradient( circle, rgba(255, 193, 7, 0.7) 0%, transparent 70% ); z-index: 1; opacity: 0; transition: opacity 0.3s ease; } |
The neon-button has a neon light effect with both text-shadow and box-shadow when hovered over. You can also add more custom styles to improve and customize the shadow effects.
Gradient Shadows Using clip-path()
The CSS clip-path() property allows you to define a clipping region for an element. A clipping region is essentially a shape that defines what parts of an element should be visible and what parts should be hidden.
By using a polygon-based clipping region, we can create complex shapes that allow us to create gradient-based shadows.
Let’s take a look at an example of how to use the clip-path() property to create a gradient shadow. In this example, we’ll use a polygon-based clipping region to create a rectangular shape that allows us to create a gradient shadow.
CSS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
.shadow::before { content: ""; position: absolute; inset: 0; transform: translate3d(0, 0, -1px); background-image: linear-gradient( 218deg, hsl(195deg 92% 51%) 13%, hsl(208deg 94% 51%) 45%, hsl(221deg 95% 51%) 56%, hsl(234deg 96% 50%) 63%, hsl(247deg 98% 50%) 68%, hsl(259deg 99% 50%) 72%, hsl(266deg 93% 54%) 75%, hsl(267deg 80% 62%) 78%, hsl(268deg 67% 70%) 80%, hsl(269deg 54% 78%) 83%, hsl(269deg 41% 86%) 87%, hsl(270deg 27% 94%) 100% ); filter: blur(4px); clip-path: polygon( -100vmax -100vmax, 100vmax -100vmax, 100vmax 100vmax, -100vmax 100vmax, -100vmax -100vmax, 0 0, 0 100%, 100% 100%, 100% 0, 0 0 ); } |
See the Pen
Untitled by adarsh (@adarsh-gupta101)
on CodePen.
Here in the code, the background-image() property is used to define the gradient along with the blur() property for smoothness. Following the same approach used in the earlier examples, the ::before pseudo-selector is used for styling the shadow.
If the background of the element is transparent, you will see the entire gradient on the screen, just like in the above image. To avoid this, we use the clip-path() property here so that we get the desired shadow.
If the element has a non-transparent background color, it’s unnecessary to use a clip-path() property, as the background of the element will naturally cover the gradient portion that isn’t needed. The clip-path() property can assure you that whether the background is transparent or not, it will accurately define the visible area of the element.
Now that we know almost all the basics of CSS gradient shadows, let’s add some animation to it.
Gradient Shadows Using Animation
So far, we have seen the best possible ways to create gradient shadows. To add more interactivity, you can add CSS animations to these shadows.
In the following example, there are two card elements that have an animated gradient shadow when you hover over the card element.
CSS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
.shadow:after { content: ""; position: absolute; top: -2px; left: -2px; background: linear-gradient( 45deg, #fb0094, #0000ff, #00ff00, #ffff00, #ff0000, #fb0094, #0000ff, #00ff00, #ffff00, #ff0000 ); filter: blur(1px); background-size: 400%; width: calc(100% + 4px); height: calc(100% + 4px); z-index: -1; opacity: 0; animation: animate 20s linear infinite; } .shadow:hover::after, .shadow:hover::before { opacity: 1; } .shadow:after { filter: blur(20px); } @keyframes animate { 0% { background-position: 0 0; } 50% { background-position: 300% 0; } 100% { background-position: 0 0; } } |
See the Pen
Untitled by adarsh (@adarsh-gupta101)
on CodePen.
Here, the ::before and ::after pseudo-elements are used to create a background shadow for the element. Along with that, the linear-gradient() function is used to create the gradient. The blur property makes the gradient smoother, giving it the appearance of a soft shadow.
For the animation part, the animation gradually changes the background position of the pseudo-elements background gradient from 0 0 to 300% 0 and then back to 0 0. This movement creates the illusion of a moving gradient, adding a dynamic effect to the border, which can catch the user’s attention.
Best Practices for Creating Gradient Shadows
Gradient shadows add depth and dimension to elements, but it is important to keep them subtle. Gradients should be used sparingly to avoid overly complicated web designs.
Here are some best practices to keep in mind while creating gradient shadows
- Blend Colors Smoothly: Choose colors that complement each other. Adding too many contrasting colors in your shadows can make them look distracting and may draw users’ attention away from the original content.
- Use Layering: While it is true that layered shadows can improve the appearance of shadows, they do come at a cost. For example, if you try to layer 20 shadows, the browser and device have to do 20 times more work than with a single layer.
- Provide Fallback for Older Browsers: Old browsers sometimes do not support all CSS properties. To ensure it doesn’t affect the user experience, provide fallback styles for these browsers.
CSS allows you to layer box shadows, through which you can define specific color stops in your gradient. This allows for more precise control over the color transition.
If your user is using an old device with slow rendering, it can affect the user experience of such users. Excessive use of gradients and filters can impact performance on lower-end devices.
You should test the performance in order to ensure that you can give the best experience for all your users.
For example, the clip-path() property is not fully supported by the major browsers, and they all have partial support. It’s important to check browser compatibility for CSS properties and use fallbacks or vendor prefixes as needed.
To ensure that your website performs as intended across all browsers, consider AI-powered testing platforms like LambdaTest that offer cross browser testing to check how your CSS properties render on older browser versions. You can easily verify the support for properties like clip-path() across different browser environments and ensure smooth fallback implementations.
In addition to browser compatibility testing, you can even use LT Browser to test sites on mobile across 50+ screen sizes. This ensures that your gradient shadows are responsive and adapt to different mobile devices.
Wrapping Up
In this detailed blog, we discussed various solutions for creating gradient shadows. While there isn’t a straightforward way to achieve the gradient shadow effect, we’ve discovered more than five methods to create stunning gradient shadows.
We’ve also looked at some real-world examples and best practices to help you get the most out of this design element. If you take the next step and dig deeper, you might discover even more interesting ways to create gradient shadows. I’ll leave that exploration to you.
Frequently Asked Questions (FAQs)
What is gradient shadow?
A gradient shadow is a visual effect where a shadow gradually fades from dark to light, creating a smooth transition in color or transparency, giving a more realistic and dynamic depth to the object.
What does gradient background mean?
A gradient background is a smooth transition between two or more colors or shades, often used to add visual interest, depth, or dimension to the background of a design element.
What is a conic gradient?
A conic gradient is a type of gradient that radiates from a central point in a circular pattern, transitioning colors around the center, like the slices of a pie chart.
Got Questions? Drop them on LambdaTest Community. Visit now