Pen Settings

HTML

CSS

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

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

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.

HTML

              
                <body>
  <!-- No need to add anything to your HTML -->
  <div id="main">
    <section class="container-1">
      <h1>Notifs</h1>
      <h2>Clean, yet beautiful notifications in your website</h2>

      <button onclick="createNotification()">Show notification</button>
    </section>

    <input id="input-content" type="text" value="Your notification text" placeholder="Your notification text">

    <section class="container-2">
      <label for="select-theme">Theme</label>
      <div class="select-style">
        <select id="select-theme" name="select-theme">
          <option value="0">Light</option>
          <option value="1" selected>Dark</option>
        </select>
      </div>

      <label for="select-content-type">Content type</label>
      <div class="select-style">
        <select id="select-content-type" name="select-content-type">
          <option value="0" selected>Default</option>
          <option value="1">Success</option>
          <option value="2">Error</option>
          <option value="3">Warning</option>
          <option value="4">Info</option>
        </select>
      </div>

      <label for="select-position">Position</label>
      <div class="select-style">
        <select id="select-position" name="select-position">
          <option value="0" selected>Whole screen</option>
          <option value="1">Right side</option>
          <option value="2">Bottom side</option>
          <option value="3">Bottom right corner</option>
        </select>
      </div>

      <label for="select-regime">Regime</label>
      <div class="select-style">
        <select id="select-regime" name="select-regime">
          <option value="0" selected>Just one</option>
          <option value="1">Dominant</option>
          <option value="2">Side by side</option>
          <option value="3">Clear</option>
        </select>
      </div>

      <label for="select-lifespan">Lifespan</label>
      <div class="select-style">
        <select id="select-lifespan" name="select-lifespan">
          <option value="0">One second</option>
          <option value="1" selected>Two seconds</option>
          <option value="2">Three seconds</option>
          <option value="3">Four seconds</option>
          <option value="4">Five seconds</option>
          <option value="5">Till close</option>
        </select>
      </div>
    </section>
  </div>
</body>
              
            
!

CSS

              
                /* notifs styles */

:root {
  --notifs-background-color: rgba(255, 255, 255, 0.9);
  --notifs-content-color: #000000;
  --notifs-content-size: 1.5em;
  --notifs-content-opaque-color: rgba(177, 177, 177, 0.5);

  --notifs-content-type-success-color: #2eb74d;
  --notifs-content-type-error-color: #c43b48;
  --notifs-content-type-warning-color: #dba810;
  --notifs-content-type-info-color: #38a2b3;
}

.notifs-close-button {
  font-size: xxx-large;
  cursor: pointer;
}

.notifs-canvas {
  display: flex;
  right: 0;
  bottom: 0;
  background-color: var(--notifs-background-color);
  position: fixed;
  overflow-x: hidden;
  z-index: 9999;
}

.notifs-canvas-hidden {
  width: 0;
  height: 0;
  transition: height 0s linear 0.5s, width 0.5s cubic-bezier(0.645, 0.045, 0.355, 1);
}

.notifs-canvas-whole-screen {
  justify-content: center;
  align-items: center;
  width: 100vw;
  height: 100vh;
  transition: width 0.5s cubic-bezier(0.645, 0.045, 0.355, 1);
}

.notifs-canvas-whole-screen .notifs-content {
  max-width: 100vw;
  max-height: 100vh;
}

.notifs-canvas-right-side {
  justify-content: center;
  align-items: center;
  width: 20em;
  height: 100vh;
  transition: width 0.5s cubic-bezier(0.645, 0.045, 0.355, 1);
}

.notifs-canvas-right-side .notifs-content {
  max-width: 20em;
  max-height: 100vh;
}

.notifs-canvas-bottom-side {
    justify-content: center;
    align-items: center;
    width: 100vw;
    height: 10em;
    transition: width 0.5s cubic-bezier(0.645, 0.045, 0.355, 1);
}

.notifs-canvas-bottom-side .notifs-content {
    max-width: 100vw;
    max-height: 10em;
}

.notifs-canvas-bottom-right-corner {
  justify-content: center;
  align-items: center;
  width: 20em;
  height: 10em;
  transition: width 0.5s cubic-bezier(0.645, 0.045, 0.355, 1);
}

.notifs-canvas-bottom-right-corner .notifs-content {
  max-width: 20em;
  max-height: 10em;
}

.notifs-content {
  display: flex;
  flex-direction: column;
  align-content: space-between;
  margin: 5%;
  padding: unset;
  font-size: var(--notifs-content-size);
  color: var(--notifs-content-color);
}

.notifs-content-type-1 {
  color: var(--notifs-content-type-success-color);
}

.notifs-content-type-2 {
  color: var(--notifs-content-type-error-color);
}

.notifs-content-type-3 {
  color: var(--notifs-content-type-warning-color);
}

.notifs-content-type-4 {
  color: var(--notifs-content-type-info-color);
}

.notifs-content-hidden {
  display: none;
}

.notifs-content li {
  list-style: none;
  text-align: center;
}

.notifs-content li:not(:last-child) {
  padding-bottom: 3%;
}

.notifs-hidden {
  display: none;
}

