On this page10 sections
Lecture 008: Outline
| Category | 2: The Box Model |
| Lecture | 008 of 057 |
| Estimated video length | 8 min |
| Target | CSS3 · any modern browser |
| Level | Beginner |
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-widthandoutline-color - Use the
outlineshorthand andoutline-offset - Tell outlines and borders apart
- Style focus rings without hurting accessibility
1. Outline Styles
Example
<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
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
<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
| Border | Outline | |
|---|---|---|
| Part of the box model | Yes | No |
| Takes up space / moves content | Yes | No |
| Different style per side | Yes | No (all four sides) |
Follows border-radius | Yes | Yes (in modern browsers) |
Example: hover over each box:
<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:
<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
- Where is an outline drawn compared with the border?
- Does an outline change the size of the element?
- Write a 2px dotted red outline.
- Which property puts space between the outline and the border?
- Why shouldn't you remove focus outlines?
Practice Answers
- Outside the border.
- No. It takes up no space.
outline: 2px dotted red;outline-offset- 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, plusoutline-offsetfor a gap.- They're used for focus rings; style them with
:focus-visible, never remove them.