Educational guide to border
How to define borders and keep control of the box
A border defines the edge of a box. Learn to choose style, thickness and color, adapt sides to writing direction and preserve dimensions and focus.
Mental model
border combines border-width, border-style and border-color on all four sides.
- 1 · ContentThe text or elements inside the box.
- 2 · PaddingInner space between content and border.
- 3 · BorderThe edge you draw with thickness, style and color.
- 4 · MarginOuter space separating the box from other boxes.
The usual order is thickness, style and color: border: 2px solid uses currentColor, the element’s text color. Omitting thickness uses medium; omitting style uses none and the border is not drawn.
solid draws a continuous line; dashed and dotted use dashes or dots; double draws two lines within the total thickness. groove, ridge, inset and outset create relief effects. Choose a readable style and avoid communicating state through color alone.
Use border-width, border-style and border-color to change one dimension without resetting the others. Four values follow top, right, bottom, left order; two values address vertical and horizontal sides.
Physical and logical sides
Choose physical sides for a fixed position and logical sides to follow the content’s writing direction.
border-top, border-right, border-bottom and border-left target physical sides. border-block groups the start and end of the block axis; border-inline groups those of the text axis. border-inline-start follows direction and writing-mode: in horizontal writing it is left in LTR and right in RTL.
An accent that follows writing direction
LTR — Note with a leading accent
RTL — Note with a leading accent
Border, outline and dimensions
border participates in the box model; outline is drawn without reserving space in layout.
With content-box, the declared width covers content: padding and border are added. With border-box, both fit inside the declared width and reduce content space. outline does not change that width and can overlap nearby elements.
Three boxes with the same declared width
The recipe adds a focus outline separated by outline-offset. Try it with Tab: it avoids size changes on focus and preserves a visible signal. Replace the project’s color tokens with your interface tokens when reusing the CSS.
Common mistakes
Before increasing thickness, check style, the cascade and the box model.
The border is invisible
- Expected result
- A visible line around the box.
- Cause
- border: 4px omits style; its initial value is none and the computed thickness becomes zero.
- Solution
- Specify a style, such as border: 4px solid, and check for a nonzero thickness.
The color or image disappears
- Expected result
- Change thickness while preserving everything else.
- Cause
- A later shorthand resets omitted values and border-image.
- Solution
- Use border-width to change thickness alone; declare border-image after the shorthand when needed.
The box grows or content narrows
- Expected result
- Preserve the intended size and readable content.
- Cause
- content-box adds padding and border; border-box subtracts them from content space.
- Solution
- Choose box-sizing deliberately, check minimum sizes and avoid increasing border thickness on focus if it shifts layout.
Focus is no longer visible
- Expected result
- Identify the active element using a keyboard.
- Cause
- outline is removed without another visible signal.
- Solution
- Keep native focus or provide a clear :focus-visible style; do not clip the outline with overflow.
Support and compatibility
Check the Baseline status of border and distinguish classic borders from other experimental properties.
border and logical borders are widely available features. The card reflects Baseline data; examples demonstrate native behavior in the current browser.
Explore outline and its distance from the border.