CSS Border
Table of Contents + โ
In the last lesson you learned about CSS Padding. Padding is the space between your content and the edge of the box. Now letโs look at that edge itself. That edge is the border.
๐ผ๏ธ What is a Border?
A border is the line that goes around a box. Think of a photo in a picture frame. The photo is your content. The frame around it is the border. You can make that frame thin or thick. You can paint it any color. You can even make it solid or dotted.
In CSS, a border is a line drawn around an HTML element. It sits between the padding and the margin. So if you remember the box model, the order from inside to outside is content, then padding, then border, then margin.
A border has three parts you control:
- The width. This is how thick the line is.
- The style. This is the look of the line, like solid or dotted.
- The color. This is the color of the line.
๐ค Why do we need borders?
Sometimes a box just blends into the page. You cannot see where it starts and where it ends. A border fixes that. It draws a clear line so the readerโs eye knows โthis is one boxโ.
Here are some everyday reasons to use a border:
- To make a card or box stand out from the rest of the page.
- To draw a line under a heading or a menu item.
- To show a buttonโs shape so people know they can click it.
- To highlight an input field when someone is typing in it.
So a border is not just decoration. It helps people understand your layout.
๐งฉ The Border Syntax
The fastest way to add a border is the shorthand. A shorthand is one property that sets many things at once. You write all three parts on one line. The order is width, then style, then color.
This code puts a thin grey solid line around a box.
.box { border: 2px solid grey;}Letโs walk through that one line:
2pxis the width. The line is 2 pixels thick.solidis the style. The line is one full line with no gaps.greyis the color. The line is grey.
What you see
A grey rectangle outline around the box. The line is thin and unbroken, like a frame drawn with a single pen stroke.
Now here is one thing you must remember. The style is the important part. If you skip the style, you see no border at all. So border: 2px grey; shows nothing. But border: solid grey; does show a line. So always include the style.
๐จ Border Styles
The style decides how the line looks. You can change just this part and get a very different feel.
Here are the styles you will use most:
| Style | How it looks |
solid | One full unbroken line |
dashed | Short lines with gaps between them |
dotted | A row of small dots |
double | Two lines next to each other |
none | No border at all |
This code shows a dashed blue border.
.note { border: 3px dashed blue;}๐ง The Long Way: Each Property On Its Own
The shorthand is quick. But sometimes you want to set just one part. For that, CSS gives you three separate properties.
This code sets the same border but one piece at a time.
.box { border-width: 2px; border-style: solid; border-color: grey;}Each line does one job:
border-widthsets the thickness.border-stylesets solid, dashed, dotted, and so on.border-colorsets the color.
Most of the time the shorthand is enough. Use the long way only when you need to change one part on its own.
๐ Borders on One Side Only
A border does not have to go all the way around. You can put it on just the top, bottom, left, or right. This is great for a line under a heading.
CSS has a property for each side:
border-topborder-bottomborder-leftborder-right
This code draws a line only under the heading. The other three sides have no border.
.heading { border-bottom: 2px solid black;}What you see
A single black line sitting right below the heading text. No line on the top, left, or right. It looks like the heading is underlined with a neat bar.
This is one of the most common uses of a border-bottom. You see it under headings and menu links all over the web.
๐ข Rounded Corners with border-radius
Real cards and buttons rarely have sharp corners. They have soft, rounded ones. The border-radius property rounds the corners of your box.
A bigger value means rounder corners. A small value gives a gentle curve.
This code makes a button with soft corners.
.button { border: 2px solid green; border-radius: 8px;}What you see
A green outlined button where all four corners are gently curved instead of sharp. It looks friendlier and more like the buttons you tap in apps like WhatsApp or Instagram.
Make a circle
If you set border-radius: 50%; on a square box, it turns into a full circle. This is how round profile pictures are made.
So border-radius is the property you use any time you want soft corners instead of sharp ones.
๐ก A Real Example: A Simple Card
Letโs put it together. Here is a small card, like a contact card you might see in an app. First the HTML.
<div class="card"> <h3>Alex Carter</h3> <p>Web Developer</p></div>Now the CSS gives the card a border, soft corners, and some padding so the text does not touch the edge.
.card { border: 1px solid #ccc; border-radius: 10px; padding: 16px;}What you see
A neat box with a light grey outline and rounded corners. The name and job title sit inside with comfortable space around them. It looks like a clean little card on the page.
โ ๏ธ Common Mistakes
A few small things trip up beginners with borders. Watch out for these.
- Forgetting the style.
border: 2px red;shows nothing because no style is set. You needborder: 2px solid red;. - A typo in a color name. A word like
darkgraymust be spelled exactly right. If you misspell it, no color shows. - Forgetting the unit on
border-width. Write2px, not just2. A plain number with no unit is ignored. - Forgetting that a border makes the box bigger. A border adds to the size of the box, just like padding does.
Here is the wrong way and the right way side by side.
/* โ Avoid: no style, so no border shows */.box { border: 2px red;}
/* โ
Good: width, style, and color all set */.box { border: 2px solid red;}So the fix for most border problems is simple. Always check that you set a style.
โ Best Practices
Keep your borders clean and easy to manage with these habits.
- Use the shorthand
border: width style color;for normal cases. It is short and clear. - Use a single side like
border-bottomfor underlines instead of a full box. - Keep border colors soft for cards, like
#cccor#ddd. Hard black can look heavy. - Use the same border-radius across your buttons and cards so the page feels consistent.
๐งฉ What Youโve Learned
Letโs quickly go over the main points from this lesson.
- โ A border is the line around a box, sitting between padding and margin.
- โ
The shorthand is
border: width style color;and the style part is required. - โ
Styles include
solid,dashed,dotted, anddouble. - โ
You can set one side only with
border-top,border-bottom,border-left, orborder-right. - โ
border-radiusrounds the corners, and50%makes a perfect circle.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
Which border shorthand actually shows a visible line?
Why: The style (like solid) is required. Without it, no border shows. So border: 2px solid red; is the one that works.
- 2
What is the correct order in the border shorthand?
Why: The shorthand order is width, then style, then color, like border: 2px solid grey;.
- 3
How do you draw a line only under a heading?
Why: border-bottom adds a border to just the bottom side. That is perfect for an underline effect.
- 4
What does border-radius: 50%; do to a square box?
Why: On a square box, border-radius: 50%; rounds the corners all the way into a full circle. This is how round profile pictures are made.
๐ Whatโs Next?
Next you will learn how to control the size of your boxes by setting their width and height.