Selectors, Pseudo-classes & Specificity – HTML & CSS
Combinators, attribute and pseudo selectors and how conflicts are resolved.
Besides element, class and ID selectors there are combinators: descendant a b (space), child a > b, adjacent sibling a + b, all following siblings a ~ b. On top of that come attribute selectors ([type="email"], [href^="https"]) and pseudo-classes such as :hover, :focus-visible, :first-child, :nth-child(odd), :not(.x).
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
nav a { color: #06c; margin-right: 10px; } /* descendant */
ul > li { list-style: square; } /* direct children only */
h2 + p { font-weight: bold; } /* paragraph directly after h2 */
li:nth-child(odd) { background: #eee; }
li:not(:last-child) { border-bottom: 1px solid #ccc; }
a[href^="https"]::after { content: " ↗"; } /* mark external links */
input[type="text"]:focus { outline: 3px solid orange; }
</style>
<nav><a href="#a">Internal</a><a href="https://example.com">External</a></nav>
<h2>List</h2><p>Directly after h2 (bold)</p>
<ul><li>One</li><li>Two<ul><li>Nested</li></ul></li><li>Three</li></ul>
<input type="text" placeholder="Focus me">Specificity decides in conflicts: it is counted as a triple (ID, class/attribute/pseudo-class, element) and compared from left to right. #a = (1,0,0) beats any number of classes; .a .b p = (0,2,1) beats .a p = (0,1,1). Inline styles beat everything except !important. :where() has specificity 0, :is()/:not() take that of their most specific argument.
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
p { color: gray; } /* (0,0,1) */
.karte p { color: blue; } /* (0,1,1) */
.karte .text { color: green; } /* (0,2,0) */
#haupt .text { color: crimson; } /* (1,1,0) wins */
p.text[lang] { border-left: 4px solid orange; padding-left: 6px; }
</style>
<div class="karte" id="haupt">
<p class="text" lang="en">Which color? crimson – the ID rule wins.</p>
</div>Pseudo-elements (double colon) create or target parts of an element: ::before/::after (generated content via content), ::first-letter, ::first-line, ::marker, ::placeholder, ::selection.
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.zitat::before { content: "«"; color: #e34f26; font-size: 1.5em; }
.zitat::after { content: "»"; color: #e34f26; font-size: 1.5em; }
.anfang::first-letter { font-size: 2.4em; float: left; line-height: .9; margin-right: 6px; color: #06c; }
::selection { background: gold; }
</style>
<p class="zitat">Decoration via CSS</p>
<p class="anfang">The first letter becomes a drop cap. Select this text to see ::selection.</p>.a .b .c .d span) and IDs for styling: they drive specificity up, so that later you can only override with even more specificity or !important. Keep selectors flat – e.g. with class names following BEM (.karte__titel).