Achieving Vertically Centered Alerts with Responsive Functionality in Bootstrap 5

I have implemented an alert that looks like this:


    <div id="success-alert" style="display: none; color: darkgreen; text-align: center; top: 50%; z-index: 9999" class="alert alert-success alert-autohide" role="alert">
    Some Message
    </div>
   

Initially, the alert is centered on the screen. However, when scrolling, it stays in its original position and goes out of view.

I would like the alert to remain centered on the screen even after scrolling. How can I achieve this?

Thank you

Answer №1

To achieve the desired outcome, utilizing the CSS property position: fixed is key. This rule informs the browser that the specified element will remain fixed in its position even as the page is scrolled.

By combining this with additional CSS rules, such as right, left, and margin, we can center the element horizontally (note that specifying a width is essential for this to work effectively).

Moreover, by incorporating the top and transform rules, the element can be centered vertically.

Here is a demonstration showcasing how these rules can be applied:

#success-alert {
  position: fixed;
  /** Ensures element stays fixed in place even during page scrolling */
  
  width: 50%;
  /** Specifies a width to prevent the element from occupying the entire viewport (alternatively, apply "col-*" classes for responsiveness) */

  right: 0;
  left: 0;
  margin: auto;
  /** 
  * The aforementioned rules horizontally center the element.
  * Both "right" and "left" values must be "0" for proper centering through the "margin" rule;
  */
  
  top: 50%;
  transform: translate3d(0, -50%, 0);
  /** The above rules centrally position the element vertically */
  
  text-align: center;
  z-index: 9999;
}

/** Visual representation for the demo */
body {
  height: 300vh; /** Simulates a lengthy page */
  background-image: linear-gradient(to bottom, red, blue)!important;
}

/** "!important" is used to override SO's background rule on the body element within snippets */
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ae8e5e5fef9fef8ebfacabfa4baa4b8">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

<!-- Demo purposes only -->
<h5 class="text-center text-white">Try scrolling to observe the alert remaining fixed in position</h3>

<div id="success-alert" class="alert alert-success alert-autohide shadow" role="alert">
 Lorem ipsum Lorem ipsum sit oolor amet sit oolor amet. Lorem ipsum sit ool Lorem ipsum sit oolor amet rem ipsum sit oolor amet. 
</div>

While the above solution is not the sole method to tackle this specific issue, it serves as a solid starting point to guide you in the right direction.

For further insights, explore the position rule on MDN.

Enhance your understanding of the transform rule through MDN.

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

Transitioning the height of a Vue component when switching routes

I've been struggling to implement a transition slide effect on my hero section. The height of the hero is set to 100vh on the homepage and half of that on other pages. Despite trying various methods, I haven't been able to get it working properly ...

Text starting to overflow in HTML line before being hidden by right-aligned content

Text is confined to a fixed-width container with hidden overflow. Three status icons may appear on the right side of the container, and I want to prevent text from flowing under these icons without using JavaScript. How can I achieve this with CSS? Below ...

Offspring containers fail to adjust in size relative to their parent container

I am working with a parent div that contains 5 child divs. The parent div does not fill the entire width of the window, but the 5 children share the same space and properly fill their parent. <div class="parent"> <div class="child ...

What are the methods for setting the width and height of an Angular mat dialog box?

I'm currently working with a Mat dialog box in Angular and I'm trying to include styles for width and height within that object. However, I'm encountering errors and can't seem to resolve them. Is there another method for setting width ...

Using antd icons in CSS pseudo-elements effectively involves leveraging the ::before or ::after

Currently, I have an icon displaying like this: import { Icon } from "antd"; ... // within a react component: <div className="someclass"> <Icon type="thunderbolt" /> </div> What I really aim to achieve i ...

What are some effective ways to align an image to the left and text to the right within a table

I need to display a member with a higher rank, showing the user image along with their username. However, I am struggling to align the image and text properly due to varying username lengths causing them to appear in a zig-zag position. Here is what I hav ...

Tips on saving a form submit button data to localStorage

As a newcomer, I am on a quest to make this function properly. My goal is to create a form where the submit button saves data to the localStorage. Here's what I have tried thus far. <script> function storeRecipe() { localStorage.setItem($(" ...

Switch the orientation of the unordered list from horizontal to vertical in a dynamic way

Perhaps a repeated inquiry, yet I have been unable to discover a solution for this issue... I am dealing with a div element (referred to as master) that I do not have the ability to adjust in terms of width or height. Contained within the master is anoth ...

Ensure the table row remains on one page when printing or viewing the print preview

I'm currently dealing with an html report that contains a table, which can be seen here http://jsfiddle.net/fhwoo3rg/2/embedded/result/ The issue arises when trying to print the report, as it breaks in the middle of a table row like this: https://i ...

What is the best way to update a CSS href using window.open with JavaScript?

I am trying to dynamically change the CSS href by using window.open in JavaScript. <a class="cssButton" id="Launch" target="_blank" data-baseref="Example/index.html?" href="Example/index.html?" >Launch Example</a> I want to transform it into: ...

What makes styling the iconic Browse button (file input) such a challenge?

Many people are curious about how to style a file input in an HTML form, as evidenced by the plethora of well-known techniques available. However, I am more interested in understanding why we encounter such technical limitations that seem so trivial and fr ...

Concealing scroll bars while still maintaining the ability to scroll by using overflow:scroll

Similar Question: How to hide the scrollbar while still allowing scrolling with mouse and keyboard I have created a sidebar for a web application that needs to enable users to scroll through it without displaying a scrollbar. The content is 500px high ...

When I include a class in a checkbox label, the CSS ceases to function properly

Apologies for my lack of understanding, but I am facing an issue with applying an attribute to a label element with class "c". It seems to work fine with other objects, like buttons, but not with labels. I cannot figure out why such a simple thing is not w ...

In Internet Explorer, HTML elements may not always align seamlessly with other elements on the page

What is causing the alignment issue with the Available Times table, Assessor Notes textbox, and Booking Notes textbox in Internet Explorer, while they appear perfectly aligned in Firefox? Here is my CSS: <style type="text/css" media="screen"> h ...

Moving the webpage into the realm of mobile and tablet devices

Over the next few weeks, I will be working on transitioning my website content to have both a mobile and tablet version. During this process, I have some questions: What is the best way to redirect users to the correct site based on their device/browse ...

Element misbehaving after a certain point

I'm encountering an issue with a piece of code that is not working as expected. The problem seems to be specifically with the after element. The desired behavior is for two lines to draw from the top left corner and the bottom right corner when hoveri ...

Ways to create a smooth transition in element height without a known fixed target height

Is it possible to create a smooth height transition for an element when the target height is unknown? Replacing height: unset with height: 100px allows the animation to work, but this presents a problem as the height needs to be based on content and may v ...

Arranging the list items in a perfectly straight vertical line

Concern: I'm struggling with alignment issues on my website. I have country flags displayed using display:inline;. The issue arises when the number next to the flag exceeds single digits (e.g., 10 or more) as it affects the alignment due to the consi ...

Align a button within an input field to be in line with a stacked label in

I'm currently facing some issues with Ionic because it's not placing my button where I want it to be: https://i.stack.imgur.com/qtUQJ.png My goal is to have the button on the same line as the input, similar to the clear-button. This is the cod ...

Experience Markdown's Single Input Update Feature

I have recently developed a Markdown editor using Vue, similar to the examples found on Vue's website. However, my query is more related to implementation rather than Vue itself. Therefore, I am open to suggestions that do not necessarily involve Vue. ...