CSS Borders
You can paint fills, photos, gradients, and soft fades. Next skill: draw the edge of the box. A border is the line that frames an element — cards, buttons, inputs, and panels all lean on it.
What is a border?
A CSS border sits on the edge of the element’s box. It has three ingredients: width (how thick), style (how it looks — solid, dashed, and friends), and color. Miss the style, and many browsers draw nothing useful even if you set a width.
The three parts
Think of every border as a small recipe: thickness + look + paint.
Longhand width, style, color
css.card {
border-width: 2px;
border-style: solid;
border-color: #0f766e;
padding: 1rem;
background-color: #f0fdfa;
}
Why style matters
border-width alone is not enough. Without a border-style other than the initial none, you usually will not see a line. Start with solid until you need a fancier look.
Shorthand: write it in one line
The border shorthand packs width, style, and color together. A common everyday order is width → style → color.
Border shorthand
css.panel {
border: 2px solid #0f766e;
padding: 1rem;
background-color: #ecfeff;
}
Common border styles
Style is the personality of the line. Memorize a small everyday set first.
📊 Common border-style values
| Keyword | Look | Everyday use |
|---|---|---|
solid |
Continuous line | Cards, buttons, form fields |
dashed |
Short dashes | Placeholders, “drop zone” hints |
dotted |
Dots | Soft dividers, subtle frames |
double |
Two parallel lines | Decorative emphasis (use sparingly) |
none |
No border drawn | Reset / remove an inherited edge |
hidden |
Like none for collapsing table borders | Tables later — know the name exists |
Try a few styles side by side
Same width and color; only the style changes.
Solid, dashed, dotted, double
css.box-solid { border: 3px solid #134e4a; }
.box-dashed { border: 3px dashed #134e4a; }
.box-dotted { border: 3px dotted #134e4a; }
.box-double { border: 3px double #134e4a; }
One side at a time
You can style top, right, bottom, and left separately — useful for underlines, left accent bars, or asymmetric cards.
Per-side borders
css.quote {
border-top: none;
border-right: none;
border-bottom: none;
border-left: 4px solid #0f766e;
padding: 0.75rem 1rem;
background-color: #f8fafc;
}
Shorthand per side
border-left: 4px solid #0f766e is the everyday way to set one edge without touching the others.
Everyday card habit
A light border plus padding and a soft fill reads as a clear card without shouting.
Simple bordered card
css.card {
border: 1px solid #cbd5e1;
border-radius: 0; /* rounding is the next lesson — keep edges square here */
padding: 1rem;
background-color: #ffffff;
color: #0f172a;
}
Removing a border
To clear a border, prefer border: none (or border-style: none). Setting only border-color: transparent can still leave width/style taking space in some layouts — none is the clean reset.
Reset with border none
css.plain {
border: none;
}
Try it yourself
- Build a card with longhand
border-width,border-style: solid, and a tealborder-color. - Rewrite the same look with
border: 2px solid #0f766e. - Make four boxes: solid, dashed, dotted, and double at the same thickness.
- Create a quote block with only a thick left border.
- Start from a bordered button, then remove the edge with
border: none.
Common mistakes
- Setting width and color but forgetting
border-style(so nothing shows) - Mixing up border (the drawn edge) with margin (outer gap) — different tools
- Expecting rounded corners from
borderalone — that isborder-radius(next lesson) - Using a loud different color on every side “just because”
- Leaving a thick transparent border when you meant
border: none
Summary
- A border needs width, style, and color
border: 2px solid #0f766eis the everyday shorthand- Learn
solid,dashed,dotted,double, andnonefirst - Style sides separately when the design asks for it
- Reset cleanly with
border: none - Keep corners square for now — rounding comes next
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top