DEV Community

Rajat Gupta
Rajat Gupta

Posted on

4 2

How to make a badge component in CSS (using transform: translate())

Since you are here, probably you already know what a badge is, but anyway let me explain it first.
0.PNG
Badges are used to highlight an item's status for quick recognition. There are many use cases of the badge:

  1. you must have seen that when we have something unread on WhatsApp, Twitter, Quora, etc. a badge icon with a number appears on the top right of the app to let us know how many messages are unread.
  2. or to show how many items are there in our cart of an e-commerce website.

There are many ways using which we can make a badge. Here we'll see how easy it is to make the badge component using the translate function of the transform property. If you don't know what it is, read my blog here.

so, let's get to the code because as the saying goes "talk is cheap, show me the code".

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        p{
            background-color: red;
            height: 22px;
            width: 22px;
            display: flex;    /* to center the content inside red circle */
            border-radius: 99rem;
            justify-content: center;
            align-content: center;
            transform: translate(-100%, -90%);    /* only thing we need */
        }
    </style>
</head>

<body>
    <img src="notification.svg">
    <p>2</p>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

1.PNG
2.PNG
3.PNG

You can see ☝️ how seamlessly we moved the red circle around the icons using the transform property.

That's all folks.

I write one article every day related to web development (yes, every single f*cking day). Follow me here if you are learning the same.

my Twitter handle: @therajatg

If you are the Linkedin type, let's connect: https://www.linkedin.com/in/therajatg/

Have an awesome day ahead 😀!

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay