Try dashed instead of solid, or bump 2px up to 6px. The sections
below go through each part, then the jobs people actually need borders for: one side only, rounded corners, hover
effects that don’t wobble, and tables.
The border shorthand
border sets the same line on all four sides. It takes up to three values, width, style and color, and
the browser works out which is which, so border: solid #7c4a2d 2px means exactly the same thing. Most
people write width first anyway, because that’s how you’ll see it in other people’s CSS.
Here’s a tag with a width and a color but no style:
Nothing. The style defaults to none, and a border with style none has a width of 0 no
matter what number you gave it. Add solid between the two values and the green line appears. The
other two parts have sensible defaults:
- No width? You get
medium, which browsers draw 3px wide.border: solidon its own is a perfectly good 3px border. - No color? The border uses the element’s text color. More on that below, because you’ll use it a lot.
Border styles: solid, dashed, dotted and more
There are eight visible styles. Here they all are with the same 5px brown border, so you can compare:
solid covers most of what you’ll build. dashed works well for “drop a file here” areas and
coupons, and dotted for a light divider. A few things that aren’t obvious from the picture:
-
doubleneeds at least 3px. It splits the width into two lines and a gap. At 1px or 2px there’s no room, so you get one plain line. Change the 5px above to 2px and watch it turn solid. -
groove,ridge,insetandoutsetfake a 3D edge by drawing darker and lighter shades of your color. They look like the web in 1999, and the exact shades differ a little between browsers. -
noneandhiddenboth remove the border. The difference only shows up in tables with collapsed borders, wherehiddenalso wins over the neighboring cell’s border.
Border width and color
Width is usually in pixels: 1px for a quiet outline, 2-4px when the border is the point. The keywords
thin, medium and thick exist too (1px, 3px and 5px in Chrome), but numbers
are easier to read later.
For the color, any CSS color works: a name, a hex code, rgb(). Or leave it out on purpose. The border
then follows the text color, so one change recolors both:
Neither tag sets a border color, and each border matches its text. The Spicy tag only overrides
color. The keyword behind this is currentColor, and you can write it yourself, for
example border-bottom: 1px solid currentColor on a link. Need a different shade? The
color picker gives you hex codes to paste in.
A border on one side only
Each side has its own shorthand: border-top, border-right, border-bottom and
border-left. They take the same three values. A line under a heading and a colored bar beside a note
are the two you’ll use the most:
For finer control, every part of every side has a longhand like border-left-color. And
border-width, border-style and border-color take 1 to 4 values that go
clockwise from the top, the same pattern as margin and padding:
/* one side, one part */
.note { border-left-color: #b91c1c; }
/* 1 to 4 values: top, right, bottom, left */
.card {
border-style: solid;
border-width: 1px 1px 4px;
border-color: #d6d3d1 #d6d3d1 #7c4a2d;
} That card gets a thin gray line on three sides and a thicker brown one along the bottom.
Three sides, not four
For a border everywhere except the bottom, write the shorthand first and then take one side away. The order matters, and this tab has it backwards:
The bottom line is still there, because border comes second and sets all four sides again. Swap the two
lines so border-bottom: 0 comes last, and the tab opens at the bottom.
Rounded corners with border-radius
border-radius rounds the corners. It rounds the background too, so it works on boxes with no border at
all:
- Small numbers like 6px to 12px soften cards and buttons.
- A huge number like
999pxgives a pill shape, whatever the height. The browser shrinks it to fit. -
50%on a square makes a circle. On a rectangle it makes an oval, so set the same width and height for avatars.
Each corner can have its own value, and there’s a slash syntax for egg and blob shapes. That’s easier to get right by dragging than by typing, so try the border radius generator.
Borders make the box bigger
By default, width sets the content area only. The border is drawn outside it, so a 4px border on a
200px box makes it 208px wide:
The first box is 4px wider on each side than its width says. The second has box-sizing: border-box, so
the border is squeezed inside the 200px instead. Plenty of stylesheets switch every element to border-box right at
the top; the box model tutorial shows that reset and explains the
padding side of the sum.
A border that doesn’t move anything
Adding a border on hover looks like a small change, but it adds width, and that shoves everything next to it sideways. Hover over each Latte button below and keep an eye on Mocha:
On the first row, Mocha jumps 4px to the right and the rows underneath drop a little. The second row already has a 2px border in
transparent and only changes its color on hover, so the size never changes. The third row draws the
line with box-shadow, which takes no space at all. An outline doesn’t take space either.
Which one to pick:
| Line drawn with | Takes space? | Rounded corners? | Good for |
|---|---|---|---|
border | Yes | Yes | Lines that are part of the design, one side only |
border in transparent | Yes, from the start | Yes | Hover and selected states that change color |
outline | No | Yes, in current browsers | Keyboard focus rings, with outline-offset for a gap |
box-shadow: 0 0 0 2px | No | Yes | Hover rings, a second line outside the border |
Older browsers drew outlines as plain rectangles even on rounded buttons, which is why you’ll still see box-shadow
rings in a lot of code. The fourth value in 0 0 0 2px is the spread, the shadow’s thickness; the
box shadow generator lets you play with it. If you change a focus style,
keep it visible: people using a keyboard rely on it.
Gradient borders and border-image
border-color only takes plain colors. For a gradient, use border-image, which paints an
image (a gradient counts) into the border area:
The 1 at the end is the slice value, which tells the browser how to cut the image up for the edges.
Delete it and only the four corners get painted. You still need border: 6px solid first, because
border-image draws into whatever border width exists.
The catch: border-image ignores border-radius. Add border-radius: 14px above
and the corners stay square. For a rounded gradient border, stack two backgrounds instead, a white one inside the
padding and the gradient underneath it reaching out to the border edge:
The border is transparent, so the gradient behind it shows through. The first layer, the plain
#fff one, is clipped to the padding box and fills the inside of the card; change both
#fff values to the color you want inside the border. The
gradient generator builds the gradient part.
Borders on tables
A border on <table> only draws around the outside. For lines between cells, put the border on
th and td. Each cell draws its own box, so two cells side by side would show two lines with
a gap between them; border-collapse: collapse on the table merges them into a single line:
Delete the border-collapse rule to see the doubled lines with gaps between them. Lines between rows
only, rounded table corners and zebra stripes are all in the table border
tutorial.
When the border won’t show up
Go through these in order. The first one covers most cases.
- No style.
border: 1px #ccchas no style, so it draws nothing. Addsolid. - Same color as the background. A white border on a white page is there, you just can’t see it. Set it to red for a moment to check.
- A rule further down wins. A later
border: none, or a shorthand that resets a side you set earlier (like the tab above). Your browser’s dev tools show crossed-out lines for rules that lost. - An inline element. A border on a
<span>or<a>is drawn, but its top and bottom don’t push the lines above and below apart, so a thick one can overlap the text there. Usedisplay: inline-blockif it needs room. - A typo.
boarder,soliedor a missing semicolon on the line before. The browser skips the whole declaration without a word. Paste your CSS into the CSS Validator to find it. - A 1px or 2px
double. It shows, but as one line. Make it 3px or more.
Questions people ask
How do I add a border in CSS?
Use the border shorthand with a width, a style and a color: .card { border: 2px solid #7c4a2d; }. The three values can go in any order. The style (solid, dashed, dotted...) is the only one you can't leave out; without it the border has a width of 0 and nothing shows.
Why is my CSS border not showing?
Most often the style is missing: border: 1px #ccc draws nothing, border: 1px solid #ccc does. Other causes: the border is the same color as the background, a later rule sets border: none or resets that side, there is a typo like boarder, or the declaration before it is missing its semicolon.
How do I add a border to only one side?
Use the side's own shorthand: border-bottom: 2px solid #e5e7eb; under a heading, or border-left: 4px solid #f59e0b; beside a note. There is border-top, border-right, border-bottom and border-left, and each takes the same width, style and color as border.
How do I make rounded borders in CSS?
Add border-radius next to the border: .card { border: 1px solid #d6d3d1; border-radius: 12px; }. A large value like 999px makes a pill shape, and border-radius: 50% on an element with the same width and height makes a circle. It rounds the background too, even when there is no border.
What border styles are there in CSS?
solid, dashed, dotted, double, groove, ridge, inset and outset, plus none and hidden, which both remove the border. double needs a width of at least 3px to show two lines. groove, ridge, inset and outset draw lighter and darker shades of your color to look 3D.
How do I add a border without changing the element's size?
Either give it a transparent border of the same width from the start (border: 2px solid transparent) and only change border-color on hover, or draw the line with something that takes no space: outline: 2px solid #7c4a2d or box-shadow: 0 0 0 2px #7c4a2d. You can also use box-sizing: border-box so the border fits inside the width you set.
What is the difference between border and outline?
A border is part of the box: it takes up space and can be set per side. An outline is drawn around the outside without taking space, can't be set for one side only, and can be moved away from the edge with outline-offset. Browsers use outlines for keyboard focus because showing one doesn't make the page move.
How do I make a gradient border?
With square corners: border: 6px solid; border-image: linear-gradient(90deg, #f97316, #7c3aed) 1;. border-image ignores border-radius, so for rounded corners use a transparent border with two backgrounds: background: linear-gradient(#fff, #fff) padding-box, linear-gradient(90deg, #f97316, #7c3aed) border-box; border: 6px solid transparent; border-radius: 14px;.
Tools that help
- CSS Border Radius Generator Drag the corners of a box and copy the border-radius.
- CSS Box Shadow Generator A shadow with spread works as a border that takes no space.
- HTML Table Generator Build a table with cell borders already set up.
- CSS Validator Spots typos like boarder or solied that make a border vanish.