CSS Tutorial menu

CSS Borders

A border is a line drawn around an element, between its padding and its margin.

Border Style

A border needs a style, or it will not show at all. Common values:

ValueLooks like
solidOne plain line
dashedShort dashes
dottedDots
doubleTwo lines
noneNo border

Example

html
<!DOCTYPE html>
<html>
<head>
<style>
p { padding: 8px; }
.a { border-style: solid; }
.b { border-style: dashed; }
.c { border-style: dotted; }
.d { border-style: double; border-width: 4px; }
</style>
</head>
<body>

<p class="a">solid</p>
<p class="b">dashed</p>
<p class="c">dotted</p>
<p class="d">double</p>

</body>
</html>
Try it Yourself »

Width and Colour

  • border-width sets the thickness, such as 1px, 3px or thin
  • border-color sets the colour

Example

css
.box {
  border-style: solid;
  border-width: 2px;
  border-color: #264de4;
}

The Border Shorthand

Most of the time, developers write all three in one line with border: width, style, colour.

Example

html
<!DOCTYPE html>
<html>
<head>
<style>
.receipt {
  border: 2px dashed #555555;
  padding: 15px;
  width: 250px;
}
.total {
  border-top: 1px solid black;
  padding-top: 8px;
  font-weight: bold;
}
</style>
</head>
<body>

<div class="receipt">
  <p>Shan Biryani masala: Rs 180</p>
  <p>Rice 1 kg: Rs 420</p>
  <p class="total">Total: Rs 600</p>
</div>

</body>
</html>
Try it Yourself »

Borders on One Side

You can style each side on its own with border-top, border-right, border-bottom and border-left. In the example above, only the total has a line on top.

A thick left border is a popular way to highlight a note:

Example

css
.note {
  border-left: 5px solid orange;
  padding-left: 10px;
}

Rounded Corners

border-radius rounds the corners. It works even if the element has no border, which is useful for buttons and cards.

Example

html
<!DOCTYPE html>
<html>
<head>
<style>
.btn {
  background-color: #264de4;
  color: white;
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
}
.pill { border-radius: 999px; }
.avatar {
  width: 80px;
  height: 80px;
  background-color: teal;
  border-radius: 50%;
}
</style>
</head>
<body>

<button class="btn">Rounded</button>
<button class="btn pill">Pill shape</button>
<div class="avatar"></div>

</body>
</html>
Try it Yourself »

border-radius: 50% on a square makes a perfect circle.

Outline

An outline looks like a border but is drawn outside the element and does not take up space. Browsers use it to show which input or link has keyboard focus.

Do not remove the focus outline (outline: none) without adding another clear focus style. People who use a keyboard instead of a mouse need it to see where they are.

Exercise

Exercise

Which declaration draws a 3 pixel solid red border?

Learn CSS with a teacher

Join the Web Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp