🎭 CSS Pseudo-Classes & Pseudo-Elements

Last Updated: November 2025


Pseudo-classes and pseudo-elements let you style elements based on state (like hover or focus) or specific parts of elements (like first letter or after content).

🗣 Hinglish Tip: Pseudo-classes = kab / kis state me element hai , Pseudo-elements = element ke andar ke part ko style karna


CSS Pseudo-Classes

Pseudo-classes start with a single colon : and describe the state or position of an element.

Syntax

selector:pseudo-class {
  property: value;
}

Common Pseudo-Classes

Pseudo-ClassDescriptionExample
:hoverWhen user hovers mouse over the elementbutton:hover
:focusWhen an element (like input) becomes focusedinput:focus
:activeWhen an element is being clickeda:active
:visitedApplies to links already visiteda:visited
:checkedSelected radio button or checkboxinput:checked
:disabledElements disabled by user or codebutton:disabled
:first-childFirst child of its parentp:first-child
:last-childLast child of its parentli:last-child
:nth-child(n)Selects the nth element inside a parentli:nth-child(2)
:not(selector)Selects elements that do NOT match the selectordiv:not(.box)

Hover & Active

button:hover {
  background-color: lightblue;
}
button:active {
  background-color: royalblue;
  color: white;
}

🗣 Hinglish Tip: :hover tab chalega jab mouse le jaoge, :active tab jab click karoge.


Focus & Disabled

input:focus {
  border: 2px solid green;
}
input:disabled {
  background: #eee;
}

nth-child()

li:nth-child(odd) {
  background: #f0f0f0;
}
li:nth-child(even) {
  background: #ddd;
}
li:nth-child(3) {
  color: red;
}

🗣 Hinglish Tip: Odd-even styling table rows me kaafi kaam aata hai.


CSS Pseudo-Elements

Pseudo-elements start with a double colon :: and target specific parts of an element’s content.

Syntax

selector::pseudo-element {
  property: value;
}

Common Pseudo-Elements

Pseudo-ElementDescriptionExample
::beforeInserts content before an elementh1::before
::afterInserts content after an elementh1::after
::first-letterStyles the first letter of textp::first-letter
::first-lineStyles the first line of a paragraphp::first-line
::selectionStyles text when selected/highlighted::selection
::placeholderStyles placeholder text inside inputsinput::placeholder

::before and ::after

h1::before {
  content: "🌟 ";
}
h1::after {
  content: " 🌟";
}

🗣 Hinglish Tip: ::before aur ::after ke saath content property dena zaroori hai!


::first-letter

p::first-letter {
  font-size: 30px;
  color: crimson;
  font-weight: bold;
}

::selection

::selection {
  background: black;
  color: yellow;
}

🗣 Hinglish Tip: Text select karoge toh styling dikhegi — like highlight effect.


::placeholder

input::placeholder {
  color: gray;
  font-style: italic;
}