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 Shorthand


The CSS background shorthand property is a powerful and efficient way to set all the background properties for an element in a single declaration. This concise syntax enhances code readability and reduces file size, leading to faster website loading times. By combining multiple background-related values, you can control the color, image, position, size, repeat, and attachment of an element's background with just one line of code.


Example 1: Setting a Solid Background Color

/* Sets the background color of the body to a light gray */
body {
  background: #f0f0f0;
}

Explanation

This code sets a simple, solid background color for the <body> element. The hexadecimal color code #f0f0f0 represents a light shade of gray.


Example 2: Adding a Background Image

/* Places a background image that covers the entire element */
.hero-section {
  background: url('hero-image.jpg') no-repeat center center/cover;
}

Explanation

This example sets a background image for an element with the class .hero-section. The url() function specifies the image source, no-repeat prevents it from tiling, center center positions it, and cover scales it to fill the element.


Example 3: Tiling a Background Image

/* Creates a repeating pattern background */
.pattern-bg {
  background: url('pattern.png') repeat;
}

Explanation

Here, a small image named pattern.png is set as the background. The repeat value causes the image to tile horizontally and vertically, creating a seamless pattern.


Example 4: Fixed Background Image

/* A fixed background image that stays in place during scroll */
.parallax-effect {
  background: url('background.jpg') no-repeat center center fixed;
  min-height: 500px;
}

Explanation

This code creates a parallax-like effect by setting the background-attachment to fixed. The background image remains stationary in the viewport as the user scrolls down the page.


Example 5: Gradient Background

/* Applies a linear gradient from top to bottom */
.gradient-box {
  background: linear-gradient(to bottom, #4a90e2, #0052a3);
}

Explanation

This example demonstrates how to apply a linear gradient as a background. The linear-gradient() function creates a smooth color transition from the starting color #4a90e2 to the ending color #0052a3.


Example 6: Multiple Background Images

/* Layers two background images */
.multi-bg {
  background: url('foreground.png') no-repeat top left, url('background-sky.jpg') no-repeat bottom right;
  height: 600px;
}

Explanation

The background shorthand allows for layering multiple background images, each with its own properties. In this case, foreground.png is placed on top of background-sky.jpg.


Example 7: Background Color and Image Combined

/* A semi-transparent color over a background image */
.overlay {
  background: url('bg.jpg') no-repeat center/cover, rgba(0, 0, 0, 0.5);
  background-blend-mode: overlay;
}

Explanation

This code layers a semi-transparent black color (rgba(0, 0, 0, 0.5)) on top of a background image. The background-blend-mode property is used to blend the color and image together.