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 Attachment


The background-attachment property determines whether a background image scrolls with the rest of the page or remains in a fixed position. This property is key for creating parallax scrolling effects and other interesting visual treatments for your backgrounds.


Example 1: The Default scroll Value

.container {
  background-image: url('image.jpg');
  /* The background image will scroll with the page (default behavior) */
  background-attachment: scroll;
  height: 500px;
  border: 1px solid #ccc;
}

Explanation

scroll is the default value for background-attachment. It causes the background image to move along with the element as the user scrolls the page.


Example 2: The fixed Value

.container {
  background-image: url('image.jpg');
  background-size: cover;
  /* The background image will remain fixed in the viewport */
  background-attachment: fixed;
  height: 500px;
  border: 1px solid #ccc;
}

Explanation

When background-attachment is set to fixed, the background image does not scroll with the page. It remains in a fixed position relative to the viewport, creating a parallax-like effect.


Example 3: The local Value

.container {
  background-image: url('image.jpg');
  /* The background will scroll with the element's content */
  background-attachment: local;
  height: 300px;
  overflow: auto;
  border: 1px solid #ccc;
}

Explanation

The local value is useful for elements that have their own scrollbars. The background image will scroll along with the content inside that specific element, rather than the entire page.


Example 4: fixed for Full-Page Backgrounds

body {
  background-image: url('background.jpg');
  background-size: cover;
  background-position: center;
  /* Creates a full-page background that stays in place */
  background-attachment: fixed;
}

Explanation

Applying background-attachment: fixed to the body element is a common technique for creating a full-page background that remains static as the user scrolls through the content.


Example 5: Multiple Backgrounds with Different Attachments

.container {
  background-image: url('foreground.png'), url('background.jpg');
  background-position: center, center;
  background-repeat: no-repeat, no-repeat;
  background-size: 150px, cover;
  /* The foreground scrolls, the background is fixed */
  background-attachment: scroll, fixed;
  height: 500px;
}

Explanation

You can apply different background-attachment values to multiple background images. In this example, a smaller foreground image scrolls with the page, while the larger background image remains fixed.


Example 6: Combining local with Padding

.container {
  background-image: url('pattern.png');
  background-repeat: repeat;
  /* The pattern will scroll with the text content */
  background-attachment: local;
  height: 200px;
  padding: 20px;
  overflow: auto;
  border: 1px solid #ccc;
}

Explanation

This demonstrates how local works with padded content. The background pattern will be visible within the padded area and will scroll as the content within the container is scrolled.


Example 7: Parallax Effect with fixed

.parallax-section {
  background-image: url('scenic.jpg');
  background-attachment: fixed;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  height: 100vh; /* Full viewport height */
}

Explanation

This code creates a classic parallax scrolling section. By setting the height to the full viewport height and using background-attachment: fixed, the background image appears to move at a different speed than the foreground content, creating a sense of depth.