CSS Border
Borders are the visible edge of every element — and in CSS they are far more powerful than a simple line. With border styles, radius, individual sides and gradient borders, you can build beautiful UI components without a single image.
border-width, border-style, border-colorborder shorthandborder-top, border-bottom, etc.border-radius — circles, pills,
and custom shapesWhat is a CSS Border?
A border is a line drawn around an element's padding (and content). It sits between the padding and the margin in the CSS Box Model. A border has three characteristics that you must define for it to be visible:
thin, medium, thick.color (text colour) if not set. Accepts any CSS colour value..box {
border-width: 2px;
border-style: solid; /* ← required for border to appear */
border-color: #264de4;
}
/* Without border-style, no border renders */
.invisible {
border-width: 5px;
border-color: red;
/* No border-style → nothing visible */
}
/* border-color defaults to the element's text color */
.inherits-color {
color: #264de4;
border: 2px solid; /* border is also blue */
}
Border Styles — All 9 Values
CSS supports 9 named border style values. solid is
by far the most common in real projects, but the others are useful for specific design effects:
.solid { border-style: solid; } /* continuous line — most common */
.dashed { border-style: dashed; } /* dashes — upload areas, dividers */
.dotted { border-style: dotted; } /* dots */
.double { border-style: double; } /* two lines — needs width ≥ 3px */
.groove { border-style: groove; } /* 3D carved-in look */
.ridge { border-style: ridge; } /* 3D raised look */
.inset { border-style: inset; } /* sunken into page */
.outset { border-style: outset; } /* popping out of page */
.none { border-style: none; } /* no border (default) */
.hidden { border-style: hidden; } /* same as none but for table border conflicts */
/* Mix styles on different sides */
.mixed {
border-style: solid dashed dotted double;
/* top right bottom left */
}
The border Shorthand
The border shorthand combines all three
sub-properties in one declaration. The order is: width → style → color. All three values
are optional except style — but you should always include all three for clarity.
/* border: width style color */
.card { border: 1px solid #e5e7eb; }
.btn { border: 2px solid #264de4; }
.error { border: 2px solid #ef4444; }
.dashed { border: 2px dashed #264de4; }
.success { border: 2px solid #22c55e; }
/* Remove border */
.no-border{ border: none; }
button { border: 0; } /* also removes border */
/* Using CSS keywords for width */
.thin { border: thin solid #264de4; } /* ~1px */
.medium { border: medium solid #264de4; } /* ~3px */
.thick { border: thick solid #264de4; } /* ~5px */
/* Using currentColor — matches text color */
.icon-btn {
color: #264de4;
border: 2px solid currentColor; /* border matches text */
}
Individual Side Borders
You can set borders on specific sides only using the four individual shorthand
properties: border-top, border-right, border-bottom, border-left. Each accepts the same width style color format.
/* Individual shorthand — width style color */
.top-only { border-top: 2px solid #264de4; }
.bottom { border-bottom: 1px solid #e5e7eb; }
.left-bar { border-left: 4px solid #264de4; } /* card accent */
/* Individual sub-properties per side */
.custom {
border-top-width: 3px;
border-top-style: solid;
border-top-color: #264de4;
border-right-width: 0; /* no right border */
border-right-style: none;
}
/* Common pattern: reset then add specific side */
.underline {
border: none; /* clear all */
border-bottom: 2px solid #264de4; /* add bottom only */
}
/* Width shorthand accepts 1–4 values (same as padding/margin) */
.mixed-width {
border-style: solid;
border-width: 4px 1px 4px 1px; /* top right bottom left */
border-color: #264de4;
}
border-left: 4px solid #264de4 is one of the most used patterns in UI
design — it creates a coloured left accent bar on cards, blockquotes, alerts and sidebar menu items. Always
pair it with a small padding-left to indent the content away from the
bar.
border-radius — Rounded Corners
border-radius rounds the corners of an element. It
works whether or not a visible border exists — it also clips the background colour and any child overflow to
the rounded shape.
/* Single value — all four corners */
.card { border-radius: 12px; }
.btn { border-radius: 8px; }
.pill { border-radius: 999px; } /* fully rounded ends */
.circle { border-radius: 50%; } /* circle when width = height */
/* Two values — top-left&bottom-right | top-right&bottom-left */
.diagonal { border-radius: 12px 4px; }
/* Four values — TL TR BR BL (clockwise from top-left) */
.custom { border-radius: 12px 4px 12px 4px; }
/* Individual corners */
.modal {
border-radius: 16px; /* all corners */
border-bottom-left-radius: 0;
border-bottom-right-radius: 0; /* flat bottom */
}
/* Elliptical radius — horizontal / vertical */
.leaf { border-radius: 60% 40% 60% 40% / 40% 60% 40% 60%; }
Advanced Shapes with border-radius
By setting individual corner radii with different values — and using the elliptical
horizontal/vertical syntax — you can create organic shapes without any
SVG or images:
Creative Border Effects
CSS borders can do far more than a simple line. Here are the most useful creative effects — all pure CSS, no images:
/* ── Gradient border using pseudo-element ── */
.gradient-border {
border: 3px solid transparent;
background-clip: padding-box;
position: relative;
}
.gradient-border::before {
content: '';
position: absolute;
inset: -3px; /* matches border-width */
border-radius: inherit;
background: linear-gradient(135deg, #264de4, #a855f7);
z-index: -1;
}
/* ── Top accent card ── */
.accent-card {
border: 1px solid #e5e7eb;
border-top:3px solid #264de4; /* accent overrides top */
border-radius: 8px;
}
/* ── Left accent blockquote ── */
blockquote {
border: none;
border-left: 4px solid #264de4;
padding-left: 16px;
font-style: italic;
}
/* ── border-image gradient underline ── */
.gradient-line {
border-bottom: 3px solid transparent;
border-image: linear-gradient(90deg, #264de4, #a855f7) 1;
}
/* ── Double border with outline ── */
.double-ring {
border: 2px solid #264de4;
outline: 2px solid #264de4;
outline-offset: 4px; /* gap between border and outline */
}
border-image
border-image replaces the standard border colour
with a gradient or image that is sliced and applied to each border segment. It is the only CSS-native way to
have a true gradient border without a pseudo-element hack.
/* border-image: source slice / width / outset repeat */
/* Gradient border — requires border-style to be set */
.gradient-border {
border: 3px solid transparent;
border-image: linear-gradient(135deg, #264de4, #a855f7) 1;
/* The '1' at the end = slice value (fill the border area) */
}
/* Gradient bottom line only */
h2 {
border-bottom: 3px solid;
border-image: linear-gradient(90deg, #264de4, transparent) 1;
}
/* Note: border-image does NOT work with border-radius! */
/* For rounded gradient borders use the pseudo-element */
/* technique shown in the creative effects section above. */
border-image overrides border-radius
— rounded corners will not appear when you use border-image. For rounded
gradient borders, use the ::before pseudo-element technique shown in the
creative effects section.
Try It Yourself
This editor showcases borders in real components — cards, buttons, badges, blockquote
and an alert. Try changing border-style, border-radius or border-color values to
experiment.
Quick Check — Chapter 13
1. border-width — how thick the border line is. Accepts px, rem, em or keywords (
thin, medium, thick). Defaults to medium.2. border-style ← REQUIRED — the pattern of the line:
solid, dashed, dotted, double, groove, ridge, inset, outset, none. Without this property set, no border renders
regardless of width and colour.3. border-color — the colour of the border. Defaults to the element's current
color (text colour) if not specified.The shorthand is
border: width style color, e.g. border: 2px solid #264de4.
border-radius: 50% on an element where the width equals
the height. The 50% value creates a radius equal to half the element's width/height on all
corners, which produces a perfect circle.Example:
.avatar { width: 48px; height: 48px; border-radius: 50%; }If the element is not square (width ≠ height),
border-radius: 50%
produces an ellipse instead. For avatar images, always set both width and height to the same
value.For a fully rounded pill (like a badge or button), use
border-radius: 999px instead — this rounds the shorter dimension
completely regardless of the element's proportions.
width style color format:border-top: 2px solid #264de4border-right: 1px dashed #264de4border-bottom: 2px solid #264de4border-left: 4px solid #264de4A common pattern is to reset all borders then add only specific ones:
border: none;border-bottom: 1px solid #e5e7eb;Or to override a single side after the shorthand:
border: 1px solid #e5e7eb;border-top: 4px solid #264de4; — this top overrides the top border
from the shorthand.
Method 1 — border-image (simple, no border-radius):
border: 3px solid transparent;border-image: linear-gradient(135deg, #264de4, #a855f7) 1;Limitation:
border-image overrides border-radius — corners won't be rounded.Method 2 — pseudo-element (supports border-radius):
Set the element to
border: 3px solid transparent; background-clip: padding-box; position: relative,
then use a ::before pseudo-element with the gradient as its
background, positioned with inset: -3px and z-index: -1. This creates the gradient behind the element's border,
producing rounded gradient borders.
border-radius: 12px — applies 12px
rounding to all four corners equally.2 values:
border-radius: 12px 4px — first value =
top-left and bottom-right, second = top-right and bottom-left.4 values:
border-radius: 12px 4px 8px 20px —
clockwise from top-left: TL=12px, TR=4px, BR=8px, BL=20px.Individual corner properties:
border-top-left-radius, border-top-right-radius, border-bottom-right-radius, border-bottom-left-radius.The elliptical slash syntax
border-radius: 60% 40% / 40% 60% sets
separate horizontal and vertical radii for each corner — used for organic blob shapes.
border
shorthand?border-radius makes a perfect
circle (assuming width equals height)?border-image with a gradient, what
limitation applies?border: 2px solid currentColor mean?
- A CSS border has three characteristics:
border-width,border-style(required), andborder-color. Withoutborder-style, nothing renders. - CSS supports 9 border styles:
solid,dashed,dotted,double,groove,ridge,inset,outset,none. - Shorthand order:
border: width style color.border-colordefaults tocurrentColor(inherits text colour). - Individual sides:
border-top,border-right,border-bottom,border-left— samewidth style colorformat. border-radiusrounds corners.50%= circle (when square).999px= pill. Accepts 1–4 values clockwise from top-left. Elliptical radii with/syntax for organic shapes.- Gradient border — two methods:
border-image(simple but breaksborder-radius) or the::beforepseudo-element technique (supports rounded corners). - Common patterns: left-accent blockquotes/cards, top-accent section cards, pill badges with
border-radius: 999px, outline buttons with transparent background.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.