Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

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

Packages

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.

Behavior

Save Automatically?

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.

HTML

              
                <div>
  <button class="add">Add +</button>
</div>
              
            
!

CSS

              
                :root {
  --notification-size: 350px;
}

.notification {
  position: fixed;
  display: flex;
  width: var(--notification-size);
  right: var(--space-4);
  border: var(--border);
  background-color: var(--color-white);
  border-radius: var(--radius);
  box-shadow: var(--shadow-large);
  transition: var(--transition);
  animation: slide-in var(--transition-speed) var(--transition-curve);
}
.content {
  padding: var(--space-4);
}
.title {
  font-size: var(--text-md);
  margin-bottom: var(--space-1);
  font-weight: bold;
  color: var(--color-gray-800);
}
.description {
  color: var(--color-gray-600);
}
.close {
  font-size: var(--text-sm);
  margin-left: auto;
  padding: 0 var(--space-4);
  border: 0;
  border-left: var(--border);
  background-color: transparent;
  border-radius: 0 var(--radius) var(--radius) 0;
  color: var(--color-gray-400);
  cursor: pointer;
}
.close:hover {
  background-color: var(--color-gray-100);
}
.close:active {
  color: var(--color-gray-700);
}
.close:focus,
.close:active {
  outline: none;
  box-shadow: var(--shadow-focus);
}
.add {
  padding: var(--space-3) var(--space-5);
  border: 0;
  font-weight: bold;
  background-color: var(--color-blue-500);
  color: var(--color-white);
  border-radius: var(--round);
  cursor: pointer;
  outline: none;
}
.add:hover {
  background-color: var(--color-blue-400);
}
.add:focus {
  box-shadow: var(--shadow-focus);
}
.add:active {
  background-color: var(--color-blue-500);
}
.animate-out {
  animation: fade-out var(--transition-speed) var(--transition-curve);
}
@keyframes fade-out {
  to {
    opacity: 0;
    transform: translateX(var(--notification-size));
  }
}
@keyframes slide-in {
  from {
    transform: translateX(var(--notification-size));
  }
  to {
    transform: translateX(0);
  }
}

              
            
!

JS

              
                const notificationTexts = [
  "Hey Jussi! If you're recording your screen, I just wanted to tell that...",
  "Congratulations, you've found the meaning of life, which by the way is being present.",
  "You looked super today! Where's that smile from?",
  "COME HOME ALREADY! -MOM",
  "How are you doing? Dismiss this message to tell me that you've seen it.",
  "Dude, I've never slided so smoothly into anything before! Well, that sounded a bit weird to be honest.",
  "Did you know that LASER is an abbreviation for Light Amplification by the Stimulated Emission of Radiation?"
];

const addButton = document.querySelector(".add");
const notificationPosition = document.body.querySelector("div");
const margin = 16;

const addNotification = () => {
  // Create notification
  const notification = document.createElement("div");
  // Add class "notification"
  notification.classList.add("notification");
  // Pick random content for notification
  const randomMessage =
    notificationTexts[Math.floor(Math.random() * notificationTexts.length)];
  // Insert random content and close button
  notification.innerHTML = `
                    <div class="content">
            <h4 class="title">New message!</h4>
            <p class="description">${randomMessage}</p>
          </div>
          <button class="close" aria-label="Dismiss notification">Close</button>
        `;
  // Get close button within notification
  const closeButton = notification.querySelector(".close");
  // Listen for the button and attach "removeNotification" function to it
  closeButton.addEventListener("click", removeNotification);
  // Position notification
  notification.style.top = `${margin}px`;
  // Add notification on the page
  notificationPosition.prepend(notification);
  // Move other notifications down
  // 1. Get height of the newly added notification
  const currentHeight = notification.offsetHeight;
  // 2. Get the rest of the notifications and turn them into an array
  const restNotifications = Array.from(
    document.querySelectorAll(".notification")
  ).slice(1);
  // 3. Add the currently added notification's height to the rest of the notifications
  restNotifications.forEach((item) => {
    item.style.top = `${parseInt(item.style.top) + currentHeight + margin}px`;
  });
};

const removeNotification = (event) => {
  // Get clicked close button
  const closeButton = event.currentTarget;
  // Get the notification
  const notification = closeButton.parentNode;
  // Get the height of the clicked notification
  const currentHeight = notification.offsetHeight;
  // Define rest of the notifications
  let restNotifications = [];
  let next = notification.nextElementSibling;
  // Loop always the next notification until none is found
  while (next) {
    // If the next element doesn't have 'notification' class, break the while loop
    if (!next.matches(".notification")) {
      break;
    }
    // Add the notification to the array
    restNotifications.push(next);
    // Set the next to be the next element
    next = next.nextElementSibling;
  }
  // Se the new height for each of the notifications below the removed one
  restNotifications.forEach((item) => {
    item.style.top = `${parseInt(item.style.top) - currentHeight - margin}px`;
  });
  // Animate removed notification
  notification.classList.add("animate-out");
  // Remove notification once animation has ended
  notification.addEventListener("animationend", () => {
    notification.parentNode.removeChild(notification);
  });
};

addButton.addEventListener("click", addNotification);
addNotification();

              
            
!
999px

Console