CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

Linear Gradients


The CSS linear-gradient() function creates an image consisting of a progressive transition between two or more colors along a straight line. This function can be used to generate a wide variety of effects, from simple, smooth gradients to more complex patterns.


Example 1: Basic Two-Color Gradient

/* Creates a simple gradient from top to bottom */
.gradient-1 {
  background: linear-gradient(to bottom, #ff7e5f, #feb47b);
  height: 200px;
}

Explanation

This code creates a vertical gradient that starts with the color #ff7e5f at the top and smoothly transitions to #feb47b at the bottom. The to bottom keyword specifies the direction of the gradient line.


Example 2: Angled Gradient

/* Creates a gradient at a 45-degree angle */
.gradient-2 {
  background: linear-gradient(45deg, #6a11cb, #2575fc);
  height: 200px;
}

Explanation

By specifying an angle, 45deg, you can control the exact direction of the gradient. This example creates a diagonal gradient that flows from the bottom left to the top right.


Example 3: Gradient with Multiple Color Stops

/* A gradient with three distinct colors */
.gradient-3 {
  background: linear-gradient(to right, #f7971e, #ffd200, #f7971e);
  height: 200px;
}

Explanation

You can use more than two colors in a linear gradient. This example creates a horizontal gradient that starts with #f7971e, transitions to #ffd200 in the middle, and ends with #f7971e again.


Example 4: Using Color Stop Positions

/* Controlling the position of colors in the gradient */
.gradient-4 {
  background: linear-gradient(to right, #00c6ff 0%, #0072ff 100%);
  height: 200px;
}

Explanation

This code explicitly sets the starting and ending points for the colors. The first color, #00c6ff, starts at the 0% mark (the far left), and the second color, #0072ff, ends at the 100% mark (the far right).


Example 5: Creating Hard Lines with Gradients

/* Making sharp transitions instead of smooth ones */
.gradient-5 {
  background: linear-gradient(to right, #e44d26 50%, #f16529 50%);
  height: 200px;
}

Explanation

By placing two color stops at the same position (50%), you can create a hard line instead of a smooth gradient. This results in a solid split of two colors down the middle of the element.


Example 6: Gradient with Transparency

/* Using RGBA to create a transparent gradient overlay */
.gradient-6 {
  background-image: url('your-image.jpg'); /* A background image */
  background-size: cover;
  position: relative;
  height: 200px;
}

.gradient-6::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(to top, rgba(0,0,0,0.8), rgba(0,0,0,0));
}

Explanation

This example uses a pseudo-element (::before) to create a gradient overlay on top of a background image. The gradient transitions from a semi-transparent black (rgba(0,0,0,0.8)) at the bottom to fully transparent (rgba(0,0,0,0)) at the top, which can improve text readability over an image.


Example 7: Repeating a Linear Gradient

/* Creates a repeating stripe pattern */
.gradient-7 {
  background: repeating-linear-gradient(
    -45deg,
    #3498db,
    #3498db 10px,
    #2980b9 10px,
    #2980b9 20px
  );
  height: 200px;
}

Explanation

The repeating-linear-gradient() function creates a pattern of stripes that repeats itself. In this case, it generates diagonal stripes of two different shades of blue, each 10 pixels wide.