02. Borders, Outlines, outline-offset & Box/Text Shadows
ЁЯЯв Beginner
1. Definition
Borders frame element box boundaries. outline sits outside the border without consuming box-model space (ideal for accessible focus indicators with outline-offset). box-shadow creates 3D elevation depth, and drop-shadow() follows alpha transparency silhouettes.
2. Why It Is Used
Outlines provide accessible focus indicators for keyboard navigation. Box shadows create visual hierarchy and elevation levels across cards and modals.
3. How It Works
Unlike borders, outlines do not trigger layout reflows when toggled on focus. box-shadow accepts x-offset y-offset blur spread color.
4. Syntax
.card {
border: 1px solid #e2e8f0;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}
5. Border & Outline Differences Table
| Feature | Border | Outline |
|---|---|---|
| Layout Space | Consumes box-model width & height | Does NOT consume space (Overlays) |
| Rounded Corners | Follows border-radius | Follows radius in modern browsers |
| Offset Spacing | N/A (Sits at padding edge) | Custom gap via outline-offset: 4px |
6. Beginner Example
.avatar {
border-radius: 50%;
border: 2px solid #2563eb;
}
7. Intermediate Example
/* Accessible focus ring with outline-offset */
button:focus-visible {
outline: 2px solid #2563eb;
outline-offset: 4px; /* Creates clean gap between button & focus ring */
}
8. Advanced Elevation Shadow Example
.elevation-2 {
box-shadow:
0 4px 6px -1px rgba(0, 0, 0, 0.1),
0 2px 4px -2px rgba(0, 0, 0, 0.1);
}
9. Line-by-Line Code Explanation
outline-offset: 4px;: Leaves a 4-pixel transparent whitespace gap between the button border and focus ring for crisp visibility.
10. Expected Terminal / Browser Output
A focused button displaying a distinct, accessible blue outline ring offset 4px away from its border.
11. Common Mistakes & Pitfalls
- Setting
outline: nonewithout a focus replacement: Removing focus rings destroys accessibility for keyboard users. Always use:focus-visiblewith custom outlines.
12. Important Rules & Limitations
- Never remove default focus outlines unless replacing them with an equally visible
:focus-visiblestyle.
13. Multilingual Explanation: English
Borders consume box model space, while outlines sit outside without affecting layout width. Use outline-offset for accessible focus rings.
14. Multilingual Explanation: Hindi (Roman Script)
Borders layout space lete hain par outlines nahi. Keyboard accessibility ke liye outline-offset ke saath custom focus ring dejiye.
15. Multilingual Explanation: Marathi (Roman Script)
Borders space рд╡реНрдпрд╛рдкрддрд╛рдд pan outlines space ghet рдирд╛рд╣реАрдд. Accessibility sathi outline-offset cha waparn karun focus ring banva.
16. Practice Questions
- Why does toggling
outlineon hover avoid layout jumps compared to togglingborder? - Why is
:focus-visiblepreferred over:focusfor button outlines?
17. Practical Coding Challenge
Write CSS rules for a .custom-input:focus-visible with outline: 2px solid #2563eb and outline-offset: 2px.