Pseudo Selector

In CSS, pseudo-selectors (or pseudo-classes and pseudo-elements) are used to style specific parts of elements or elements in particular states. They help in applying styles dynamically without needing to modify the HTML structure.

Pseudo-Classes (:)

Pseudo-classes target elements based on their state, position, or user interaction.

Pseudo-ClassDescription
:hoverApplies styles when the user hovers over an element.
:focusStyles an element when it gains focus (e.g., an input field).
:nth-child(n)Selects the nth child of a parent element.
:first-childTargets the first child of a parent.
:last-childTargets the last child of a parent.
:checkedStyles checkboxes or radio buttons that are checked.
:disabledTargets disabled form elements.
:not(selector)Excludes elements that match a specific selector.

Example:

button:hover {
  background-color: blue;
  color: white;
}

Pseudo-Elements (::)

Pseudo-elements target specific parts of an element.

Pseudo-ElementDescription
::beforeInserts content before an element.
::afterInserts content after an element.
::first-letterStyles the first letter of a text block.
::first-lineStyles the first line of a paragraph.
::selectionStyles text when selected.

Example:

p::first-letter {
  font-size: 2rem;
  color: red;
}
p::after {
  content: " ✨";
}

Latest Current Affairs 2025 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2025 Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz