Achieving a floating alert box overlay on a responsive website for all window resolutions: useful tips and tricks!

(Please refer to the image below for a visual representation)

I have developed a responsive website using Bootstrap 4. I am looking to create an alert box that will be displayed at the bottom of the page and overlay on top of all elements, regardless of the screen size.

This alert box should appear when each box is clicked (as shown in the image with gray areas labeled "Some text") and then fade out after 2 seconds.

I need guidance on how to ensure the alert box overlays correctly regardless of the window size.

I have also included some relevant code snippets for reference.

Thank you in advance.

Image https://i.sstatic.net/y9d6a.jpg

HTML

 <div class="container-fluid">
        <div class="row">
            <div class="card-group">

               <div class="col-sm-6 col-md-5 col-lg-4 col-xl-3">
                    <div class="card">
                        <div class="card-body">
                            <h5>.....Some Text.....</h5>
                        </div>
                    </div>
                </div>

                <div class="col-sm-6 col-md-5 col-lg-4 col-xl-3">
                    <div class="card">
                        <div class="card-body">
                            <h5>.....Some Text.....</h5>
                        </div>
                    </div>
                </div>

                (More div blocks here)
            </div>
        </div>
    </div>

CSS

 

    .card{
    width: 14rem; 
    height: 8rem; 
    background-color: #e2e2e2;
    transition: 0.3s;
    color: #333333;
    margin-top: 0.5rem;
    margin-bottom: 0.5rem;
}
.card:hover{ 
    background-color: rgba(71, 172, 255,1);
    cursor: pointer;
}
.card-body{
  display: flex;
  align-items: center;
  text-align: center;
  justify-content: center
}

Answer №1

Take a look at the code snippet below:

If you're looking to customize your own alert design, you can follow a similar approach as shown in the snippet.

To ensure the alert box appears consistently in the same position every time, refer to the CSS for .alert-fixed and replace alert alert-primary with .alert-fixed in the code snippet.

Feel free to give it a try!

.card{
    width: 14rem; 
    height: 8rem; 
    background-color: #e2e2e2;
    transition: 0.3s;
    color: #333333;
    margin-top: 0.5rem;
    margin-bottom: 0.5rem;
   
}
.card:hover{ 
    background-color: rgba(71, 172, 255,1);
    cursor: pointer;
}
.card-body{
  display: flex;
  align-items: center;
  text-align: center;
  justify-content: center
}

