CHAPTER 3
CSS Selectors
CSS selectors are used to select the HTML elements that you want to style.
01. What is CSS Selectors?
An element selector targets HTML elements by their tag name.
A class selector targets elements using a class name.
An ID selector targets one element using an ID.
The universal selector targets all elements.
Grouping selectors allow multiple selectors to share the same CSS rule.
02. Syntax
/* Element */
p {
color: blue;
}
/* Class */
.box {
padding: 20px;
}
/* ID */
#title {
color: red;
}
/* Universal */
* {
box-sizing: border-box;
}
03. Example
CSS / HTML Example
AIFA Coding Lab
<!DOCTYPE html>
<html>
<head>
<style>
* {
box-sizing: border-box;
}
h1 {
color: #2563eb;
}
.card {
padding: 20px;
background: #eef6ff;
}
#special {
color: #dc2626;
}
</style>
</head>
<body>
<h1>CSS Selectors</h1>
<div class="card">
<p>
This paragraph uses a class.
</p>
<p id="special">
This paragraph uses an ID.
</p>
</div>
</body>
</html>
04. Important Points
✓
Use classes when the same style may be reused.
✓
IDs should normally identify a unique element.
✓
The universal selector is written using an asterisk.
✓
Selectors determine which elements receive a CSS rule.
05. Common Mistakes
⚠
Forgetting the dot before a class selector.
⚠
Forgetting the hash symbol before an ID selector.
⚠
Using the wrong class or ID name in HTML.
06. Real World Use
Selectors are used throughout websites to target buttons, cards, headings, menus and other interface elements.
Ready to Practice? 🚀
Open the AIFA CSS Editor and practice this chapter live.
Try It Yourself →
Practice Exercise
Practice Selectors
Create three paragraphs and style them using an element selector, class selector and ID selector.