Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource


Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource


Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.


Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.


                <!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Name Initial Avatar</title>
    <link rel="stylesheet" href="./style.css" />
    <div class="inputs">
      <form action="">
        <label for="firstname">Firstname:</label>
        <input type="text" id="firstname" />
        <label for="lastname">Lastname:</label>
        <input type="text" id="lastname" />
        <button type="submit" id="generateAvatar">Generate Image</button>
    <div class="outputs">
      <img id="img" />
    <script src="./app.js"></script>


                * {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: Arial, Helvetica, sans-serif;
body {
  display: flex;
  height: 100vh;
  width: 100%;
  align-items: center;
  justify-content: center;

input[type="text"] {
  padding: 0.5rem;
  border-radius: 0.3rem;
  border: none;
  margin: 10px 0;
  display: block;
  background: #e9e9e9;
img {
  border: 2px solid #f4f4f4;
  border-radius: 50%;
.outputs {
  padding: 0.5rem 1rem;
#generateAvatar {
  padding: 0.5rem;
  width: 100%;
  border-radius: 0.5rem;
  border: none;
  background-color: #7070ff;
  color: #f4f4f4;
@media screen and (max-width: 560px) {
  body {
    flex-direction: column-reverse;


                // selecting components
let firstname, lastname, generate, avatar, ctx, color;
firstname = document.getElementById("firstname");
lastname = document.getElementById("lastname");
generate = document.querySelector("form");

//creating canvas
avatar = document.createElement("canvas");
avatar.width = avatar.height = "200";
ctx = avatar.getContext("2d");
ctx.font = `${avatar.width / 2}px Arial`;
ctx.textAlign = "center";

//generating color
color = [

//function to get name initials
function getInitials() {
  if (lastname.value == "") {
    let Initials = firstname.value[0].toUpperCase();
    return Initials;
  } else {
    let Initials = (firstname.value[0] + lastname.value[0]).toUpperCase();
    return Initials;

//function to create avatar
function createAvatar(Initials) {
  let random = Math.floor(Math.random() * color.length);
  //clear canvas
  ctx.fillStyle = "#ffffff";
  ctx.fillRect(0, 0, avatar.width, avatar.height);

  //add background
  ctx.fillStyle = `${color[random]}60`;
  ctx.fillRect(0, 0, avatar.width, avatar.height);

  //add text
  ctx.fillStyle = color[random];
  ctx.fillText(Initials, avatar.width / 2, (65 / 100) * avatar.height);

  //generate as Image
  let dataURL = avatar.toDataURL();
  document.getElementById("img").src = dataURL;
//Event Listener
generate.addEventListener("submit", (e) => {

// Preloaded avatar for example