
p {
color: red;
}
Here, the selector is p. It tells the browser, “Yo, find all <p> elements and make their text red.”
Now, there are many types of selectors, each with its own special mission:
🧱 1. Basic Selectors:
These are your everyday workhorses.
h1{
color: blue;
}
. before the class name.)-
HTML code:
<p class="highlight">This text stands out!</p>CSS code:
.highlight{
background: red;
}
Output:
This text stands out!
# before the ID name )-
HTML code:
<h1 class="title" id="main-title">Welcome to my page</h1>CSS code:
#main-title {
font-size: 2em;
}
Output:
,) between them.
It’s like saying:
“Hey browser, style all of these things the same way.”
HTML code:
<h1>Hello World!</h1> <h2>Welcome to my page</h2> <h3>Code with Fahim<h3>CSS code:
h1, h2, h3 {
color: royalblue;
font-family: "Poppins", sans-serif;
text-align: center;
}
Output:
*, and when you use it, it literally means “every element on the page.” The universal selector (*) targets all HTML elements at once — no exceptions.
It’s often used for resetting or applying global styles.
CSS code:
* {
color: white;
background-color: black;
}
💬 This makes every element on your webpage white text on a black background — from <h1> to <p>, even <a> and <div>.
4. Combinator Selectors:
Combinator selectors are the tactical squad of CSS. 🎯
They don’t just style elements; they define relationships between them.
It’s like saying:
“Don’t style every paragraph — only the one that lives right after this header” or
“Only the <p> that’s a child of a <div>.”
These selectors let you pinpoint elements based on their position in the HTML structure.
Let’s break ‘em down clearly and powerfully 👇
div p {
color: blue;
}
💬 Meaning: “Select all <p> elements inside <div>, no matter how deep they’re nested.”
Example:
<div>
<p>✅ Styled</p>
<section>
<p>✅ Also styled (still inside div)</p>
</section>
</div>
<p>❌ Not styled (outside div)</p>
div>p{
color:red;
}
💬 Meaning: “Select only <p> elements that are direct children of <div>.”
Example:
<div>
<p>✅ Styled</p>
<section>
<p>❌ Not styled (inside a section, not directly in div)</p>
</section>
</div>
It’s like saying: “I only care about my immediate kids, not my grandkids.” 😂
h2 + p {
color: green;
}
💬 Meaning: “Select the first <p> that comes right after an <h2>.”
Example:
<h2>Title</h2> <p>✅ Styled (right after h2)</p> <p>❌ Not styled (second p)</p>Perfect when you want to style a summary or note that directly follows a header.
h2 ~ p {
color: purple;
}
💬 Meaning: “Select all <p> elements that come after an <h2> (same parent).”
Example:
<h2>Title</h2> <p>✅ Styled</p> <p>✅ Styled too</p> <p>✅ You guessed it… styled!</p>This one’s like: “Everyone after the <h2> gets the same princess treatment.” 😎 Quick Comparison:
| Selector | Symbol | Target | Example |
|---|---|---|---|
| Descendant | (space) | All matching elements inside | div p |
| 👶Child | > | Only direct children | div>p |
| ➕Adjacent Sibling | + | Next element only | h2+p |
| General Sibling | ~ | All following siblings | h2~p |