.alert-fixed
{
position: absolute;
    right: 0;
    bottom: 0;
    left: 0;
    width: 100%;
    padding-left: 50px;
    
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<div class="container-fluid">
        <div class="row">
            <div class="card-group">

               <div class="col-sm-6 col-md-5 col-lg-4 col-xl-3">
                    <div class="card">
                        <div class="card-body">
                            <h5>.....Some Text.....</h5>
                        </div>
                    </div>
                </div>

             
                 <div class="col-sm-6 col-md-5 col-lg-4 col-xl-3">
                    <div class="card">
                        <div class="card-body">
                            <h5>.....Some Text.....</h5>
                        </div>
                    </div>
                </div>
      </div>
        </div>
 
                
        <div class="alert alert-primary" role="alert">
  This is a primary alert—check it out!
</div>
                
                
            </div>

Answer №2

Consider this addition for improvement

.alert-box{
    position: fixed;
    bottom: 10px;
    width: 80vw;
}

position: fixed; ensures fixed positioning, bottom: 10px adjusts the alertbox position 10px from the bottom, and width: 80vw; adapts its width based on the viewport size.

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

utilizing jquery to trigger events based on scrolling

I am working with a HTML class that contains various elements. I want to add a new class to the first element when it reaches the top of those elements, and then continue adding the class to the subsequent elements as the page is scrolled. I've attemp ...

Absence of Icon in PrimeFaces RowEditor

Having difficulty with the p:rowEditor feature in PrimeFaces as the icon is not displaying properly. The style class was not modified and the default icon is being used. Works perfectly in FF, Chrome, and IE9. Unfortunately, project needs to run on IE8 w ...

Display webpage content in an Iframe using Javascript after PHP code has been executed

Despite researching keywords like PHP // Javascript // Load // URL online, I'm still struggling to fully grasp the concepts. Real-life case studies have been helpful, but I must admit that I'm feeling a bit overwhelmed at the moment. I created a ...

What is the reason behind the constraints of min/max width and height values preventing the element from fully filling its parent div?

HTML Code: <div class="content"> <div class="card"> </div> </div> CSS Code: .content { min-height: 350px; min-width: 320px; max-width: 350px; padding: 15px; } .card { width: 100%; height: 100%; ...

Prevent the shifting of website elements when zooming in or out

As a beginner in HTML/CSS, I am trying to ensure that my content stays in place when zooming in/out or resizing the window. Initially, everything was centered, but after adding position:absolute to the outermost div, the alignment shifted slightly to the l ...

Reveal a hidden div sliding in from the left within a parent div that contains multiple divs with the same class

I have come across a situation where I have multiple divs containing several hidden divs. The goal is to reveal each hidden div when hovering over a specific link. The problem I am facing is that the current code shows and hides all hidden divs at once. W ...

Place the text on top of the textarea

Is it possible to position a div/text in the top right corner of a textarea using HTML and CSS? HTML <div id="textarea-container"> <textarea></textarea> <div id="copy">copy</div> </div> CSS ...

How can I resolve the issue of the mouse x/y glitch while drawing on an HTML canvas using JavaScript/jQuery?

I've been searching all over for a solution to this issue, but I just can't seem to find out what's causing the problem... This is my first attempt at working on something like this in my spare time. I hope to apply what I learn to create a ...

Based on the action taken, send specific data through AJAX - whether a form submission or a div click

There is a function called search, which can be triggered either by clicking on a div or submitting a form. When the div is clicked, the id of the div is sent as data in an AJAX call. However, if the form is submitted, I want to send the inputted data thr ...

Simple HTML files on a local server are having trouble loading images and JavaScript, yet the CSS is loading

Having worked in web design for quite some time, I recently began working on a basic Angular app that is meant to be very simple. For this project, I am only using the angular files and a single html file as the foundation. As I started setting up the bas ...

What changes can be made to the HTML structure to ensure that two form tags function separately?

Hey there! I'm currently tackling a web project that involves incorporating two form tags on one page, each with its own distinct purpose. Nevertheless, it appears that the inner form tag isn't behaving as it should. My suspicion is that this iss ...

The md-autocomplete feature showcases search results in a list format rather than a dropdown menu

Currently, I am working on implementing the Angular Material md-autocomplete tag to provide suggestions for zip codes in a form. To achieve this, I have set up an asynchronous service that fetches the suggestions and populates the results. My reference poi ...

JavaScript: Is there a way to toggle the visibility of an element on click?

I am currently working on writing a code that will allow me to make any part of the screen clickable. When a user clicks on the screen, a message saying "click!" should be displayed. So far, this is the code I have: HTML: <html> <head> ...

Invisibility Problem

As a newcomer to web design, this is only the second website I have ever created. However, I am facing an issue with my Nav bar that I cannot seem to resolve. I want my "Other Pages" section to be visible when the screen size is below 600px and hidden when ...

Using CSS to align the second line of a label directly beneath the first line through indentation

I need to adjust the second line of the label to ensure it doesn't overlap with the starting point of the first line. Below is my HTML code: <label for="ui-multiselect-edit-1" title="" class="ui-corner-all ui-state-hover"> <inpu ...

When padding is added on an MVC 5 page, the Bootstrap 4.1.1 class option for rounded-circle becomes distorted

Incorporating VS 2017 and bootstrap 4.1.1 into an MVC 5 page, I am facing a challenge in adding right-side padding to an image without distorting the circular shape of the image, as shown below. When I apply the padding, the circle becomes warped, but remo ...

The legend in the fieldset is not displaying correctly due to overflow issues

Within my fieldset, there is a legend structured as shown below: <fieldset class="fieldsetStyle"> <legend class="fieldsetLegendStyle"> <div> <h:outputText value="#{msgs.LABEL_AJOUTER_U ...

How can I ensure that my SVG Image perfectly fits within the Parent wrapper div?

I've been grappling with the following code snippet. I'm struggling to make my SVG image fit within my container div. .img { margin: 0; padding: 0; overflow: hidden; width: 500px; position: relative; } img { top:0; ...

Jquery solution for a fixed footer on a window

I come across a solution (which includes a helpful screen rewrite fix in the comments by Luco) that fits my needs perfectly. Here is the revised code that has been fixed and tested: // Utilizing window load event to account for image-dependent window heig ...

Steps for closing a Bootstrap 4 Pop-up using the X button alongside a carousel

I need help with closing a Bootstrap 4 pop-up only when the X icon is clicked. If I remove the 'data-backdrop="static"' code, the pop-up closes whenever I click outside the modal, but this is not ideal as it would also close when trying to intera ...