94

I need to show the popular red notification indicator with count such as the one shown below. Getting something that looks nice cross browser seems tricky. For example, different browsers seem to treat paddings differently, resulting in weird looking notifications.

What is the best cross-browser way of ensuring the notifications show up nicely? Not adverse to using javascript, but pure css is of course preferable

enter image description here

Inigo
  • 12,186
  • 5
  • 41
  • 70
meow
  • 27,476
  • 33
  • 116
  • 177
  • 7
    Facebook uses CSS3 - if the user doesn't have a browser that supports it, there are other rules so that it degrades. For instance, IE users wouldn't get rounded corners. – Chris Baker Apr 21 '11 at 17:55
  • really? that is great information. – meow Apr 21 '11 at 18:07
  • 1
    I came looking for an answer but found no comprehensive one. All 3 answers given when this was asked 11 years ago provide essentially the same solution: absolute positioning within a relatively positioned parent. This is correct. But none of the existing answers, despite 11 years of CSS and browser upgrades, provides a solution that is [***responsive*** while also looking good, with elegant CSS that works in almost all situations](https://stackoverflow.com/a/71440299/8910547). All the blog posts I found weren't much better. So I figured it out and offer a new answer for a new decade. – Inigo Mar 11 '22 at 19:28

6 Answers6

181

The best way to achieve this is by using absolute positioning:

/* Create the blue navigation bar */
.navbar {
  background-color: #3b5998;
  font-size: 22px;
  padding: 5px 10px;
}

/* Define what each icon button should look like */
.button {
  color: white;
  display: inline-block; /* Inline elements with width and height. TL;DR they make the icon buttons stack from left-to-right instead of top-to-bottom */
  position: relative; /* All 'absolute'ly positioned elements are relative to this one */
  padding: 2px 5px; /* Add some padding so it looks nice */
}

/* Make the badge float in the top right corner of the button */
.button__badge {
  background-color: #fa3e3e;
  border-radius: 2px;
  color: white;
 
  padding: 1px 3px;
  font-size: 10px;
  
  position: absolute; /* Position the badge within the relatively positioned button */
  top: 0;
  right: 0;
}
<!-- Font Awesome is a great free icon font. -->
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet"/>

<div class="navbar">
  <div class="button">
    <i class="fa fa-globe"></i>
    <span class="button__badge">2</span>
  </div>
  <div class="button">
    <i class="fa fa-comments"></i>
    <span class="button__badge">4</span>
  </div>
</div>
Phill Healey
  • 3,084
  • 2
  • 33
  • 67
Titus
  • 4,487
  • 6
  • 32
  • 42
  • It works even I used in multiple times in a page by just replacing the id of the container. – Web_Developer Mar 03 '16 at 13:25
  • The absolute positioning part of this answer is right. But there is also getting **size, shape and look** right, and doing it in an elegant and **[responsive](https://web.dev/responsive-web-design-basics/)** way. The badge should look like it's floating above and partly outside. This answer puts it inside the element, but luckily the icons are small and round so it looks ok IN THIS CASE. You have to do a lot of tweaking of `px` values to fit other cases, and even then it isn't *responsive*. I wrote [a new answer](https://stackoverflow.com/a/71440299/8910547) with a live demo to compare. – Inigo Mar 11 '22 at 19:44