Introduction
Backgrounds and borders are among the most commonly used visual styling tools in CSS. Backgrounds let you fill an element with a color, image, or gradient, while borders draw a visible outline around an element's box. Together they give web pages depth, structure, and personality, from simple colored panels to card-based layouts with rounded corners and soft shadows.
Cricket analogy: A stadium's colorful boundary rope and painted advertising boards give the ground personality, just as CSS backgrounds and borders give a plain div visual identity and structure.
Syntax
.card {
background-color: #f5f5f5;
background-image: url("pattern.png");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
border: 2px solid #cccccc;
border-radius: 12px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15);
}Explanation
The background shorthand and its longhand properties (background-color, background-image, background-repeat, background-position, background-size) control how a fill or image appears within an element. The border shorthand sets width, style, and color in one declaration, while border-radius rounds the corners by defining the radius of an ellipse at each corner. box-shadow adds a drop shadow using horizontal offset, vertical offset, blur radius, optional spread, and a color, and it does not affect layout flow.
Cricket analogy: box-shadow is like a fielder's shadow cast on the pitch; it changes how the scene looks but never actually moves the fielder's real position on the ground.
Example
.button {
background: linear-gradient(135deg, #6a11cb, #2575fc);
border: none;
border-radius: 8px;
padding: 10px 24px;
color: white;
box-shadow: 0 2px 6px rgba(37, 117, 252, 0.4);
}
.button:hover {
box-shadow: 0 4px 12px rgba(37, 117, 252, 0.6);
}Output
The button renders with a diagonal purple-to-blue gradient background, no visible border, gently rounded corners, and a soft blue-tinted shadow beneath it. On hover, the shadow grows larger and more intense, giving the button a subtle lifted appearance without any change to its size or position.
Cricket analogy: The button's hover shadow growing larger without moving is like a batsman's presence looming bigger on TV replay zoom without actually stepping out of the crease.
Key Takeaways
- background-color, background-image, and gradients can all be combined using the background shorthand.
- border-radius accepts one to four values to round individual corners independently.
- box-shadow does not take up layout space; it is purely visual and can be layered with commas for multiple shadows.
- Always provide a fallback background-color when using background-image in case the image fails to load.
Practice what you learned
1. Which CSS property adds a drop shadow to an element without affecting its layout size?
2. What does 'background-size: cover' do?
3. Which property rounds the corners of an element's border box?
4. What is the correct shorthand order for the border property?
5. Which function can be used as a value for background-image to create a smooth color transition?
Was this page helpful?
You May Also Like
The CSS Box Model
Understand how content, padding, border, and margin combine to determine the size and spacing of every element.
Colors and Units in CSS
Explore CSS color formats and the absolute and relative units used to size and space elements.
CSS Transitions and Animations
Understand how to animate CSS property changes smoothly using transitions and create complex motion with keyframe animations.
Related Reading
Related Study Notes in Web Development
Browse all study notesWebSockets Study Notes
Web Development · 30 topics
Web DevelopmentWebAssembly Study Notes
WebAssembly · 30 topics
Web DevelopmentgRPC Study Notes
Protocol Buffers · 30 topics
Web DevelopmentSpring Boot Study Notes
Java · 30 topics
Web DevelopmentFlask Study Notes
Python · 30 topics
Web DevelopmentDjango Study Notes
Python · 30 topics