Tips for creating a custom notification container using CSS

I have been tasked with creating a notification display for a web application. The notifications should appear in the bottom right corner of the window and disappear after a certain period. Alternatively, users can click on a notification to dismiss it. When multiple notifications are present, they should be stacked on top of each other, with new notifications pushing the stack upwards. If the stack grows taller than the window height, older notifications from the top must move to a new stack displayed to the left of the newer one. This pattern continues if subsequent stacks also reach the window height limit.

Visually, the layout should resemble this: https://i.sstatic.net/93ugL.png

I have managed to arrange the notifications correctly, but I am struggling to position the notification container flush against the right edge of the window.

function notify(msg) {
  let element = document.createElement('div');
  element.classList.add('notification');
  element.innerHTML = msg;
  let timeout;
  element.onclick = () => element.remove();
  element.onmouseenter = () => clearTimeout(timeout);
  element.onmouseleave = () => createTimeout();
  createTimeout();
  let recentElement = container.firstElementChild;
  if(recentElement) recentElement.before(element);
  else container.appendChild(element);
  indicator.innerHTML='Last msg is '+msg;
  function createTimeout() {
    timeout = setTimeout(() => { 
      element.remove() 
    }, 10000);
  }
}

let notifyIndex = 0;

x1.onclick = () => {
  notify(++notifyIndex);
}
x10.onclick = () => {
  for (let i = 0; i < 10; i++)
    notify(++notifyIndex);
};
x30.onclick = () => {
  for (let i = 0; i < 30; i++)
    notify(++notifyIndex);
};
body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  place-items: center;
}
.action-list button {
  width: 4rem;
  margin .5rem
}
#container {
  position: fixed;
  display: flex;
  flex-direction: column-reverse;
  justify-content: flex-end;
  flex-wrap: wrap-reverse;
  align-content: flex-end;
  align-items: flex-end;
  bottom: 0;
  right: 30px;
  max-height: 100vh;
}
.notification {
  transition: all 500ms ease-in-out;
  box-shadow: 0 0 1rem #0004;
  margin: 0 1rem 1rem 0;
  background: #f9f99f;
  color: #000;
  border: 1px solid #0008;
  padding: .2rem 1rem;
  cursor: default;
  user-select: none;
}
<div id="container"></div>

<div>
<div id="indicator">Last msg is 0</div>
<div class="action-list">
  <button id="x1">x1</button>
  <button id="x10">x10</button>
  <button id="x30">x30</button>
</div>
</div>

I have added a 30-pixel indentation to demonstrate that the notifications extend beyond the window's edge. The aim is to prevent recent notification stacks from exceeding the window boundary.

Where am I going wrong?

Answer №1

position: fixed;
display: flex;
flex-direction: column-reverse;
justify-content: flex-end;
flex-wrap: wrap-reverse;
align-items: flex-end;
bottom: 0;
right: 30px;
max-height: 100vh;

Give this code a try and let me know if it solves the issue!

In short: The problem was caused by the align-content property set to flex-end

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is gulp.dest set to match the current file?

I'm having trouble directing my compiled .css file to the same directory as its original .scss version. gulp.task('sass', function() { return gulp.src([ appDir + '/*.scss', appDir + '/**/*. ...

Changing icons in a collapsed menu in Bootstrap 4

I have been working with Bootstrap 4 alpha 6 version and I am facing an issue with multiple collapse blocks on my page. I want to change the icon when a user opens or closes a block, so I tried using classes but it didn't work. Can you help me figure ...

How to transfer data from local storage to PHP using Ajax

