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

Multiple Backgrounds


Multiple backgrounds in CSS allow you to layer two or more background images on top of each other within a single HTML element. This powerful feature lets you create complex and visually interesting designs without needing to edit the images themselves. You can control the position, size, and repetition of each background layer independently.


Example 1: Basic Layering

/* style.css */
.multiple-backgrounds {
  width: 100%;
  height: 400px;
  background-image: url('image1.png'), url('image2.png'); /* Layer images, the first is on top */
  background-repeat: no-repeat, no-repeat; /* Control repeat for each image */
  background-position: center, bottom right; /* Position each image independently */
}

Explanation: This code layers two background images. The first image (image1.png) is placed in the center and on top, while the second image (image2.png) is positioned at the bottom right corner underneath the first.


Example 2: Combining Images and Gradients

/* style.css */
.gradient-and-image {
  width: 100%;
  height: 400px;
  background-image: url('pattern.svg'), linear-gradient(to right, rgba(255,0,0,0.5), rgba(0,0,255,0.5)); /* An image on top of a gradient */
  background-position: top left; /* Position the image */
  background-repeat: repeat; /* Repeat the pattern image */
}

Explanation: Here, a semi-transparent repeating pattern (pattern.svg) is layered over a linear gradient. This technique is great for adding texture to colored backgrounds.


Example 3: Controlling Background Size

/* style.css */
.sized-backgrounds {
  width: 100%;
  height: 500px;
  background-image: url('foreground.png'), url('background-sky.jpg');
  background-size: 50%, cover; /* Set size for each background, first value for first image */
  background-repeat: no-repeat;
  background-position: bottom center;
}

Explanation: The background-size property is used to control each layer's dimensions. The foreground image is set to 50% of the container's width, while the sky image covers the entire element.


Example 4: Using the background Shorthand Property

/* style.css */
.shorthand-backgrounds {
  width: 100%;
  height: 450px;
  background:
    url('icon.svg') no-repeat top left / 50px 50px, /* First layer with size */
    url('texture.png') repeat, /* Second layer */
    #466368; /* Base color */
}

Explanation: The shorthand background property allows you to define all properties for multiple layers in a single declaration, separated by commas. A solid background color can be set as the final layer.


Example 5: Creating a Frame Effect

/* style.css */
.frame-effect {
  padding: 40px;
  background-image: url('corner-tl.png'), url('corner-tr.png'), url('corner-bl.png'), url('corner-br.png');
  background-repeat: no-repeat;
  background-position: top left, top right, bottom left, bottom right;
  background-color: #f0f0f0;
}

Explanation: This example uses four different images, one for each corner, to create a decorative frame around the element's content. The padding ensures the content doesn't overlap the frame images.


Example 6: Animating a Background Layer

/* style.css */
@keyframes move-clouds {
  from { background-position: 0 0, center; }
  to { background-position: 500px 0, center; }
}

.animated-background {
  height: 400px;
  background-image: url('clouds.png'), url('sky.jpg');
  background-repeat: repeat-x, no-repeat;
  background-size: auto, cover;
  animation: move-clouds 20s linear infinite;
}

Explanation: You can animate the background-position of one layer while keeping another static. In this case, the clouds.png layer moves horizontally, creating a parallax effect against the static sky.jpg.


Example 7: Full Page Hero Section

/* style.css */
.hero-section {
  height: 100vh;
  background:
    linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), /* Dark overlay for text readability */
    url('hero-image.jpg') no-repeat center center / cover; /* High-quality hero image */
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
}

Explanation: This technique is common for hero sections. A semi-transparent dark gradient is layered over a full-screen background image (cover size) to ensure that any text on top is legible.