On this page
CSS Borders, Outlines & Rounded Corners
What Are CSS Borders and Outlines?
A CSS border is a line drawn around an element between its padding and margin. An outline is a similar line drawn outside the border that does not affect layout. Rounded corners come from border-radius. Our focus keyword is CSS borders, and this lesson also covers trending topics: CSS border-radius examples, pill buttons and circles, outline-offset, :focus-visible for accessible focus rings, border-image, gradient borders, logical border properties, and box-shadow rings.
1. The border Shorthand
border: 2px solid #334155; sets width, style and color in one line. Without a style, no border appears, because the default style is none.
border-width:thin,medium,thickor a length like3px.border-style:solid,dashed,dotted,double,groove,ridge,inset,outset,none,hidden.border-color: any CSS color. It defaults tocurrentColor.
2. Individual Sides and Logical Properties
Set a single side with border-top, border-right, border-bottom, border-left. Modern CSS prefers logical properties that adapt to writing direction: border-inline-start, border-inline-end, border-block-start, border-block-end. They make right-to-left layouts (Arabic, Urdu, Hebrew) work automatically.
3. Borders and the Box Model
With the default box-sizing: content-box, borders add to the element's width. Use box-sizing: border-box so that width includes padding and border. Most resets set *, *::before, *::after { box-sizing: border-box; }.
4. border-radius
border-radius rounds corners.
- One value: all corners.
border-radius: 12px; - Two to four values follow the order top-left, top-right, bottom-right, bottom-left.
- Circle: a square element with
border-radius: 50%. - Pill or capsule:
border-radius: 9999px(a huge value works for any height). - Elliptical corners use a slash:
border-radius: 50% / 20%(horizontal radius / vertical radius). - Individual longhands:
border-top-left-radius,border-start-start-radius(logical).
Rounded corners also clip backgrounds, and with overflow: hidden they clip child content, such as images inside cards.
5. outline
An outline is drawn outside the border edge, does not take space in layout and does not change the element's size. Properties: outline-width, outline-style, outline-color, outline-offset and the shorthand outline.
outline-offsetmoves the outline away from the border (positive) or into it (negative).- Modern browsers make outlines follow
border-radius. - Outlines are perfect for focus rings, debugging layout (
* { outline: 1px solid red; }) and hover highlights that must not shift layout.
6. Border vs Outline vs Box-shadow
| Feature | border | outline | box-shadow |
|---|---|---|---|
| Affects layout | Yes | No | No |
| Per-side control | Yes | No | Via offsets |
| Follows border-radius | Yes | Yes (modern) | Yes |
| Offset from box | No | Yes (outline-offset) | Yes (spread) |
| Forced-colors (high contrast) visible | Yes | Yes | No |
7. Accessible Focus Styles
Never write outline: none without a replacement, because keyboard users depend on a visible focus indicator (WCAG 2.4.7 and 2.4.11). Use :focus-visible, which shows the ring for keyboard focus but usually not for mouse clicks.
:focus-visible {
outline: 3px solid #2563eb;
outline-offset: 3px;
}8. border-image
border-image paints an image or gradient on the border: border-image: linear-gradient(45deg, #f43f5e, #6366f1) 1;. It ignores border-radius, so for rounded gradient borders use the two-layer background technique (see the gradients lesson).
9. Double Borders and Rings with box-shadow
Multiple rings: box-shadow: 0 0 0 3px white, 0 0 0 6px #2563eb;. Shadows do not affect layout, but they are not shown in forced-colors mode, so pair them with a transparent outline for accessibility: outline: 2px solid transparent;.
Best Practices
- Use
box-sizing: border-boxglobally. - Use
remorpxradius tokens in your design system (for example 4px, 8px, 16px, 9999px). - Prefer
outlinefor focus and hover states so layout does not jump. - Prefer logical border properties for international sites.
- Match child radius to parent radius minus padding for nested rounded shapes.
Common Mistakes
- Forgetting
border-style, so the border is invisible. - Removing outlines and leaving no focus indicator.
- Using borders for hover effects, causing layout shift.
- Expecting
border-imageto round. - Using
border-radius: 50%on a non-square element and getting an ellipse instead of a circle.
Summary
Borders shape the box, outlines draw attention without shifting layout, and border-radius creates everything from soft cards to circles and pills. Combine them with :focus-visible, logical properties and box-shadow rings for polished, accessible interfaces. Explore the examples below.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Style .pill so it has a 2px solid blue border, a fully rounded pill shape, and padding of 10px 24px. Add an accessible :focus-visible style using outline and outline-offset (do not remove the outline).
Press Run to execute.
Show expected output
A pill-shaped button with a visible orange or blue focus ring when tabbed to.This is a self-check — compare your result with the expected output above.
Turn the .avatar div into a 80px circle with a 4px white border and a soft blue box-shadow ring. Then style .callout with a 5px left border using a logical property (border-inline-start), a light background and 6px radius.
Press Run to execute.
Show expected output
A circular avatar with a ring, and a callout with a colored start-side border.This is a self-check — compare your result with the expected output above.
Was this page helpful?