Pseudo-elements and Pseudo-classes Flashcards
Hover Effect
button:hover { background: blue; color: white; } <button>Hover Me</button>
Button turns blue when hovered.
Style Every Other Row
tr:nth-child(odd) { background: lightgray; } <table> <tr><td>Row 1</td></tr> <tr><td>Row 2</td></tr> <tr><td>Row 3</td></tr> </table>
Odd rows get a gray background.
Style Only Checked Checkboxes
input:checked { accent-color: green; } <input type="checkbox" checked> Checked <input type="checkbox"> Not Checked
Checked checkboxes turn green.
Exclude Certain Elements
p:not(.special) { color: gray; } <p>This is normal.</p> <p class="special">This is special.</p>
Only normal <p>
elements turn gray.
First Letter Styling
p::first-letter { font-size: 2em; color: red; } <p>This is a paragraph.</p>
The first letter of the paragraph is bigger and red.
Inserting Content Before and After
h1::before { content: "π₯ "; } h1::after { content: " β"; } <h1>My Heading</h1>
Displays as π₯ My Heading β.
Styling Selected Text
p::selection { background: yellow; color: black; } <p>Try selecting this text!</p>
Selected text highlights in yellow.
Change First Letter Only When Hovered
p:hover::first-letter { color: blue; font-size: 2em; } <p>Hover over this text.</p>
The first letter turns blue only when hovered.
Style the First Letter of First Child Paragraph
p:first-child::first-letter { font-size: 2em; color: red; } <div> <p>First paragraph.</p> <p>Second paragraph.</p> </div>
Only the first letter of the first <p>
is red.
Add βπβ Before External Links
a[href^="http"]:not([href*="mywebsite.com"])::before { content: "π "; color: red; } <a href="https://google.com">Google</a> <a href="https://mywebsite.com">My Website</a>
External links get a π icon, but internal links donβt.
What is Pseudo-Element
(::)
Pseudo-elements allow you to style a specific part of an element, such as the first letter or inserting content.
What is Pseudo-Classess?
(:)
Pseudo-classes act like βtemporary classesβ applied under specific conditions.
Here are some examples of pseudo-classes:
:nth-child(n)
Targets an element based on position among siblings tr:nth-child(odd) { background: lightgray; }
:first-child
Targets the first child of a parent p:first-child { font-weight: bold; }
:last-child
Targets the last child of a parent li:last-child { color: red; }
:not(selector)
Excludes certain elements p:not(.special) { color: gray; }
Here are some examples of psuedo-elements:
::first-letter
Styles the first letter of an element p::first-letter { font-size: 2em; }
::first-line
Styles the first line of an element p::first-line { font-weight: bold; }
::before
Inserts content before an element h1::before { content: "π₯ "; }
::after
Inserts content after an element h1::after { content: " β"; }
::selection
Styles the text when selected p::selection { background: yellow; color: black; }