MyInternships.in

CSS Tutorial

CSS Outline

An outline is a line drawn around an element, just outside its border. Unlike a border, an outline does not take up space, and it plays an important role in accessibility.


What is an outline?

The outline is a line drawn outside an element's border edge. It looks similar to a border, but with two key differences: it does not affect the element's size or layout, and it can be drawn a small distance away from the element using outline-offset.

Outline properties

Like borders, outlines have a style, width and color, plus the outline shorthand to set them together.

Outline properties
div {
  outline-style: solid;
  outline-width: 3px;
  outline-color: #2b5cff;
}

/* shorthand */
div {
  outline: 3px solid #2b5cff;
}

Outline vs border

Because an outline does not add to an element's size, adding one will not shift other elements around, whereas a border can. Outlines also cannot have rounded corners in the same way borders do.

OutlineBorder
Takes up spaceNoYes
Affects layoutNoYes
Per-side controlNoYes
Rounded cornersLimitedYes (border-radius)
Offset supportYes (outline-offset)No

See outline and offset live

Border vs outline with offset
Example
<!DOCTYPE html>
<html>
<head>
<style>
  div {
    background-color: #dbe7ff;
    padding: 16px;
    margin: 24px;
    font-family: Arial, sans-serif;
  }
  .bordered {
    border: 4px solid tomato;
  }
  .outlined {
    outline: 4px solid green;
    outline-offset: 6px;   /* pushes the outline outward */
  }
</style>
</head>
<body>
  <div class="bordered">I have a red border.</div>
  <div class="outlined">I have a green outline, offset by 6px.</div>
</body>
</html>

Outlines and accessibility

When a user tabs through a page with the keyboard, the browser draws a focus outline around the active link or button. This shows keyboard users where they are. Removing it makes a site harder to use.

⚠️

Never remove focus outlines with outline: none unless you replace them with another clear focus style. Doing so breaks keyboard navigation and hurts accessibility.

💡

Use outline-offset to create a small gap between an element and its outline, which is great for a clean focus ring around buttons.

Key points

  • An outline is drawn just outside the border.
  • It does not take up space or shift layout.
  • Set it with outline (width, style, color).
  • outline-offset moves the outline away from the element.
  • Focus outlines are vital for keyboard accessibility, do not remove them.

Related CSS Topics

Keep learning with these closely related tutorials.

Ready to use your CSS skills?

Find web development internships and fresher jobs across India.

Browse Web Dev Internships