CSS Program to Demonstrate What is a Selectors
Learn CSS step by step.
CSS Selectors
Jan 23, 2026
## Selectors — Do You Know How to Apply CSS to the “Right Place”?
**Let me say this clearly.
Most beginners don’t struggle in CSS because they don’t know properties like `color` or `font-size`.
They struggle because their CSS keeps styling the **wrong elements**.
They write:
```css
p { ... }
a { ... }
```
### And then the page becomes messy:
- TOC links start looking like normal links
- footer links get the same style
- warning text looks like regular paragraph
- “note” doesn’t feel important
- code explanation looks flat and boring
Then the student says:
> “Sir, CSS is not working.”
But CSS *is* working.
The real issue is:
✅ **The target is wrong.**
And the system used to target elements in CSS is called:
✅ **Selectors**
Selectors simply tell CSS:
> “Apply these styles exactly here — not everywhere.”
## Why selectors matter *more* on tutorial websites
On a tutorial website like MeetCode, a single page is always mixed.
One tutorial page usually contains:
- normal paragraphs
- note/tip lines
- warning/mistake lines
- reference/external links
- PDF notes download links
- code blocks
- next/previous navigation buttons
So if you style using only tag selectors, everything becomes “equal”.
And when everything looks equal, students can’t understand:
- ** what is important
- what is optional
- what is a dangerous mistake
- where they should click
** That’s why selectors are not optional.
Selectors are the **real CSS skill**.
## 1) Tag selectors — good for defaults, not for control
A tag selector means styling all elements of one tag.
```css
p{
color: #333;
}
```
Now every paragraph becomes dark grey.
That’s fine for basic design.
### Best use of tag selectors (in tutorial websites)
Tag selectors are perfect for “default rules”:
```css
body{
font-family: Arial, sans-serif;
line-height: 1.75;
}
a{
color: #0b57d0;
}
```
This is like setting classroom rules.
But tag selectors alone cannot build:
- note boxes
- warning blocks
- special links
- TOC sections that look different
So remember:
✅ Tag selectors = base rules
❌ Tag selectors ≠ precision control
---
## 2) Class selectors — where real projects begin
If I had to choose one selector for every beginner to master first:
**Class selector**
A class selector is group-based styling.
### Example (MeetCode tutorial content)
HTML:
```html
<p class="note">This point is important for your interview.</p>
<p class="warning">Do not reuse the same ID on a page.</p>
```
CSS:
```css
.note{
padding: 12px;
border-left: 4px solid #2e7d32;
background: #f3fff5;
}
.warning{
padding: 12px;
border-left: 4px solid #c62828;
background: #fff4f4;
}
```
Now students instantly understand:
- note is different
- warning is different
### Why class selectors are your “main weapon”
Because classes are:
- reusable
- scalable
- clean
- perfect for multiple pages
Big websites build UI using reusable classes:
- `.tutorialCard`
- `.btn`
- `.toc`
- `.codeBox`
- `.note`
- `.warning`
That’s how professional CSS works.
## 3) ID selectors — strong, but don’t overuse them
An ID selector targets **one unique element**.
HTML:
```html
<h1 id="pageTitle">CSS Selectors</h1>
```
CSS:
```css
#pageTitle{
font-size: 34px;
}
```
IDs are powerful — sometimes too powerful.
### When IDs are actually useful
- page titles
- unique sections
- TOC jump points
Example:
```html
<h2 id="examples">Examples</h2>
```
### Why you should not build styling with IDs
IDs do not repeat.
If your site has 100 tutorial pages, you cannot maintain design with IDs.
So follow this rule:
✅ IDs for structure / anchors
✅ Classes for styling system
## 4) Grouping selectors — save time, reduce repetition
If you want one rule for multiple tags:
```css
h2, h3{
margin-top: 22px;
}
```
This small trick improves maintainability.
## 5) Descendant selectors — “style only inside this section”
This is extremely common in tutorial layouts.
Example:
```css
article p{
font-size: 18px;
}
```
Meaning:
✅ Only paragraphs inside `<article>` will be affected.
This matters because you might have:
- article paragraphs
- footer paragraphs
- sidebar paragraphs
And you don’t want them all to look the same.
## 6) Child selectors (`>`) — direct elements only
Descendant selector styles everything inside.
Child selector means:
✅ “Only direct children, not nested ones.”
HTML:
```html
<article class="post">
<p>Direct paragraph</p>
<div class="box">
<p>Nested paragraph</p>
</div>
</article>
```
CSS:
```css
.post > p{
color: #111;
}
```
This affects only the first paragraph, not the nested one.
### Why it helps in tutorials
It keeps your styles clean and prevents nested blocks from breaking layout.
## 7) Sibling selectors — “the line after a heading”
Very useful in tutorial writing.
### Adjacent sibling (`+`)
Targets only the immediate next element:
```css
h2 + p{
font-weight: 600;
}
```
So the first paragraph after `<h2>` becomes stronger.
It looks natural in tutorials because it feels like a summary line.
### General sibling (`~`)
Targets all next siblings:
```css
h2 ~ p{
color: #444;
}
```
# 8) Attribute selectors — CSS filtering without extra classes
Attribute selectors are extremely practical.
### PDF download links
```css
a[href$=".pdf"]{
font-weight: 700;
}
```
Now all PDF links become bold automatically.
### External links opened in a new tab
```css
a[target="_blank"]{
text-decoration: underline;
}
```
Users instantly understand: it’s special/external.
### Secure links (starts with https)
```css
a[href^="https"]{
color: #0b57d0;
}
```
### Symbol meanings
- `^=` → starts with
- `$=` → ends with
- `*=` → contains
Attribute selectors are smart because:
✅ You can style without adding extra classes.
## 9) Pseudo classes — styles based on user actions
Pseudo classes handle interaction.
## Hover
```css
a:hover{
color: #0842a0;
}
```
### Focus (most people ignore this)
Important for keyboard users:
```css
input:focus{
outline: 2px solid #0b57d0;
}
```
If you ignore focus:
- forms feel incomplete
- UX looks cheap
- accessibility becomes poor
### Active (click moment)
```css
button:active{
transform: scale(0.98);
}
```
This makes UI feel alive.
## 10) `:nth-child()` — pattern selection for clean readability
This is perfect for long content.
Example: zebra effect in tutorial cards:
```css
.tutorialCard:nth-child(even){
background: #fafafa;
}
```
**Example: zebra effect in tables:
```css
tr:nth-child(odd){
background: #f7f7f7;
}
```
This improves readability a lot.
## 11) Pseudo elements (`::before`, `::after`) — add labels without editing HTML
Imagine you have 50 notes.
You don’t want to type “Note:” manually everywhere.
CSS can generate it:
```css
.note::before{
content: "Note: ";
font-weight: 700;
}
```
HTML stays clean:
```html
<p class="note">Attributes are written in the opening tag.</p>
```
### Output automatically becomes:
**Note:** Attributes are written…
This is professional practice.
# Mini Practice (MeetCode tutorial page example)
### HTML
```html
<article class="post">
<h1 id="pageTitle">CSS Selectors</h1>
<p class="intro">Selectors decide where your CSS should apply.</p>
<p class="note">Classes are best for reusable UI blocks.</p>
<p class="warning">Never repeat IDs on the same page.</p>
<p>
Download notes:
<a href="css-notes.pdf" target="_blank">PDF</a>
</p>
<ul class="toc">
<li><a href="#basic">Basic selectors</a></li>
<li><a href="#advanced">Advanced selectors</a></li>
</ul>
</article>
```
### CSS
```css
body{
font-family: Arial, sans-serif;
line-height: 1.75;
}
#pageTitle{
font-size: 34px;
}
.intro{
font-size: 18px;
color: #333;
}
.note{
padding: 12px;
border-left: 4px solid #2e7d32;
background: #f3fff5;
}
.warning{
padding: 12px;
border-left: 4px solid #c62828;
background: #fff4f4;
}
.post p{
margin: 12px 0;
}
a[href$=".pdf"]{
font-weight: 700;
}
a[target="_blank"]{
text-decoration: underline;
}
a:hover{
color: #0842a0;
}
.toc li:nth-child(even){
background: #fafafa;
}
```
### This one example includes:
- tag selector
- class selector
- id selector
- descendant selector
- attribute selectors
- pseudo classes
- nth-child
---
### Common mistakes students make with selectors
Let’s be honest — these mistakes are very common:
1. Styling everything using IDs
2. Writing extremely deep selectors (hard to maintain)
3. Using random class names like `.box1`, `.x2`
4. Ignoring `:focus` styles
<h2>Conclusion</h2>
<p>In this article, we explored the core concepts of CSS Selectors. Understanding these fundamentals is crucial for any developer looking to master this topic. We hope this guide helps you in your learning journey.</p>
## Frequently Asked Questions (FAQs)
**Q: What is CSS Selectors?**
A: CSS Selectors is a fundamental concept in this programming language/topic that allows developers to perform specific tasks efficiently.
**Q: Why is CSS Selectors important?**
A: It helps in organizing code, improving performance, and implementing complex logic in a structured way.
**Q: How to get started with CSS Selectors?**
A: You can start by practicing the basic syntax and examples provided in this tutorial.
**Q: Are there any prerequisites for CSS Selectors?**
A: Basic knowledge of programming logic and syntax is recommended.
**Q: Can CSS Selectors be used in real-world projects?**
A: Yes, it is widely used in enterprise-level applications and software development.
**Q: Where can I find more examples of CSS Selectors?**
A: You can check our blog section for more advanced tutorials and use cases.
**Q: Is CSS Selectors suitable for beginners?**
A: Yes, our guide is designed to be beginner-friendly with clear explanations.
**Q: How does CSS Selectors improve code quality?**
A: By providing a standardized way to handle logic, it makes code more readable and maintainable.
**Q: What are common mistakes when using CSS Selectors?**
A: Common mistakes include incorrect syntax usage and not following best practices, which we've covered here.
**Q: Does this tutorial cover advanced CSS Selectors?**
A: This article covers the essentials; stay tuned for our advanced series on this topic.