Linear gradient with rainbow color

The CSS linear-gradient function can be used to create beautiful rainbow effects by combining all seven colors of the rainbow spectrum. A rainbow gradient provides a smooth transition from red through violet, creating an eye-catching visual effect.

Syntax

selector {
    background: linear-gradient(direction, red, orange, yellow, green, blue, indigo, violet);
}

Example: Rainbow Linear Gradient

The following example creates a horizontal rainbow gradient using all seven spectrum colors −

<!DOCTYPE html>
<html>
<head>
<style>
    #demo {
        height: 100px;
        width: 400px;
        background: linear-gradient(to right, red, orange, yellow, green, blue, indigo, violet);
        display: flex;
        align-items: center;
        justify-content: center;
        color: white;
        font-weight: bold;
        text-shadow: 1px 1px 2px rgba(0,0,0,0.5);
    }
</style>
</head>
<body>
    <h2>Linear Gradient - Rainbow Colors</h2>
    <div id="demo">Rainbow</div>
</body>
</html>
A colorful rectangular box displaying a smooth horizontal rainbow gradient from red on the left to violet on the right, with "Rainbow" text centered in white.

Example: Vertical Rainbow Gradient

You can also create a vertical rainbow gradient by changing the direction −

<!DOCTYPE html>
<html>
<head>
<style>
    .vertical-rainbow {
        height: 200px;
        width: 200px;
        background: linear-gradient(to bottom, red, orange, yellow, green, blue, indigo, violet);
        border-radius: 10px;
        margin: 20px;
    }
</style>
</head>
<body>
    <h2>Vertical Rainbow Gradient</h2>
    <div class="vertical-rainbow"></div>
</body>
</html>
A square box with rounded corners showing a vertical rainbow gradient flowing from red at the top to violet at the bottom.

Conclusion

Rainbow linear gradients use all seven spectrum colors to create vibrant, eye-catching backgrounds. You can adjust the direction and dimensions to fit your design needs.

Updated on: 2026-03-15T12:41:40+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements