Colors, Units & Backgrounds

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) β†’ Black
  • rgb(255, 255, 255) β†’ White

RGBA (With Transparency)

.box {
    background-color: rgba(0, 0, 0, 0.5);
}
  • Last value = opacity
  • Range: 0 (transparent) to 1 (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

UnitTypeBased On
pxFixedScreen
%RelativeParent
emRelativeParent
remRelativeRoot

πŸ”Ή 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 = 16px
  • 1.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:

  • repeat
  • no-repeat
  • repeat-x
  • repeat-y

πŸ“ Background Position

div {
    background-position: center;
}

Examples:

  • top
  • bottom
  • left
  • right
  • center

πŸ“ Background Size

div {
    background-size: cover;
}

Options:

  • cover β†’ fills entire area
  • contain β†’ 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 rem for 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.

Related Tutorials

  • jQuery AJAX Tutorial for Beginners (Step-by-Step Introduction)

    Introduction Ever noticed how some websites update content instantly without refreshing the page? That’s powered by AJAX. AJAX (Asynchronous JavaScript and XML) allows websites to communicate with the server in the background and update content dynamically β€” without reloading the entire page. With jQuery, using AJAX becomes much easier because it provides simple methods like…

  • CSS Introduction

    Introduction CSS stands for Cascading Style Sheets. It is used to style and design web pages created with HTML. While HTML is used to structure content, CSS is used to control how that content looks on the screen. With CSS, you can change colors, fonts, layouts, spacing, and overall appearance of a website. In this…

  • CSS Syntax & Selectors

    Introduction CSS syntax defines how styles are written and applied to HTML elements. Selectors are used to target specific elements on a webpage and apply styles to them. Understanding CSS syntax and selectors is essential for writing effective and clean CSS code. In this lesson, you will learn the basic structure of CSS and different…

  • CSS Borders & Box Model

    Introduction CSS borders and the box model are essential concepts for designing layouts and controlling spacing in web development. Every HTML element is treated as a rectangular box, and understanding how this box works helps you create clean and structured designs. In this lesson, you will learn how to style borders and understand the CSS…

  • CSS Display & Position

    Introduction CSS display and position properties control how elements are shown and placed on a web page. These properties are essential for creating layouts, aligning elements, and building modern web designs. In this lesson, you will learn how different display types work and how positioning helps control element placement. What You’ll Learn πŸ”Ή CSS Display…

  • CSS Flexbox

    Introduction CSS Flexbox is a powerful layout system used to design flexible and responsive layouts. It makes it easy to align items, distribute space, and create modern UI designs without using complex positioning or floats. Flexbox works on a one-dimensional layout (row or column) and is widely used in real-world web development. What You’ll Learn…

Leave a Reply

Your email address will not be published. Required fields are marked *