Codectionary / Developer documentation / CSS

Border Properties

border draws a line around an element's edge, controlled by width, style, and color, which can be set together via the border shorthand or individually per side (border-top, border-right, etc). border-radius rounds the corners, accepting one value for all corners or up to four for individual control.

Syntax

border: width style color;
border-radius: length;

Examples

The border Shorthand

Setting width, style, and color in one declaration.

.box { border: 2px solid #333; }
.dashed { border: 1px dashed #999; }
.top-only { border-top: 3px solid blue; } /* individual sides */

border-radius Variations

Rounding corners uniformly or individually.

.rounded { border-radius: 8px; }               /* all four corners */
.pill { border-radius: 999px; }                 /* fully rounded ends */
.circle { border-radius: 50%; }                 /* on a square element, makes a circle */
.custom { border-radius: 8px 8px 0 0; }         /* top-left top-right bottom-right bottom-left */

Parameters and values

  • border-width (length): Thickness of the border
  • border-style (solid | dashed | dotted | none): Line style of the border
  • border-color (color): Color of the border
  • border-radius (length | %): Rounds the corners of the border box

Best practices

  • Use border-radius: 50% on an element with equal width and height to create a perfect circle
  • Use a very large border-radius value (like 999px) as a common trick for reliably pill-shaped buttons regardless of their size
  • Remember border-style must be set (border defaults to none) or the border will not render even with width and color set
  • Use individual longhand properties (border-top, border-left) when only specific sides need a border, rather than setting border then overriding with border: none on other sides

At a glance

Purpose
Presentation and layout
File extension
.css
Runs in
Web browsers
Usually used with
HTML and JavaScript

Specifications & further reading

Related CSS documentation