.notifs-opaque {
  color: var(--notifs-content-opaque-color);
}
/* end of notifs styles*/

/*-----------------------------------------------------*/

/* showcase page styles */

body {
  margin: 0;
  padding: 0;
  font-family: Montserrat Light, Arial, Sans Serif;
  color: #343434;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(to bottom right, #fff, #bfbfbf);
  min-height: 100vh;
}

h1 {
  font-size: 3em;
  font-weight: normal;
  margin: 1em 0 0.5em 0;
}

h2 {
  font-size: 1.2em;
  font-weight: normal;
}

.container-1, .container-2 {
  margin-left: 2.5em;
  margin-right: 2.5em;
  display: flex;
  flex-direction: column;
  align-content: space-between;
}

.container-2 {
  align-items: center;
}

.container-2 *:not(label) {
  margin-top: 0.5em;
  margin-bottom: 0.5em;
}

.container-2:last-child {
  margin-bottom: 4em;
}

button, input, select {
  width: 100%;
  height: 1.5em;
  font-family: Montserrat Light, Arial, Sans Serif;
  font-size: 1.3em;
  color: #343434;
  border: none;
}

button {
  height: 3em;
  margin-top: 3em;
  background-color: #fff;
  cursor: pointer;
  border-radius: 50px;
  -moz-box-shadow: 0 5px 5px rgba(0, 0, 0, 0.1);
  -webkit-box-shadow: 0 5px 5px rgba(0, 0, 0, 0.1);
  box-shadow: 0 5px 5px rgba(0, 0, 0, 0.1);
  transition: all .2s linear;
}

input {
  width: 99%;
  background: none;
  border-bottom: #878787 solid 2px;
  text-align: center;
  font-size: 1.7em;
  margin-top: 3.5em;
  margin-bottom: 3em;
}

label {
  font-size: 1.3em; 
  margin-top: 1.5em;
  margin-bottom: 1em;
}

/*select*/

.select-style {
  padding: 0;
  margin: 0;
  width: 100%;
  border-radius: 10px;
  overflow: hidden;
  background-color: #fff;
  -moz-box-shadow: 0 5px 5px rgba(0, 0, 0, 0.1);
  -webkit-box-shadow: 0 5px 5px rgba(0, 0, 0, 0.1);
  box-shadow: 0 5px 5px rgba(0, 0, 0, 0.1);
}

.select-style select {
  height: 2em;
  text-indent: 0.5em;
  width: 100%;
  border: none;
  box-shadow: none;
  background-color: transparent;
  background-image: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
}

.select-style select:focus {
  outline: none;
}

/*end of select*/

button:hover {
  transform: scale(1.02, 1.02);
}

button:active {
  transform: scale(1, 1);
}
              
            
!

JS

              
                /** Enums representing notification's options. */
/** Light and dark theme */
enum NTheme { Light, Dark, }
/** Notification content type - differs in content color */
enum NContentType { Default, Success, Error, Warning, Info, }
/** Position of notification (with BottomRightCorner consider setting Regime to JustOne or Clear) */
enum NPosition { WholeScreen, RightSide, BottomSide, BottomRightCorner, }
/** Regime - JustOne (shows only last notification), Dominant (shows all notifications, but the last one is more dominant), SideBySide (shows all notifications), Clear (shown only last and clears history) */
enum NRegime { JustOne, Dominant, SideBySide, Clear, }
/** Lifespan of notification, when TillClosed set, close button will appear */
enum NLifespan { OneSecond, TwoSeconds, ThreeSeconds, FourSeconds, FiveSeconds, TillClosed, }

/*-----------------------------------------------------*/

/**
 * Necessary to call to make notifs work
 */
function initNotifs(): void {
    let olNotifications = document.createElement('ol');
    olNotifications.classList.add('notifs-content', 'notifs-content-hidden');
    let divNotifications = document.createElement('div');
    divNotifications.classList.add('notifs-canvas', 'notifs-canvas-hidden');
    divNotifications.appendChild(olNotifications);
    //append necessary notifs element to body
    document.getElementsByTagName('body')[0].appendChild(divNotifications);
}
initNotifs(); //calling to init notifs

/*-----------------------------------------------------*/

/**
 * Call this function with desired parameters to build notification.
 * 
 * @param {string} text Content text of notification.
 * @param {NTheme} theme Theme of notification.
 * @param {NContentType} contentType Differentiates content text.
 * @param {NPosition} position Whole screen / Right side / Bottom right corner.
 * @param {NRegime} regime Option to show just the last one or the other notifications too.
 * @param {NLifespan} lifespan Lifespan of notification, could disappear after few seconds or stay there till user dismisses it.
 */
