The Box ModelExtra· 25 min read

Outline & Opacity

A border that does not affect layout, and how to make whole elements see-through.

What you will learn

  • Use outline and how it differs from border
  • Control opacity
  • Respect focus outlines for accessibility

Outline vs border

An outline looks like a border but sits outside the box and does not take up space (it never shifts your layout). Browsers use it to show keyboard focus.

Outline sits outside and reserves no space
<style>
  .a { border: 3px solid #4338ca; padding: 10px; margin-bottom: 10px; }
  .b { outline: 3px solid #ef4444; padding: 10px; }
</style>
<div class="a">border (affects layout)</div>
<div class="b">outline (does not affect layout)</div>
Live preview

Both boxes get a 3px coloured line, but they behave differently. .a uses border, which is part of the box — it takes up space and can push neighbours around. .b uses outline, which is drawn on top, outside the box and reserves no space, so adding or removing it never shifts the layout. That is exactly why browsers use an outline (not a border) to show which element has keyboard focus — it can appear and disappear without the page jumping.

Watch out: Do not remove focus outlines (outline: none) without adding your own visible focus style — keyboard users rely on them to see where they are. This is an accessibility must.

Opacity

opacity goes from 0 (invisible) to 1 (solid) and affects the whole element, including its content.

opacity fades the whole element
<style>
  .box { background:#4338ca; color:#fff; padding:14px; margin-bottom:8px; }
  .o7 { opacity: 0.7; } .o4 { opacity: 0.4; }
</style>
<div class="box">opacity: 1 (default)</div>
<div class="box o7">opacity: 0.7</div>
<div class="box o4">opacity: 0.4</div>
Live preview

The three boxes step down in opacity: 1 (default, fully solid), 0.7 (slightly faded), and 0.4 (clearly see-through). Notice the text fades tooopacity affects the whole element and everything inside it, not just the background. If you only want a transparent background but solid text, use rgba() on the background colour instead (from the colours lesson).

Q. How does outline differ from border?

Answer: outline is drawn outside the box and does not affect layout, so it never shifts surrounding elements. Browsers use it for focus.

✍️ Practice

  1. Add a coloured outline to a button and confirm the layout does not shift.
  2. Fade an image to 50% opacity.

🏠 Homework

  1. Add a clear, custom focus style to your form inputs (an outline on :focus).
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →