On this page10 sections
  1. Learning Objectives
  2. 1. Outline Styles
  3. 2. Width, Colour and the Shorthand
  4. 3. outline-offset
  5. 4. Outline vs Border
  6. 5. Focus Rings
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 008: Outline

Category2: The Box Model
Lecture008 of 057
Estimated video length8 min
TargetCSS3 · any modern browser
LevelBeginner

An outline is a line drawn outside an element's border. It looks like a border, but it takes up no space and never moves anything. Browsers use it to show keyboard focus, which makes it one of the most important lines on a page.


Learning Objectives

  • Draw outlines with outline-style, outline-width and outline-color
  • Use the outline shorthand and outline-offset
  • Tell outlines and borders apart
  • Style focus rings without hurting accessibility

1. Outline Styles

Example

html
<style>
  p { padding: 8px; margin: 18px 10px; border: 1px solid #ced4da; }
</style>

<p style="outline: 3px solid #4c6ef5;">solid</p>
<p style="outline: 3px dashed #4c6ef5;">dashed</p>
<p style="outline: 3px dotted #4c6ef5;">dotted</p>
<p style="outline: 6px double #4c6ef5;">double</p>
<p style="outline: 6px groove #4c6ef5;">groove</p>
<p style="outline: 6px ridge #4c6ef5;">ridge</p>

Each paragraph has a thin grey border, and the blue outline sits outside it.


2. Width, Colour and the Shorthand

css
p {
  outline-style: solid;    /* required, as with borders */
  outline-width: 4px;      /* or thin, medium, thick */
  outline-color: crimson;
}

/* the same in one line */
p { outline: 4px solid crimson; }

3. outline-offset

Moves the outline away from the border, leaving a transparent gap.

Example

html
<p style="border: 2px solid #2f9e44; outline: 2px dashed #e8590c; outline-offset: 8px; padding: 10px; margin: 20px;">
  The orange outline is 8px away from the green border.
</p>

4. Outline vs Border

BorderOutline
Part of the box modelYesNo
Takes up space / moves contentYesNo
Different style per sideYesNo (all four sides)
Follows border-radiusYesYes (in modern browsers)

Example: hover over each box:

html
<style>
  .box { display: inline-block; padding: 10px; margin: 10px; background: #e7f5ff; }
  .b:hover { border: 6px solid #1c7ed6; }     /* pushes things around */
  .o:hover { outline: 6px solid #1c7ed6; }    /* nothing moves */
</style>

<span class="box b">Border on hover</span><span class="box">Neighbour</span><br>
<span class="box o">Outline on hover</span><span class="box">Neighbour</span>

5. Focus Rings

Browsers draw an outline around the element that has keyboard focus. Press Tab in the example:

html
<style>
  button, a { margin: 6px; }
  .custom:focus-visible { outline: 3px solid #f59f00; outline-offset: 3px; border-radius: 4px; }
</style>

<button type="button">Default focus ring</button>
<button type="button" class="custom">Custom focus ring</button>
<a href="#" class="custom">Custom link focus</a>

Never write outline: none on focus without a replacement. Keyboard users would lose track of where they are.


Common Beginner Mistakes

1. Forgetting outline-style. No style means no outline.

2. Removing focus outlines. *:focus { outline: none; } makes the site unusable by keyboard.

3. Expecting outlines to add space. They overlap neighbouring elements instead.


Practice Questions

  1. Where is an outline drawn compared with the border?
  2. Does an outline change the size of the element?
  3. Write a 2px dotted red outline.
  4. Which property puts space between the outline and the border?
  5. Why shouldn't you remove focus outlines?

Practice Answers

  1. Outside the border.
  2. No. It takes up no space.
  3. outline: 2px dotted red;
  4. outline-offset
  5. Keyboard users rely on them to see which element is focused.

Key Takeaways

  • Outlines sit outside the border and take up no space.
  • outline: width style colour, plus outline-offset for a gap.
  • They're used for focus rings; style them with :focus-visible, never remove them.