CSS Backgrounds & Borders
Backgrounds and borders give personality to sections, cards, buttons and banners. In this chapter, you will learn how to design modern webpage blocks with colors, images, gradients, radius and shadows.
background-color
Applies solid color behind an element.
background-image
Adds image or gradient background.
border
Adds visible boundary around an element.
border-radius
Creates smooth rounded corners.
After This Chapter, You Will Understand
This chapter helps you make sections, cards and banners visually attractive.
Background Color
Apply solid background colors to webpage elements.
Background Image
Use image backgrounds with size and position.
Borders
Create solid, dashed, dotted and custom borders.
Shadows
Add depth using box-shadow and soft card effects.
background-color and background
Background color is one of the most commonly used CSS properties.
1 Background Color
The background-color property applies a solid color behind the content of an element. It is used in sections, cards, buttons, badges and banners.
background-color: #ffffff;
}
2 Background Shorthand
The background shorthand property can be used for color, image or gradient.
background: #06285f;
}
.hero {
background: linear-gradient(135deg, #06285f, #0d6efd);
}
Solid Color
Used for headers, buttons and footers.
Light Color
Used for cards and note sections.
Highlight Color
Used for tips and important notes.
Using Images as Background
Background images are useful for hero sections, banners and decorative blocks.
3 Image Background
background-image: url("banner.jpg");
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
4 Important Properties
- background-image: sets the image path
- background-size: controls image size
- background-position: controls image placement
- background-repeat: controls image repetition
- background-attachment: controls scroll behavior
| Property | Use | Common Value |
|---|---|---|
| background-size | Controls image size | cover, contain, 100% |
| background-position | Controls image position | center, top, bottom, left |
| background-repeat | Controls repetition | no-repeat, repeat |
| background-attachment | Controls scroll behavior | scroll, fixed |
Linear and Radial Gradients
Gradients create a premium and modern visual feel.
Linear Gradient
Radial Gradient
Multi-Color
border and border-radius
Borders help separate elements and create beautiful cards, buttons and boxes.
5 Border Syntax
Border shorthand includes width, style and color.
border: 2px solid #0d6efd;
}
6 Border Radius
Border radius creates rounded corners.
border-radius: 20px;
}
solid
Clean and commonly used border style.
dashed
Useful for upload boxes and highlights.
dotted
Useful for light decorative separation.
Adding Depth with box-shadow
Shadows make cards, buttons and panels look layered and premium.
Soft Premium Card Shadow
7 box-shadow Syntax
box-shadow uses horizontal offset, vertical offset, blur, spread and color.
box-shadow: 0 18px 45px rgba(0,0,0,.18);
}
Practice Backgrounds and Borders
Edit background, border, radius and shadow values to see the design change.
Mistakes Students Should Avoid
Background and border styling should support readability and brand quality.
Wrong Habits
- Using very busy background images behind text
- Forgetting background-repeat: no-repeat
- Using too many border styles on one page
- Adding very dark or harsh shadows
- Using low contrast between text and background
Correct Habits
- Use readable background and text contrast
- Use background-size: cover for hero banners
- Use soft shadows for premium cards
- Keep border radius consistent
- Use gradients with balanced brand colors
Test Your Understanding
Select the correct answers and check your score.
1. Which property sets a solid background color?
2. Which value makes background image cover the full area?
3. Which function creates a smooth color transition?
4. Which property creates rounded corners?
5. Which property adds depth/shadow to a box?
Remember These Points
Revise these before moving to Chapter 18.
Background Color
background-color applies a solid color behind an element.
Background Image
background-image adds an image or gradient behind content.
background-size
cover is commonly used for full hero background images.
Border
Border includes width, style and color.
Border Radius
border-radius creates rounded corners.
Box Shadow
box-shadow adds depth and premium card effect.