1.8 KiB
1.8 KiB
HTML/CSS Patterns and Traps
HTML
- Semantic elements for SEO/a11y — Use
<main>,<article>,<nav>,<header>,<footer>; screen readers depend on them <button>vs<a>— Buttons for actions, anchors for navigation; mixing breaks keyboard/screen reader UX<img alt="">— Decorative images need empty alt (not missing); screen readers announce filename otherwise- Self-closing tags — In HTML5,
<br/>works but<br>is canonical;<div/>is WRONG (not self-closing) - ID must be unique — Duplicate IDs break
querySelector, labels, and ARIA; use classes for styling <input type="number">— Allowse,+,-characters; validate server-side for real numbers- Hidden content still in DOM —
display: nonehides from screen readers;visibility: hiddendoesn't remove from flow
CSS
margin: autoneeds width — Won't center without explicitwidthormax-widthon block elementsz-indexneeds position — Only works on positioned elements (relative,absolute,fixed,sticky)- Flexbox gap support —
gapworks in flexbox in all modern browsers; no need for margin hacks - Grid auto-fit vs auto-fill —
auto-fitcollapses empty tracks;auto-fillpreserves them 100vhon mobile — Includes address bar; use100dvhor JS for true viewport height:focus-visible— Shows focus ring only for keyboard users; cleaner than removing:focusoutlines- Cascade layers —
@layercontrols specificity across files; newer than!importanthacks - Custom properties scope — CSS variables cascade; define on
:rootfor global, on element for local calc()whitespace —calc(100% -20px)fails; needs spaces:calc(100% - 20px)- Transform origin — Default is center; for corner rotations, set
transform-origin: top left