DEV Community

Designyff
Designyff

Posted on • Originally published at designyff.com

4

Simple avatar with status indicator - CSS only - A step-by-step tutorial

Avatar with status indicator

Tutorial on how to create a simple avatar with status indicator using only CSS.

HTML

For HTML, we need only one div element with "avatar" class and a span element inside with "status" element. This element will indicate status.

Default value will be offline, by adding the "active" class to it, the status indicator will become green.

For now, we'll add an "active" class to it.



<div class="avatar">
    <span class="status active"></span>
</div>


Enter fullscreen mode Exit fullscreen mode

CSS

For CSS, first we'll style the avatar.

We'll set it's width and height to 40x40 px.

A little rounded border of 3px width, solid dark blue.

We'll set it's position to relative.

And, of course, the image. We'll set the background image to url image, and it's size to cover the whole 40x40 area, with position set to center.



.avatar {
    width: 40px;
    height: 40px;
    border: 3px rgb(48, 69, 96) solid;
    border-radius: 6px;
    position: relative;
    background-image: url('https://www.rd.com/wp-content/uploads/2017/09/01-shutterstock_476340928-Irina-Bg.jpg');
    background-size: cover;
    background-position: center;
}


Enter fullscreen mode Exit fullscreen mode

Our status indicator will be 6x6 pixels size with a dark border with radius set to 50%, which will form a circle.

We'll set it's position to absolute, and with top and right set to 0 with transform translate set to 40% and -40%, it will be positioned in the top right corner.

Lastly, we'll set it's background to dark grey. This is the look of offline status. The default one.



.status {
    width: 6px;
    height: 6px;
    border: 2px solid rgb(48, 69, 96);
    border-radius: 50%;
    position: absolute;
    right: 0px;
    top: 0px;
    transform: translate(40%, -40%);
    background-color: rgb(33, 34, 35);
}


Enter fullscreen mode Exit fullscreen mode

Now we'll style the "active" class.

This class we'll be appended to a status element.

We'll just override the background-color property, and set it to green.



.active {
    background-color: rgb(48, 249, 75);
}


Enter fullscreen mode Exit fullscreen mode

And that's it.

Simply remove the "active" class from the status element when offline and add it when online.

You can find video tutorial with full code here.

Thanks for reading. ❤️

Tiugo image

Modular, Fast, and Built for Developers

CKEditor 5 gives you full control over your editing experience. A modular architecture means you get high performance, fewer re-renders and a setup that scales with your needs.

Start now

Top comments (1)

Collapse
 
maame-codes profile image
Maame Afia Fordjour

Saving this for later.

Image of Stellar post

How a Hackathon Win Led to My Startup Getting Funded

In this episode, you'll see:

  • The hackathon wins that sparked the journey.
  • The moment José and Joseph decided to go all-in.
  • Building a working prototype on Stellar.
  • Using the PassKeys feature of Soroban.
  • Getting funded via the Stellar Community Fund.

Watch the video 🎥

👋 Kindness is contagious

Value this insightful article and join the thriving DEV Community. Developers of every skill level are encouraged to contribute and expand our collective knowledge.

A simple “thank you” can uplift someone’s spirits. Leave your appreciation in the comments!

On DEV, exchanging expertise lightens our path and reinforces our bonds. Enjoyed the read? A quick note of thanks to the author means a lot.

Okay