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

Background Image


The background-image property sets one or more images as the background of an element. These images are drawn on top of the background-color and will sit behind the element's content. This property is key for creating visually rich and textured designs.

Example 1: Setting a Single Background Image

/* style.css */
.image-one {
  /* Sets a single background image from a URL */
  background-image: url('https://via.placeholder.com/300/CCCCCC/FFFFFF');
  height: 200px; /* Define height to ensure the background is visible */
  border: 1px solid black;
}

Explanation

This code uses the url() function to specify the path to an image file. The image is then placed as the background for the element with the class .image-one.


Example 2: Using Multiple Background Images

/* style.css */
.image-two {
  /* Sets two background images; the first one is layered on top */
  background-image: url('https://via.placeholder.com/100/FF0000/FFFFFF'), url('https://via.placeholder.com/300/0000FF/FFFFFF');
  background-repeat: no-repeat; /* Prevents the images from tiling */
  height: 200px;
  border: 1px solid black;
}

Explanation

You can layer multiple background images by providing a comma-separated list of url() values. The first image in the list is the topmost layer.


Example 3: Applying a Gradient

/* style.css */
.image-three {
  /* A linear gradient is treated as a background image */
  background-image: linear-gradient(to right, steelblue, lightblue);
  height: 200px;
}

Explanation

CSS gradients, such as linear-gradient(), are generated by the browser and treated as images. This example creates a smooth color transition from steelblue to lightblue across the element.


Example 4: Combining an Image and a Gradient

/* style.css */
.image-four {
  /* Combines a semi-transparent gradient on top of an image */
  background-image: linear-gradient(rgba(0, 0, 255, 0.5), rgba(0, 0, 255, 0.5)), url('https://via.placeholder.com/400');
  height: 200px;
  color: white;
  text-align: center;
  line-height: 200px; /* Vertically centers the text */
}

Explanation

This demonstrates layering a semi-transparent blue gradient over a background image. This technique is often used to tint images or improve the readability of text placed over them.


Example 5: Using a Radial Gradient

/* style.css */
.image-five {
  /* Creates a circular gradient from the center outwards */
  background-image: radial-gradient(circle, #8A2BE2, #FF1493);
  height: 200px;
}

Explanation

The radial-gradient() function creates a gradient that radiates from a central point. Here, the gradient transitions from blue-violet at the center to a deep pink at the edges.


Example 6: Removing a Background Image

/* style.css */
.image-six {
  background-image: url('https://via.placeholder.com/300');
}
.image-six-override {
  /* The 'none' value removes any previously set background image */
  background-image: none;
  background-color: #f0f0f0; /* Add a fallback color */
  height: 200px;
  border: 1px solid black;
}

Explanation

The none keyword is used to specify that no background image should be displayed. This is often used to override an existing background-image style from another CSS rule.


Example 7: Using an SVG as a Background Image

/* style.css */
.image-seven {
  /* Sets a scalable vector graphic (SVG) as the background */
  background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /></svg>');
  height: 200px;
  border: 1px solid black;
}

Explanation

You can directly embed SVG code within the url() function. SVGs are vector-based, meaning they are resolution-independent and will appear sharp at any size.