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 Repeat


The background-repeat property defines how a background image is repeated if it is smaller than its container. By default, background images repeat both horizontally and vertically. This property gives you precise control over that tiling behavior.

Example 1: No Repetition

/* style.css */
.repeat-one {
  background-image: url('https://via.placeholder.com/100');
  /* Prevents the image from repeating in any direction */
  background-repeat: no-repeat;
  height: 250px;
  border: 1px solid grey;
}

Explanation

The no-repeat value ensures that the background image is displayed only once. It will appear at the top-left corner of the element by default.


Example 2: Repeat Horizontally

/* style.css */
.repeat-two {
  background-image: url('https://via.placeholder.com/100');
  /* Repeats the image horizontally across the element */
  background-repeat: repeat-x;
  height: 100px;
  border: 1px solid grey;
}

Explanation

Using repeat-x causes the background image to tile only along the horizontal axis (the x-axis). The vertical repetition is disabled.


Example 3: Repeat Vertically

/* style.css */
.repeat-three {
  background-image: url('https://via.placeholder.com/100');
  /* Repeats the image vertically down the element */
  background-repeat: repeat-y;
  height: 250px;
  width: 100px;
  border: 1px solid grey;
}

Explanation

The repeat-y value makes the background image tile only along the vertical axis (the y-axis), creating a column of the image.


Example 4: The Default repeat Value

/* style.css */
.repeat-four {
  background-image: url('https://via.placeholder.com/100');
  /* Explicitly sets the default behavior to repeat in both directions */
  background-repeat: repeat;
  height: 250px;
  border: 1px solid grey;
}

Explanation

The repeat value is the default behavior. It causes the image to tile both horizontally and vertically until it fills the entire background of the element.


Example 5: space Value for Even Spacing

/* style.css */
.repeat-five {
  background-image: url('https://via.placeholder.com/100');
  /* Repeats the image with even spacing between them, without clipping */
  background-repeat: space;
  height: 250px;
  border: 1px solid grey;
}

Explanation

The space value repeats the image as many times as it can without clipping it. The extra space is distributed evenly between the images.


Example 6: round Value for Stretching

/* style.css */
.repeat-six {
  background-image: url('https://via.placeholder.com/100');
  /* Stretches the image slightly to avoid gaps or clipping */
  background-repeat: round;
  height: 250px;
  border: 1px solid grey;
}

Explanation

The round value is similar to repeat, but it stretches or squashes the image instances slightly so that they fit perfectly within the container without any gaps.


Example 7: Two-Value Syntax for Different Axes

/* style.css */
.repeat-seven {
  background-image: url('https://via.placeholder.com/100');
  /* First value for horizontal, second for vertical (repeat-x, no-repeat-y) */
  background-repeat: repeat no-repeat;
  height: 250px;
  border: 1px solid grey;
}

Explanation

You can provide two values to background-repeat: the first for horizontal repetition and the second for vertical. This example repeats the image horizontally but not vertically.