100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
HTML & CSS

Backgrounds and Borders in CSS

Learn how to style element backgrounds and borders using CSS properties like background-image, border-radius, and box-shadow.

CSS Styling & EffectsBeginner9 min readJul 8, 2026
Analogies

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

css
.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

css
.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

Was this page helpful?

Topics covered

#HTMLCSS#HTMLCSSStudyNotes#WebDevelopment#BackgroundsAndBordersInCSS#Backgrounds#Borders#CSS#Syntax#StudyNotes#SkillVeris