Element or Tag Selector
style.css
h1 {
font-size: 30px;
}Class Selector
style.css
.classname {
font-size: 30px;
}id Selector
style.css
#idname {
font-size: 30px;
}Universal Selectors
style.css
* {
margin: 0;
padding: 0;
}Group Selectors
style.css
h1, h1, h3, h3, h5, h6, b {
color: red;
font-family: rockwell;
}π What Are Simple Selectors?
Simple selectors are the most basic type of CSS selectors. They allow you to target HTML elements using names, classes, IDs, attributes, and universal matching.
Note
β Type Selector
β Class Selector
β ID Selector
β Universal Selector
β Attribute Selector
β Pseudo-class Selector
π¦ The 6 Types of Simple Selectors
| Selector | Syntax | Targets |
|---|---|---|
| Type Selector | element | All elements of that tag |
| Class Selector | .class | Elements with that class |
| ID Selector | #id | Element with that unique ID |
| Universal Selector | * | Matches all elements |
| Attribute Selector | [attr] | Elements with attributes |
| Pseudo-class Selector | :hover | Elements in a certain state |
πΉ 1. Type Selector
Targets all elements of a specific HTML tag.
Type Selector
p {
color: blue;
}This styles all <p> elements.
πΉ 2. Class Selector
Targets any element with a specific class.
Class Selector
.card {
border: 1px solid #ddd;
padding: 1rem;
}Multiple elements can share the same class.
πΉ 3. ID Selector
Targets an element with a unique id. IDs must be used only once in a document.
ID Selector
#header {
background-color: black;
color: white;
}Note
πΉ 4. Universal Selector (*)
Selects all elements.
Universal Selector
* {
margin: 0;
padding: 0;
}Often used to reset default browser styles.
πΉ 5. Attribute Selector
Selects elements based on the presence or value of attributes.
Attribute Selector
input[type="email"] {
border: 2px solid blue;
}Useful for form styling and targeting elements semantically.
Common Attribute Selector Types
| Pattern | Meaning |
|---|---|
| [attr] | Has attribute |
| [attr="value"] | Exact match |
| [attr^="value"] | Starts with |
| [attr$="value"] | Ends with |
| [attr*="value"] | Contains substring |
πΉ 6. Pseudo-class Selector
Selects elements in a specific βstateβ such as hover, focus, or active.
Pseudo-class Selector
button:hover {
background-color: red;
}States do not change HTML β they reflect user interaction or element conditions.
π§ͺ Practical Examples Using Simple Selectors
π¨ Styling Form Elements
Form Styling
input[type="text"] {
padding: 0.5rem;
}
input:focus {
outline-color: blue;
}π¦ Card Component
Card with class
.card {
padding: 1rem;
border-radius: 8px;
background: #fafafa;
}
.card:hover {
transform: scale(1.02);
}ποΈ Navigation Highlight
Nav Example
nav a.active {
color: red;
font-weight: bold;
}π§ Specificity of Simple Selectors
| Selector | Specificity |
|---|---|
| Type | 0-0-1 |
| Class | 0-1-0 |
| ID | 1-0-0 |
| Attribute | 0-1-0 |
| Pseudo-class | 0-1-0 |
| Universal | 0-0-0 |
Note
β Universal selector has zero specificity
β οΈ Common Mistakes
- β Overusing ID selectors β makes CSS harder to override
- β Using universal selector (*) too often β performance impact
- β Styling without classes β reduces reusability
- β Misusing attribute selectors β can become slow on large DOMs
Note
π₯ Summary
CSS simple selectors are the building blocks of styling. By mastering them, you gain full control over how elements are targeted, allowing clean, efficient, maintainable CSS.
Learn more βMDN Docs π