Is there a way to pass values from JavaScript to PHP that are stored in local storage? JavaScript let cartItems = localStorage.getItem("productsInCart"); var jsonString = JSON.stringify(cartItems); $.ajax({ url:"read.php", method: "post", da ...

Crafting a Presentation of Inspirational Words

I'm currently in the process of creating a photo album using AngularJS and CSS, but I'm facing an issue with the pictures showing up too large. Despite my efforts to resize them, they still appear oversized. I've even tried resizing them in ...

React Js month picker problem encountered

I am currently working on implementing a month picker using the library called react-month-picker The code is functioning correctly in React: https://codesandbox.io/s/react-month-picker-forked-84rqr However, when I tried to integrate the same code into a ...

Why isn't the card positioned in the center of the screen?

Why is the card not centered on the screen as it is supposed to be? I am utilizing Bootstrap 4.5 <div class="container"> <div class="row"> <div class"col"></div> <div class="col-8"> <div class="card"> ...

Ways to fix npm error requiring a suitable loader

I was working on a Vue.js project with vue-chartjs and encountered an issue. I attempted to reinstall the library, but the error persisted: error in ./node_modules/chart.js/dist/chart.esm.js Module parse failed: Unexpected token (6613:12) You may need an ...

Transfer a session ID cookie generated by express-session to a React front end on a different domain

I am currently developing a web application. The back end of my app is built using Node.js and Express.js, with a specific focus on utilizing the express-session module for implementing session-based authentication to maintain user logins. I understand tha ...

The footer is anchored at the bottom on one page but mysteriously relocates to the center on the next page

I've been working on creating a footer for my Angular application and here's the code for it: // HTML <footer class="footer"> // code for footer </footer> // LESS .footer { position: absolute; bottom: 0; width: 100% ...

Retrieve the binary file data that was sent via Postman using Node.js/Express.js

I am currently testing file uploading in my backend system. I am using Postman to send binary data as a file in the request body, and now I need to extract this data from the POST request. req.body The above code snippet returns a binary buffer that look ...

Utilizing API data sharing across AngularJS controllers

I am facing a challenge with my parent controller and its children controllers, as I want them all to share the data fetched from an Api service. Controllers: var app = angular.module('mymodule',[]); app.controller('main', ['$scop ...

What are the steps to successfully install OpenCV (javascript edition) on Internet Explorer 11?

I'm currently experiencing issues with getting the OpenCV javascript version to function properly on IE11 for contour detection. While my code runs smoothly on all other up-to-date browsers, I am encountering errors such as: TypeError: Object doesn&a ...

Verifying the name's uniqueness and disregarding it in case of any alterations

I am currently working on a form that includes a modal to allow users to modify their name and email address. The issue I am facing is that the form sends a request to the server to check for unique values in all cases. While this is appropriate when creat ...

Steps to Change the Background Color to White in a Dropdown menu

Hello everyone! I'm currently using this codepen example on my website. My query is regarding the fifth panel - is it possible to change the color of the drop-down box when clicking on it? Here's a snippet of the HTML: <link href='https: ...

Tips for making the text input smaller in a datepicker

https://i.sstatic.net/fuECu.png Is there a way to reduce the width of a datepicker while using Bootstrap? It's taking up too much space in my browser window. Below is the code snippet I am using for this functionality. <!---DatePicker ----> ...

Nesting jQuery appends

Can you nest appends in jQuery? I attempted the following: var div = $('#ui-container'); var div2 = div.append( $('<div/>').addClass('second') ); var div3 = div2.append( $('<div/>').addClass('third ...

In my Node.js/Express.js application, I have a directory that holds various images. Whenever I attempt to view these images via their URL, I encounter a 404 error message

In my Node.js/Express js project, I have the following folder structure: root ----bin ----controllers ----middleware ----models ----node_modules ----public --------images ------------test.png ----routes ----views I am currently trying to determine the cor ...

Parsing JSON data repeatedly using JavaScript within an HTML environment

The following code I found on a popular web development website works perfectly: <!DOCTYPE html> <html> <body> <h1>Customers</h1> <div id="id01"></div> <script> var xmlhttp = new XMLHttpRequest(); var url ...

Display a dynamic list of data fetched from an axios Get request in a React MUI dropdown menu

import { useEffect, useState } from "react"; import Box from "@mui/material/Box"; import FormControl from "@mui/material/FormControl"; import InputLabel from "@mui/material/InputLabel"; import MenuItem from "@m ...

Check if there are any child nodes before executing the RemoveChild JavaScript function to avoid any errors

function delete(){ let k = document.getElementsByClassName('row'); for(let i=0; i<k.length; i++) { if(k[i].hasChildNodes()){ k[i].removeChild(k[i].childNodes[2]); } } } <div id="table"> <div class="row"& ...