Introduction
CSS allows you to control the appearance of web pages using colors, units, and background properties. These are essential for designing visually appealing and responsive websites.
In this lesson, you will learn how to apply colors, use different units, and style backgrounds in CSS.
What Youβll Learn
- CSS color formats
- CSS units (px, %, em, rem)
- Background properties
- Best practices
These are core CSS skills used on every website.
π¨ Types of Color Values
Color Names
h1 {
color: red;
}
β Easy to use
β Limited color options
Common names:red, blue, green, black, white, gray
HEX Color Codes
p {
color: #ff0000;
}
- Starts with
# - 6 characters (numbers + letters)
Examples:
#000000β Black#ffffffβ White#00ff00β Green
β Most commonly used
RGB Colors
div {
color: rgb(255, 0, 0);
}
- Red, Green, Blue values
- Range:
0β255
Example:
rgb(0, 0, 0)β Blackrgb(255, 255, 255)β White
RGBA (With Transparency)
.box {
background-color: rgba(0, 0, 0, 0.5);
}
- Last value = opacity
- Range:
0(transparent) to1(solid)
πΉ HSL Colors (Missing but Important π₯)
π¨ HSL Color Format
HSL stands for Hue, Saturation, and Lightness.
h1 {
color: hsl(0, 100%, 50%);
}
Explanation:
- Hue β Color (0β360)
- Saturation β Intensity (0%β100%)
- Lightness β Brightness (0%β100%)
π HSL is useful for adjusting colors easily.
πΉ Opacity Property
You can control transparency using the opacity property.
div {
opacity: 0.5;
}
π Value ranges from:
- 0 β Fully transparent
- 1 β Fully visible
πΉ Absolute vs Relative Units (Important π₯)
π Absolute vs Relative Units
Absolute Units:
- px (fixed size)
Relative Units:
- % (relative to parent)
- em (relative to parent font size)
- rem (relative to root element)
π Relative units are better for responsive design.
πΉ Where Colors Are Used
- Text β
color - Background β
background-color - Border β
border-color
πΉ CSS Units
Units define the size of elements, text, spacing, and layout.
π Types of Units
1οΈβ£ Pixels (px)
p {
font-size: 16px;
}
π Fixed size unit
2οΈβ£ Percentage (%)
div {
width: 50%;
}
π Relative to parent element
3οΈβ£ em Unit
p {
font-size: 2em;
}
π Relative to parent font size
4οΈβ£ rem Unit
p {
font-size: 2rem;
}
π Relative to root (<html>) font size
πΉ px vs em vs rem
| Unit | Type | Based On |
|---|---|---|
| px | Fixed | Screen |
| % | Relative | Parent |
| em | Relative | Parent |
| rem | Relative | Root |
πΉ Viewport Units (Modern CSS π₯)
π± Viewport Units
Viewport units are based on screen size.
div {
width: 100vw;
height: 100vh;
}
Types:
- vw β viewport width
- vh β viewport height
π Useful for full-screen layouts.
πΉ When to Use Units
- Use px for borders and small elements
- Use % for layouts
- Use rem for font sizes (recommended π₯)
π Quick Tip
html {
font-size: 16px;
}
1rem = 16px1.5rem = 24px
πΉ CSS Backgrounds
Background properties are used to style the background of elements.
π¨ Background Color
body {
background-color: #f5f5f5;
}
πΌοΈ Background Image
div {
background-image: url("image.jpg");
}
π Background Repeat
div {
background-repeat: no-repeat;
}
Options:
repeatno-repeatrepeat-xrepeat-y
π Background Position
div {
background-position: center;
}
Examples:
topbottomleftrightcenter
π Background Size
div {
background-size: cover;
}
Options:
coverβ fills entire areacontainβ fits image inside
Background Shorthand
You can combine background properties:
div {
background: #000 url("bg.jpg") no-repeat center / cover;
}
β Clean & professional
πΉ Background Attachment
π Background Attachment
Controls scrolling behavior of background.
div {
background-attachment: fixed;
}
Values:
- scroll (default)
- fixed
- local
π fixed creates a parallax effect.
πΉ Multiple Backgrounds (Advanced π₯)
πΌοΈ Multiple Backgrounds
You can use more than one background.
div {
background: url("img1.jpg"), url("img2.jpg");
}
π Images are layered on top of each other.
πΉ Complete Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: #f0f0f0;
} h1 {
color: #333;
font-size: 2rem;
} .box {
width: 50%;
height: 200px;
background: url("image.jpg") no-repeat center/cover;
color: white;
padding: 20px;
}
</style>
</head>
<body><h1>CSS Example</h1>
<div class="box">Styled Box</div></body>
</html>
πΉ Important Notes
- Avoid large image files
- Use HEX or RGB for precise colors
- Prefer
remfor scalable design - Optimize images for backgrounds
πΉ Common Mistakes
- Using too many colors
- Mixing units incorrectly
- Not setting background size
- Using large images
πΉ Best Practices
- Keep color scheme consistent
- Use relative units for responsiveness
- Use background images carefully
- Write clean CSS
πΉ Practice Tasks
- Task 1: Apply different colors to text.
- Task 2: Use px, %, and rem units.
- Task 3: Add a background image.
- Task 4: Use background shorthand.
FAQs
What is the best color format?
HEX and RGB are most commonly used.
Which unit is best for fonts?
rem is recommended.
Which unit is best for fonts?
rem is recommended.
Conclusion
CSS colors, units, and backgrounds are essential tools for designing modern web pages. By using the right color formats, flexible units, and background properties, you can create visually appealing and responsive designs.
π In the next tutorial, you will learn about CSS Borders & Box Model.
