CSS Selectors & Colors
CSS selectors decide which HTML elements will be styled. CSS colors decide the look, mood and branding of a webpage. In this chapter, you will learn both with practical examples.
Element Selector
Styles all elements of a tag such as h1, p or button.
Class Selector
Styles multiple elements using reusable class names.
ID Selector
Styles one unique element using an ID.
Colors
Use names, HEX, RGB, RGBA, HSL and gradients.
After This Chapter, You Will Understand
This chapter improves your control over styling and visual identity.
Selectors
Select exact HTML elements for styling.
Class & ID
Understand when to use class and when to use ID.
Colors
Use color names, HEX, RGB, RGBA and HSL.
Gradients
Create attractive gradient backgrounds.
Basic CSS Selectors
Selectors tell CSS which HTML element or group of elements should receive styling.
1 Element Selector
It selects all HTML elements with the same tag name.
color: gray;
}
2 Class Selector
It selects elements having a specific class. It starts with a dot.
background: yellow;
}
3 ID Selector
It selects one unique element. It starts with a hash sign.
color: navy;
}
| Selector Type | Symbol | Example | Use |
|---|---|---|---|
| Element Selector | No symbol | h1 | Selects all h1 tags |
| Class Selector | . | .card | Selects all elements with class="card" |
| ID Selector | # | #header | Selects element with id="header" |
Grouping, Descendant and Universal Selectors
These selectors help write shorter and cleaner CSS.
Grouping Selector
Applies the same style to multiple selectors.
color: #06285f;
}
Descendant Selector
Selects an element inside another element.
color: #334155;
}
Universal Selector
Selects all elements on the page.
box-sizing: border-box;
}
Ways to Write Colors in CSS
CSS gives many ways to define colors for text, backgrounds, borders and buttons.
Color Name
Example: red, blue, green, black
HEX Color
Commonly used in professional web design.
RGB Color
Uses red, green and blue values.
RGBA Color
Same as RGB but with transparency.
4 HSL Color
HSL means Hue, Saturation and Lightness. It is useful when you want to adjust shades systematically.
color: hsl(220, 90%, 45%);
}
5 Common Color Properties
- color: changes text color
- background-color: changes background color
- border-color: changes border color
- outline-color: changes outline color
- box-shadow: can use color values for shadow
Gradient Backgrounds
Gradients create smooth transitions between two or more colors.
6 Linear Gradient
Linear gradients are commonly used in hero sections, cards, buttons and modern banners.
background: linear-gradient(135deg, #06285f, #0d6efd);
}
Practice Selectors and Colors
Edit the CSS and click Run Code to see the design change.
Mistakes Students Should Avoid
Selectors and colors need proper symbols, spellings and consistency.
Wrong Habits
- Forgetting dot before class selector
- Forgetting hash before ID selector
- Using same ID repeatedly on many elements
- Using too many random colors
- Choosing poor contrast between text and background
Correct Habits
- Use dot for class selector
- Use hash for ID selector
- Use class for reusable design
- Use limited brand colors
- Maintain readable contrast
Test Your Understanding
Select the correct answers and check your score.
1. Which symbol is used for class selector?
2. Which symbol is used for ID selector?
3. Which selector selects all paragraph tags?
4. Which color format supports transparency?
5. Which CSS function creates smooth color transition?
Remember These Points
Revise these before moving to Chapter 15.
Element Selector
Selects all elements with a specific tag name.
Class Selector
Starts with dot and is used for reusable design.
ID Selector
Starts with hash and is used for unique elements.
HEX Color
A professional color format such as #0d6efd.
RGBA Color
Allows color transparency using alpha value.
Gradient
Creates smooth color transition backgrounds.