← Back to HTML & CSS, page 4

html-css-0065 · Easy fresher

How do border, border-radius, box-shadow and outline differ, and how do you make a circle or a pill shape?

border is part of the box and changes its size, border-radius rounds corners (50% gives a circle on a square, a large value gives a pill), box-shadow and outline are drawn without taking space. Outline is the property to use for focus rings.

Full answer

These four properties draw lines and shapes around a box, but they behave differently.

  • border is part of the box. It takes space and changes the size of the element (unless box-sizing: border-box absorbs it into the width).
  • border-radius rounds the corners of the box and its background. A value of 50% on a square makes a circle. A very large value such as 999px on a rectangle makes a pill shape with fully round ends.
  • box-shadow draws a shadow outside the border (or inside with inset). The syntax is offset-x offset-y blur spread color. It takes no space and does not push other elements. You can list several shadows separated by commas.
  • outline is drawn outside the border and takes no space. In current browsers it follows the rounded corners of border-radius. It is the property used for focus rings, and outline-offset moves it away from the box.

Because shadows and outlines do not affect layout, they are good for hover and focus effects. Changing border-width on hover makes the content jump, so use a shadow or outline instead.

The first box is a circle and the pill has round ends. The bordered box is 92px wide, because the 6px border adds 12px to the width of 80px. The outlined box stays 80px wide, since the outline takes no room. The inset shadow draws a teal ring inside the box without changing its size.

For a focus ring, prefer outline with outline-offset, and never remove it without giving something equally visible back.

html
<style>
  body { margin: 0; padding: 20px; font-family: sans-serif; }
  .box { width: 80px; height: 80px; background: #cde; margin-bottom: 16px; }
  .circle { border-radius: 50%; }
  .pill { width: 160px; height: 40px; border-radius: 999px; background: #0a6cff; color: #fff; text-align: center; line-height: 40px; }
  .card { box-shadow: 0 2px 6px rgba(0, 0, 0, 0.25), 0 0 0 1px #ccc; }
  .bordered { border: 6px solid #333; }
  .outlined { outline: 6px solid #333; outline-offset: 2px; }
  .inset { box-shadow: inset 0 0 0 4px teal; }
</style>
<div class="box circle" id="circle"></div>
<div class="pill" id="pill">Enrol now</div>
<div class="box card" id="card"></div>
<div class="box bordered" id="bordered"></div>
<div class="box outlined" id="outlined"></div>
<div class="box inset" id="inset"></div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • border takes space; box-shadow and outline do not.
  • border-radius: 50% on a square gives a circle; a large value gives a pill.
  • box-shadow syntax: offset-x offset-y blur spread color, with optional inset.
  • outline is for focus rings and can be moved with outline-offset.
  • Prefer shadow or outline for hover effects so layout does not jump.

Common mistakes

  • Increasing border-width on hover and making the content jump. Use outline or box-shadow for effects.
  • Removing the outline from buttons and links without a replacement focus style.
  • Using border-radius: 50% on a rectangle and getting an oval instead of a pill. Use a large pixel value for a pill.

Follow-ups

  • What do the four numbers in box-shadow mean?
  • How would you draw a focus ring that does not move the layout?

Tips

  • Remember the one-line rule: border takes space, shadow and outline do not.
  • Explain a card shadow in two layers, a soft blur and a thin ring, to show practical use.