DEV Community

Raizo-03
Raizo-03

Posted on

DAY 4 OF CSS

Today I dove deep into CSS background properties, and wow - there's so much more to backgrounds than just setting a color! Here's what I discovered and practiced.
The Basics: Background Color
Starting simple with background-color:
css#background-color {
background-color: aquamarine;
}
This creates a solid color background. Simple but effective for establishing visual hierarchy and adding personality to elements.
Adding Images with Background-Image
Things get interesting when we add images:
css#background-image {
background-image: url("../code.png");
background-repeat: no-repeat;
background-size: cover;
}
The background-image property opens up a world of possibilities. Combined with background-size: cover, it ensures the image covers the entire element while maintaining aspect ratio.
Controlling Repetition
Sometimes you want patterns or controlled repetition:
css#background-repeat {
background-image: url("../code.png");
background-repeat: repeat-x;
}
background-repeat options include:

repeat (default) - tiles in both directions
repeat-x - only horizontally
repeat-y - only vertically
no-repeat - shows once

Background Attachment
The background-attachment property controls scrolling behavior:
css#background-attachment {
background-image: url("../code.png");
background-attachment: scroll;
}
Options are:

scroll (default) - background scrolls with content
fixed - background stays fixed during scroll
local - background scrolls with element's content

Positioning Backgrounds
Control exactly where your background appears:
css#background-position {
background-image: url("../code.png");
background-position: center center;
}
You can use keywords (top, bottom, left, right, center) or precise values (50px 100px, 50% 25%).
Background Size Control
Make backgrounds fit perfectly:
css#background-size {
background-image: url("../code.png");
background-size: cover;
}
Key background-size values:

cover - scales to cover entire element
contain - scales to fit within element
auto - original size
Custom values like 50px 100px or 50% 75%

The Shorthand Property
CSS loves shortcuts! The background shorthand combines multiple properties:
css#background-shorthand {
background: #f0f0f0 url("../code.png") no-repeat;
}
This sets color, image, and repetition in one line. The order is flexible, but typically: color image repeat attachment position/size.
Key Takeaways

Layering: You can combine background colors with images for fallback effects
Performance: Consider image size and format for web performance
Accessibility: Ensure sufficient contrast between background and text
Responsive: Use background-size: cover or contain for responsive designs

Top comments (0)