Javatpoint Logo
Javatpoint Logo

Linear Gradient in CSS

What is Gradient?

The term "CSS gradient" describes a Cascading Style Sheets (CSS) method that enables seamless transitions between two or more colors. It enables you to add a gradient effect to different CSS properties, including text, borders, and backgrounds.

With CSS gradients, you can specify a gradual blending of colors in either a circular or elliptical shape (radial-gradient) or across a specific direction. The gradient is defined by defining at least two color stops or the hues that will be mixed together.

For Example:

An illustration of a linear gradient that changes from blue to green is shown below:

The 'to right' keyword is used in this example's use of the linear-gradient() function to specify the gradient's direction. "Blue" and "Green" are the gradient's beginning and ending colors.

Additionally, you can specify more color stops to produce more intricate gradients.

Illustration:

An illustration of a radial gradient with three color stops is as follows:

In this instance, a circular gradient is made using the radial-gradient() function. The color "red," "yellow," and "green" stand in for the color stops, while the keyword "circle" specifies the shape.

You can create various visual effects and transitions by adjusting CSS gradients' direction, shape, and color stops. They are a potent tool for creating aesthetically pleasing websites because contemporary web browsers widely support them.

What is Linear Gradient?

In CSS, a linear gradient produces a seamless change in color along a single axis. Here is a program example and the syntax for a linear gradient:

The following syntax:

  • [angle]: Indicates the gradient line's direction. Degrees (deg) or keywords like to top, bottom, left, right, etc. can be used to specify it.
  • Color-stops 1, 2, and so forth: Specify the colors and where they should be placed along the gradient line.

Example:

This example applies a linear gradient background to the element with the class gradient-example and sets its height to 200 pixels using the linear-gradient() function. The gradient shifts from red (#ff0000) on the left to green (#00ff00) on the right.

Output:

Linear Gradient in CSS

Example 2:

Here's an example of an HTML program that uses a top-to-bottom linear gradient as the background:

This example applies a linear gradient background to the element with the class gradient-example and sets its height to 200 pixels using the linear-gradient() function. The gradient transitions from red (#ff0000) at the top to green (#00ff00) at the bottom, as indicated by the bottom keyword.

Output:

Linear Gradient in CSS

Example

Here's an example of an HTML program that uses a right-to-left linear gradient as the background:

This example applies a linear gradient background to the element with the class gradient-example and sets its height to 200 pixels using the linear-gradient() function. The gradient starts red (#ff0000) on the right and transitions to green (#00ff00) on the left, according to the left keyword.

Output:

Linear Gradient in CSS

Advantages of Linear Gradient in CSS

Here are the following advantages of the Linear gradient in CSS, such as:

  • Smooth Color Transitions: Linear gradients enable a pleasing and seamless blending of colors by ensuring a smooth transition between them. This gives designs a sense of depth and dimension.
  • Versatility: Linear gradients are incredibly versatile and can be used in various design elements, such as backgrounds, shapes, and text. They can be used at any angle or horizontally, providing various design options.
  • Depth and Realism: Using linear gradients, you can mimic real-world lighting conditions and give your designs a sense of depth and realism. This is especially helpful when creating illustrations, icons, or user interfaces.
  • Customization: Linear gradients have a high degree of customizability. You can alter the gradient's colors, direction, angle, and color stops to create the desired effect. A wide range of creative options are made possible by this flexibility.

Disadvantages of Linear Gradient in CSS

Here are the following disadvantages of the Linear gradient in CSS:

  • Limited Complexity: The color transition in linear gradients happens in a straight line because of their linear progression. Due to this restriction, it is difficult to create color blends that are more intricate or non-linear, like radial gradients or irregular shapes.
  • Banding is the term for visible lines or steps between color stops. In some circumstances, it can occur with linear gradients, mainly when using a small color palette or low-quality output. The gradient's smoothness can be diminished, and the appearance of polish diminished by banding.
  • Impact on Performance: The performance impact should be considered when using linear gradients in computer graphics, especially in web design or real-time rendering. Complex gradients with multiple color stops need more processing power, which could impact the website's overall performance and load times.

Next TopicCSS Properties





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA