Notification badges annoy me most of the times by popping up every now and then and I am pretty sure most of you experience similar thing, but anyways, let's see how we can create a notification badge using CSS.
Step 1: HTML
The element with a class 'base' will act as a profile image or an icon upon which we will position the notification indicator element having a class 'indicator'.
Step 2: CSS
First of all, we have to set up the height and width of the main 'base' element. Then we set the border-radius property to 50%. Border radius rounds the edges of the border by a specified amount. In our case the height and width of the element are equal and so, when we apply border radius of 50%, a square looking element will transform to a circle.
After applying a background color, the base element will look like a circle.
Now, set the position of the base element to 'relative' which means that it will be positioned relative to its current position. This will not change anything but we want this property to position the child elements, more on that in a second.
You can also add an image instead of a background color to the base element, just like this.
Now, let's design the indicator.
First of all, set the position of the indicator as 'absolute' which means that it will be positioned inside the ancestor element which has its position as 'relative'.
Then, we will define the final location of the indicator by setting the values of top, bottom, right and left properties of the indicator.
The 'bottom' property will offset the 'indicator' element by 60% of the height of the 'base' element from the bottom of the 'base' element.
Similarly, the 'left' property will offset the 'indicator' element by 60% of the width of the 'base' element from the left of the 'base' element.
Next, we will add a border having the color same as the 'body' element having a border-radius of 50%.
Then, we will style the notification counter.
In order to center the notification count number, we can add 'flex' properties to its parent element.
The final output will be:
Top comments (20)
You can also rely on mask to have transparency, related: stackoverflow.com/a/51377952/8620333
You ok buddy...you are recommending SVG! 😜😂
not really, the question was initially using SVG so I gave the proper solution with SVG and then I added the CSS mask solution to show that it's also easy with CSS ;)
Phew, for a second there I was going to call someone to come and check on you! 😜
lol
olo
CSS is better option for me
Can be easily done it with Yogurt CSS.
Sure, but I think this is the easy way!
Great!
This is great! But I tried to make it as simple as possible.
Hi! Great article! Here is some advices for you
Good job!
Sure, thanks. I will definitely take that into consideration.
Good, but what do you think about improving this solution with accessibility supporting. See example w3.org/WAI/WCAG21/Techniques/aria/....
thank you.
Thanks a lot!
Improved!
Show
Very cool!
I love this
That's great!