Here are short, drop‑in CSS snippets for diagnosing common layout, spacing, overflow, stacking, focus/keyboard, contrast, and responsive issues. These are minimal and meant to be temporarily added in DevTools or your CSS while debugging:
Outline all elements (visualize boundaries to spot spacing/layout misalignments):
* {
outline: 1px solid rgba(255, 0, 0, 0.3);
}→ helps see box boundaries and unexpected margins/padding. (whitep4nth3r.com)
Force box-sizing: border-box globally (consistent sizing across elements):
*,
*::before,
*::after {
box-sizing: border-box;
}→ simplifies layout calculations and often fixes overflow/spacing weirdness. (Hoverify)
Highlight overflowing elements (when horizontal scroll appears):
html, body {
border: 5px dashed orange;
overflow-x: hidden;
}Temporary overflow debug: add red for overflowed content:
* {
overflow: visible !important;
}→ Useful to identify sources of unintended scrolling. (Smashing Magazine)
Add temporary high z‑index to root elements (brings all content to front):
body * {
z-index: 9999 !important;
position: relative !important;
}Stacking context helper (outline and label):
[data-debug-stack] {
outline: 2px solid magenta !important;
}→ Stacking contexts are created by transforms, opacity, positioning, etc.; knowing what creates them is key to debugging. (MDN Web Docs)
Focus rings on all focusable elements (improves keyboard debugging):
:focus {
outline: 3px dashed #00f !important;
}All interactive items visible on tab:
a, button, input, textarea, select {
outline: 2px solid cyan !important;
}Temporary high‑contrast mode
html {
filter: contrast(200%) saturate(150%);
}Text vs. background overlay (detect low contrast):
* {
background-color: rgba(255, 255, 0, 0.2) !important;
color: #000 !important;
}(Only for diagnostic use; validate with dedicated contrast tools)
Show active media query breakpoints:
body::before {
content: "Viewport: " attr(data-width);
position: fixed;
top: 0;
left: 0;
background: black;
color: white;
padding: 2px 6px;
font-size: 12px;
z-index: 9999;
}👉 To make this work dynamically, add in DevTools or injected script:
document.body.setAttribute('data-width', innerWidth + 'px');Example responsive grid outline:
@media (max-width: 768px) {
* {
outline: 1px dashed green;
}
}Center any element for alignment testing:
[data-debug-center] {
position: fixed !important;
top: 50% !important;
left: 50% !important;
transform: translate(-50%, -50%) !important;
outline: 3px dashed magenta !important;
}Disable transitions/animations during debug:
* {
transition: none !important;
animation: none !important;
}- Browser DevTools Layout/Box Model inspectors are indispensable for seeing computed margins, padding, and box sizes. (MDN Web Docs)
- Stacking contexts are hierarchical and can be unintuitive; use dedicated stack inspectors in DevTools or tools like Polypane to visualize them. (Polypane)
- Temporary outlines (using
outlinenotborder) don’t affect layout and make debugging easier. (whitep4nth3r.com)