Background Properties

🌈 Background

Background-color Background-image Background-size Background-position Background-repeat Background-attachment Background-clip Background (Shorthand) Background-origin

Assignment

Assignment 1 Assignment 2
📍 background-color
Definition: Sets the background color of an element.
The color fills the entire element area including padding, but not margins.
Default Value: transparent
Values: Color names | hex codes | rgb() | rgba() | hsl() | transparent

✦ Using Hex Color

background-color: #ff6b6b;
.element { background-color: #ff6b6b; /* Red color using hex */ }

✦ Using RGB Color

background-color: rgb(100, 200, 100);
.element { background-color: rgb(100, 200, 100); /* Green using RGB */ }

✦ Using RGBA (with transparency)

background-color: rgba(0, 150, 200, 0.5);
.element { background-color: rgba(0, 150, 200, 0.5); /* 50% transparency */ }

✦ Using Color Names

background-color: skyblue;
.element { background-color: skyblue; /* Named color */ }
📍 background-image
Definition: Sets one or more images as the background of an element.
The image is placed on top of the background-color.
Default Value: none
Values: url('path') | linear-gradient() | radial-gradient() | none

✦ Single Image URL

background-image: url('image.jpg');
.element { background-image: url('path/to/image.jpg'); background-size: cover; background-repeat: no-repeat; }

✦ Linear Gradient

background-image: linear-gradient(to right, #ff6b6b, #ffd89b);
.element { background-image: linear-gradient(to right, #ff6b6b, #ffd89b); }

✦ Radial Gradient

background-image: radial-gradient(circle, #ff6b6b, #4ecdc4);
.element { background-image: radial-gradient(circle, #ff6b6b, #4ecdc4); }

✦ Multiple Images

Multiple images layered
.element { background-image: url('image.jpg'), linear-gradient(180deg, rgba(0,0,0,0.5), rgba(0,0,0,0.5)); }
📍 background-size
Definition: Specifies the size of the background image.
Controls how the image scales within the element.
Default Value: auto
Values: auto | cover | contain | width height | percentage

✦ auto (Default)

background-size: auto;
.element { background-image: url('image.jpg'); background-size: auto; /* Original image size */ background-repeat: no-repeat; }

✦ cover

background-size: cover;
.element { background-image: url('image.jpg'); background-size: cover; /* Covers entire element */ background-repeat: no-repeat; }

✦ contain

background-size: contain;
.element { background-image: url('image.jpg'); background-size: contain; /* Fits entire image */ background-repeat: no-repeat; }

✦ Custom Dimensions (width height)

background-size: 150px 100px;
.element { background-image: url('image.jpg'); background-size: 150px 100px; /* Width and height */ }

✦ Percentage

background-size: 50% 50%;
.element { background-image: url('image.jpg'); background-size: 50% 50%; /* 50% of container size */ }
📍 background-repeat
Definition: Specifies how the background image repeats/tiles.
Controls tiling behavior when image is smaller than container.
Default Value: repeat
Values: repeat | no-repeat | repeat-x | repeat-y | space | round

✦ repeat (Default)

background-repeat: repeat;
.element { background-image: url('image.jpg'); background-repeat: repeat; /* Repeats both horizontally and vertically */ }

✦ no-repeat

background-repeat: no-repeat;
.element { background-image: url('image.jpg'); background-repeat: no-repeat; /* Image appears once */ }

✦ repeat-x (Horizontal only)

background-repeat: repeat-x;
.element { background-image: url('image.jpg'); background-repeat: repeat-x; /* Repeats only horizontally */ }

✦ repeat-y (Vertical only)

background-repeat: repeat-y;
.element { background-image: url('image.jpg'); background-repeat: repeat-y; /* Repeats only vertically */ }

✦ space

background-repeat: space;
.element { background-image: url('image.jpg'); background-repeat: space; /* Repeats with equal spacing */ }

✦ round

background-repeat: round;
.element { background-image: url('image.jpg'); background-repeat: round; /* Scales images to fit evenly */ }
📍 background-position
Definition: Sets the starting position of the background image.
Determines where the image appears within the element.
Default Value: 0% 0% (top-left)
Values: Keywords (center, top, left, etc.) | Percentages | Pixel values

✦ center

background-position: center;
.element { background-image: url('image.jpg'); background-position: center; /* Center both horizontally and vertically */ background-repeat: no-repeat; }

✦ top left

background-position: top left;
.element { background-image: url('image.jpg'); background-position: top left; /* Top-left corner */ }

✦ bottom right

background-position: bottom right;
.element { background-image: url('image.jpg'); background-position: bottom right; /* Bottom-right corner */ }

✦ Pixel Values (x y)

background-position: 50px 30px;
.element { background-image: url('image.jpg'); background-position: 50px 30px; /* 50px from left, 30px from top */ }

✦ Percentage Values

background-position: 75% 50%;
.element { background-image: url('image.jpg'); background-position: 75% 50%; /* 75% from left, 50% from top */ }
📍 background-attachment
Definition: Determines if background image scrolls or stays fixed while scrolling.
Controls parallax effect and fixed backgrounds.
Default Value: scroll
Values: scroll | fixed | local

✦ scroll (Default)

background-attachment: scroll;
.element { background-image: url('image.jpg'); background-size: cover; background-attachment: scroll; /* Scrolls with content */ }

✦ fixed (Parallax Effect)

background-attachment: fixed;
.element { background-image: url('image.jpg'); background-size: cover; background-attachment: fixed; /* Stays fixed while scrolling (parallax) */ }

✦ local

background-attachment: local;
.element { background-image: url('image.jpg'); background-attachment: local; /* Scrolls inside element (not viewport) */ overflow-y: auto; }
📍 background-origin
Definition: Specifies the positioning area of the background image.
Determines from which box model the background is positioned (border, padding, or content).
Default Value: padding-box
Values: border-box | padding-box | content-box

✦ padding-box (Default)

background-origin: padding-box;
.element { background-image: url('image.jpg'); background-origin: padding-box; /* Starts from padding edge */ padding: 20px; border: 3px solid; }

✦ border-box

background-origin: border-box;
.element { background-image: url('image.jpg'); background-origin: border-box; /* Includes border area */ padding: 20px; border: 3px solid; }

✦ content-box

background-origin: content-box;
.element { background-image: url('image.jpg'); background-origin: content-box; /* Only inside content area */ padding: 20px; border: 3px solid; }
📍 background-clip
Definition: Specifies how far the background extends within an element.
Controls clipping area of the background (border, padding, or content).
Default Value: border-box
Values: border-box | padding-box | content-box | text

✦ border-box (Default)

background-clip: border-box;
.element { background: linear-gradient(90deg, #ff6b6b, #ffd89b); background-clip: border-box; /* Extends behind border */ border: 5px solid; }

✦ padding-box

background-clip: padding-box;
.element { background: linear-gradient(90deg, #00d2fc, #0055ff); background-clip: padding-box; /* Excludes border */ padding: 15px; border: 5px solid; }

✦ content-box

background-clip: content-box;
.element { background: linear-gradient(90deg, #00d2fc, #928dab); background-clip: content-box; /* Only inside content area */ padding: 15px; border: 5px solid; }

✦ text (Gradient Text Effect)

Gradient Text
.element { background: linear-gradient(90deg, #ff6b6b, #ffd89b); -webkit-background-clip: text; /* Vendor prefix needed */ background-clip: text; color: transparent; /* Hide text color to show background */ }
📍 background (Shorthand)
Definition: Combines all background properties into one declaration.
More concise and efficient way to set multiple background properties at once.
Syntax: background: [color] [image] [repeat] [attachment] [position/size]
Note: Order matters - not all properties need to be specified

✦ Basic Shorthand

background: #6a11cb url(image.jpg) no-repeat center/cover;
.element { background: #6a11cb url('image.jpg') no-repeat center/cover; /* Combines color, image, repeat, position, and size */ }

✦ With Attachment

background: url(image.jpg) fixed center/cover no-repeat;
.element { background: url('image.jpg') fixed center/cover no-repeat; /* Includes fixed attachment for parallax effect */ }

✦ Color Only

background: #ff6b6b;
.element { background: #ff6b6b; /* Just a color */ }

✦ Gradient Shorthand

background: linear-gradient(135deg, #ff6b6b, #ffd89b);
.element { background: linear-gradient(135deg, #ff6b6b, #ffd89b); /* Gradient as background */ }

✦ Multiple Backgrounds

Multiple backgrounds combined
.element { background: url('image.jpg'), linear-gradient(180deg, rgba(0,0,0,0.3), rgba(0,0,0,0.7)), #333; background-size: cover; background-position: center; }