← Back to HTML & CSS, page 7

html-css-0137 · Easy fresher

How do you put a small notification badge on the corner of an avatar or an icon?

Make the wrapper position: relative and the badge position: absolute at top: 0 and right: 0. Add transform: translate(50%, -50%) to centre it on the corner, and give it hidden text that a screen reader can read.

Full answer

This is a standard use of absolute positioning inside a relative parent.

  1. The wrapper gets position: relative. It becomes the reference box for the badge. The wrapper should fit the avatar, so make it display: inline-block, or a flex or grid item that does not stretch.
  2. The badge gets position: absolute with top: 0 and right: 0. That puts its top-right corner at the corner of the wrapper.
  3. To centre the badge on the corner instead, move it by half of its own size with transform: translate(50%, -50%). Percentages in translate are percentages of the element itself, so this works for any badge size, even when the number inside gets wider.

Other points:

  • The badge needs min-width and a rounded shape, with padding that fits the text, and a border in the page colour so it stands out from the avatar.
  • Add accessible text. A number alone, such as "3", has no meaning for a screen reader. Add visually hidden text, such as "3 new messages", or give the control a label that includes the count. Hide the visible number with aria-hidden if the hidden text already says it.
  • If the wrapper is a button, the badge belongs inside the button so it is part of the same control.
  • Do not use float or manual margins to place a badge. They break when the text changes.

The red badge sits with its centre on the top-right corner of the avatar image. When the number grows to two digits, the badge grows to the left and right by the same amount, because the translate uses half of its own width.

Check with a longer number such as 99+ and with a larger browser font size.

html
<style>
  .avatar { position: relative; display: inline-block; margin: 20px; }
  .avatar img { display: block; width: 48px; height: 48px; border-radius: 50%; background: #ccc; }
  .badge { position: absolute; top: 0; right: 0; transform: translate(50%, -50%);
    min-width: 20px; height: 20px; padding: 0 4px; box-sizing: border-box;
    border-radius: 10px; background: #c00; color: #fff; font: 12px/20px sans-serif; text-align: center; }
</style>
<span class="avatar" id="a">
  <img src="/img/user.png" alt="Asha">
  <span class="badge" id="b"><span aria-hidden="true">3</span><span style="position:absolute;left:-9999px">3 new messages</span></span>
</span>

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

  • The wrapper needs position: relative and a size that fits the avatar.
  • The badge uses position: absolute with top and right.
  • translate(50%, -50%) centres it on the corner at any size.
  • Add a border so it stands out.
  • Add accessible text for the count.

Common mistakes

  • Forgetting position: relative on the wrapper, so the badge goes to the page corner.
  • Using a fixed left margin to place the badge, which breaks when the number grows.
  • Showing only a number with no text for screen readers.

Follow-ups

  • Why do percentages in translate use the element's own size?
  • How would you make the count readable for a screen reader?

Tips

  • Describe the pair: relative parent, absolute child.
  • Explain why translate(50%, -50%) centres the badge on the corner.