Create Image Avatars with text based fallbacks.

Usage no npm install needed!

<script type="module">
  import agneyReactAvatar from 'https://cdn.skypack.dev/@agney/react-avatar';


React Avatar

npm License: MIT code style: prettier

All Contributors

Create Image Avatars with text based fallbacks.

Demo Storybook

Features ✨

  1. Ability to render image avatar or text avatar as circle or square.
  2. Ability to render text based fallbacks for images for the time they take to load or error.
  3. Renders text based avatars and backgrounds based on text passed in.
  4. Autoscales text if it doesn't fit the container width.
  5. Automatically determines readable text color depending on background.
  6. Exposes a hook to use/generate gravatar for a user.
  7. Exposes a context so you can configure avatar components across your application.


npm install @agney/react-avatar
# OR
yarn add @agney/react-avatar

Requires React v16.8 or above and Styled Components v4 or above.


import React from "react";
import { Avatar } from "@agney/react-avatar";

const App = () => {
  return (

export default App;


Description Default Value
src Source of image to be displayed
text Text to be displayed as fallback
imageAlt Alt tag for image '' - decorative
shape shape of avatar. options: 'circle' 'square'
htmlWidth width of image element and text fallback. 100%
htmlHeight height of image element and text fallback same as htmlWidth
backgrounds Array of background colors ['#3c40c6','#ffa801','#485460','#0be881','#f53b57']
bgColor background color for specific text fallback. picks random from backgrounds, this changes according to text prop passed in.
textColor text color for specific text fallback readable dark or light color depending on background.
textProcessor function to process visible text inside the avatar. Original text will be used for hashing so even ones with same initials inside avatar can have different backgrounds. (text) => text
className className on the wrapper. can be used for wrapping with CSS-in-JS frameworks

See Storybook for more examples and code snippets.


Package exposes a context in the form of AvatarContext.

import { Avatar, AvatarContext } from '../.';

function App() {
  const contextValue = React.useMemo(() => ({
    backgrounds: ['#000000', '#DD2C00', '#6200EA', '#3F51B5'], // Any props used by Avatar can be used here.
  }), []);
  return (
      <div style={{ display: 'flex' }}>

Values on the Context Provider are overridden by any props that are on the individual component. Context Provider is not compulsory for usage of Avatar component. Read more about Context API on docs.

useGravatar hook

Gravatars or Globally Recognized Avatars is a free service that allows you to share profile pictures/avatars across different sites and services. react-avatar exposes a hook that makes it easier for you to request and use these gravatars in your application.


import React from "react";
import { Avatar, useGravatar } from "@agney/react-avatar";

const App = () => {
  const url = useGravatar('person@email.com');
  return (

export default App;


Argument Description Default
email Email of the person for which gravatar is to be fetched
config configuration object. following fields are part of this object
config.size number representing height & width of image. 1px up to 2048px 80
config.defaultImage If the image is not available, gravatar defaults to this property. You can provide a custom URL image to default to or one of the alternatives that Gravatar supports.
config.forceDefault Always returns the default image if true false
config.rating Gravatar allows users to self rate their images. If required pass in a higher rating. g

For a complete list of available options, view Gravatar docs

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page.


We use yarn v1 for development.

yarn start

# To run example
cd example
yarn start

# Running storybook
yarn run storybook

Run tests

yarn test

Show your support

Give a ⭐️ if this project helped you!

Twitter: agneymenon

📝 License

Copyright © 2020 Agney Menon agney@outlook.in.
This project is MIT licensed.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Agney Menon

📖 💻 🤔

This project follows the all-contributors specification. Contributions of any kind welcome!