Something like that. Obviously you would want to change the specifics and probably use background images. The point is to emphasis the absolute positioning which it really consistent across browsers, at least in my experiences.
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>
I am sure that the answers about absolute positioning are correct. For the sake of experiment, I tried to make an SVG-only solution. The result is far from ideal, maybe someone knows a more elegant solution to a similar svg puzzle? :)
A responsive solution using font-relative sizing and shaping
The above image is a snapshot of running the Live Demo below. Scroll down and try it yourself. Scroll further down to see a demo of the currently accepted answer.
As other answers say, absolute positioning with a relatively positioned parent is a very reliable way to position the indicator in the upper right corner (or any corner you prefer).
But there are other very important considerations:
Size: not just relative to what it tags, but also what happens if the number gets long?
Shape: ideally round, but what happens if the number gets long?
Floating appearance: It should appearing to float above the corner, rather than be inside.
Border: optionally, depending on the colors involved, you may need a subtle border or shadow.
Accessibility: proper tagging of the notification so that it is accessible to the sight impaired.
Doing the above in px units as the accepted answer does is very messy, brittle, and not at all responsive.
The following CSS address all of the above issues in an elegant responsive way:
.notification {
/* circle shape, size and position */
position: absolute;
right: -0.7em;
top: -0.7em;
min-width: 1.6em; /* or width, explained below. */
height: 1.6em;
border-radius: 0.8em; /* or 50%, explained below. */
border: 0.05em solid white;
background-color: red;
/* number size and position */
display: flex;
justify-content: center;
align-items: center;
font-size: 0.8em;
color: white;
}
Key things:
The element with the .notification class will display in the upper-right corner of the element that contains it.
For example it will display in the upper right corner of
this mailbox button:
If its position looks wrong, debug by using your browser's
dev tools to inspect the true boundaries of the parent element.
If you want the indicator to be a circle no matter how big the count, change min-width to width and set border-radius to 50%. The CSS above is designed to stretch horizontally if the count gets large, as shown below (and like so many people's inboxes 🤣).
For accessibility, you should include role="status" on the notification element, as shown above.
Live Demo
Here is a live demo, along with a slider so that you can see how it responds to font size changes:
/* IGNORE this Javascript part.
It's just here to let you change the size with
a slider. Press Run and try it. */
var boxes = document.getElementsByClassName("mailbox");
function updateFontSize(size) {
for (var i = 0; i < boxes.length; i++) {
boxes[i].style.fontSize = size + "px";
}
}
var px = getComputedStyle(boxes[0]).fontSize
var label = document.getElementById("label");
label.innerHTML = px
var slider = document.getElementById('slider');
slider.value = px.slice(0, -2)
slider.onchange = () => {
label.innerHTML = slider.value + "px";
updateFontSize(slider.value);
}
.mailbox {
position: relative;
font-size: 16px;
line-height: 2.5em;
margin: 0.3em;
}
.notification {
/* circle shape, size and position */
position: absolute;
right: -0.7em;
top: -0.7em;
min-width: 1.6em; /* or width, explained below. */
height: 1.6em;
border-radius: 0.8em; /* or 50%, explained below. */
border: 0.05em solid white;
background-color: red;
/* number size and position */
display: flex;
justify-content: center;
align-items: center;
font-size: 0.8em;
color: white;
}