function buildNotification(
 text: string,
 theme: NTheme = NTheme.Dark,
 contentType: NContentType = NContentType.Default,
 position: NPosition = NPosition.BottomRightCorner,
 regime: NRegime = NRegime.JustOne,
 lifespan: NLifespan = NLifespan.OneSecond): void {

  //remove close button
  removeCloseButton();

  //theme
  document.documentElement.style.setProperty('--notifs-background-color', theme === NTheme.Dark ? 'rgba(0, 0, 0, 0.8)' : 'rgba(255, 255, 255, 0.9)');
  document.documentElement.style.setProperty('--notifs-content-color', theme === NTheme.Dark ? '#ffffff' : '#000000');

  //position
  getCanvas().classList.remove('notifs-canvas-hidden');
  getContent().classList.remove('notifs-content-hidden');

  switch (position) {
    case NPosition.WholeScreen:
      getCanvas().classList.add('notifs-canvas-whole-screen');
      break;
    case NPosition.RightSide:
      getCanvas().classList.add('notifs-canvas-right-side');
      break;
    case NPosition.BottomSide:
      getCanvas().classList.add('notifs-canvas-bottom-side');
      break;
    default:
      getCanvas().classList.add('notifs-canvas-bottom-right-corner');
      break;
  }

  //regime
  //recommended regime for bottom right corner notification is JustOne or Clear
  switch (regime) {
    case NRegime.Clear:
      clearActives();
      break;
    case NRegime.JustOne:
      hideAll();
      break;
    case NRegime.Dominant:
      makeAllDefault();
      makeAllOpaque();
      break;
    default:
      makeAllDefault();
      break;
  }

  //content and content type
  //it takes roughly about 100 characters to correctly fit bottom right corner notification
  addNotification(text, contentType);

  //lifespan
  if (lifespan !== NLifespan.TillClosed) {
    let timeout = 0;
    switch (lifespan) {
      case NLifespan.TwoSeconds:
        timeout = 2000;
        break;
      case NLifespan.ThreeSeconds:
        timeout = 3000;
        break;
      case NLifespan.FourSeconds:
        timeout = 4000;
        break;
      case NLifespan.FiveSeconds:
        timeout = 5000;
        break;
      default:
        timeout = 1000;
        break;
    }
    setTimeout(() => {
      hide();
    }, timeout);
  } else {
    addCloseButton();
  }
}

/*-----------------------------------------------------*/

function getCanvas(): HTMLElement {
  return document.getElementsByClassName('notifs-canvas')[0] as HTMLElement;
}

function getCloseButton(): HTMLElement {
  return document.getElementsByClassName('notifs-cross')[0] as HTMLElement;
}

function getContent(): HTMLOListElement {
  return document.getElementsByClassName('notifs-content')[0] as HTMLOListElement;
}

function getActiveCount(): number {
  return document.getElementById('notifs-content').childElementCount;
}

/**
 * Adds notification to html document. Called from buildNotification.
 */
function addNotification(text: string, contentType: NContentType = NContentType.Default): void {
  let newNotification = document.createElement('li');
  newNotification.innerHTML = text;
  if (contentType !== NContentType.Default) {
    newNotification.classList.add(`notifs-content-type-${contentType}`);
  }
  getContent().appendChild(newNotification);
}

function addCloseButton(): void {
  let closeButton = document.createElement('li');
  closeButton.innerHTML = '&times;';
  closeButton.id = 'notifs-close-button';
  closeButton.classList.add('notifs-close-button');
  closeButton.addEventListener('click', hide);
  getContent().appendChild(closeButton);
}

function removeCloseButton(): void {
  if (getContent().lastChild !== null && ((getContent().lastChild as HTMLElement).id === 'notifs-close-button')) {
    getContent().removeChild(getContent().lastChild);
  }
}

function hide(): void {
  getCanvas().classList.remove('notifs-canvas-whole-screen', 'notifs-canvas-right-side', 'notifs-canvas-bottom-side', 'notifs-canvas-bottom-right-corner');
  getCanvas().classList.add('notifs-canvas-hidden');
  getCanvas().removeEventListener('click', hide);

  getContent().classList.add('notifs-content-hidden');
}

function clearActives(): void {
  while (getContent().hasChildNodes()) {
    getContent().removeChild(getContent().lastChild);
  }
}

function makeAllDefault(): void {
  for (let i = 0; i < getContent().children.length; i++) {
    getContent().children[i].classList.remove('notifs-hidden', 'notifs-opaque');
  }
}

function hideAll(): void {
  for (let i = 0; i < getContent().children.length; i++) {
    getContent().children[i].classList.add('notifs-hidden');
  }
}

function makeAllOpaque(): void {
  for (let i = 0; i < getContent().children.length; i++) {
    getContent().children[i].classList.add('notifs-opaque');
  }
}

/*-----------------------------------------------------*/

/**
 * This function is called from showcase site. For your project, call buildNotification above to create notifs.
 */
function createNotification() {
  const content = (document.getElementById('input-content') as HTMLInputElement).value;
  const theme = Number((document.getElementById('select-theme') as HTMLSelectElement).value);
  const contentType = Number((document.getElementById('select-content-type') as HTMLSelectElement).value);
  const position = Number((document.getElementById('select-position') as HTMLSelectElement).value);
  const regime = Number((document.getElementById('select-regime') as HTMLSelectElement).value);
  const lifespan = Number((document.getElementById('select-lifespan') as HTMLSelectElement).value);

  buildNotification(content, theme, contentType, position, regime, lifespan);
}
              
            
!
999px

Console