Tips for connecting a suspended div below a Bootstrap 5 fixed navigation bar

I successfully implemented a sticky navigation using Bootstrap 5, and now I want to add a hanging div (.chat-tag) below the sticky nav for a chat feature. This "hanging tag" should stay attached to the sticky nav as the user scrolls down the page.

It's displaying correctly at widths of 992 and below, but on desktop, it's appearing in the same row as the top navigation.

Here's the current implementation which I've also shared on Codepen: https://codepen.io/Codewalker/pen/vYRoXdB?editors=1100

nav {
  background-color: red;
  height: 48px;
  position: sticky;
  top: 0;
}

.main-nav {
  display: -webkit-flex;
  display: inline-flex;
  -webkit-justify-content: center;
}

.masthead-fluid {
  background-color: #F7CAC9;
}

.masthead {
  height: 60vh;
  display: inline-flex;
  align-items: center;
}

.chat-tag {
  background-color: #B565A7;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 50px;
  width: 200px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet" />
<header class="header sticky-top bg-white">
  <div class="container-fluid">
    <div class="nav-wrapper text-center">
      <nav class="main-nav navbar navbar-expand-lg bg-white">
        <div class="container-fluid text">
          <a class="navbar-brand" href="#">
            LOGO
          </a>
          <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
          </button>
          <div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav me-auto mb-2 mb-lg-0">
              <li class="nav-item">
                <a class="nav-link active" aria-current="page" href="#what">Home</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#registry">Page 1</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">Page 2</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">Page 3</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">Page 4</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">Page 5</a>
              </li>
            </ul>
          </div>
        </div>
        <div class="chat-tag">Chat with us</div>
      </nav>
    </div>
  </div>
</header>

<div class="container-fluid masthead-fluid mb-5">
  <div class="row">
    <div class="col-12">
      <div class="color-overlay d-flex align-items-center">
        <div class="container">
          <div class="row">
            <div class="col-12 masthead">
              <h1>Hello, world!</h1>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<div class="container">
  <div class="row">
    <div class="col-12">
      <p>Text goes here...</p>

      <p>More text goes here...</p>

      <p>And more text goes here...</p>

      <p>Final section of text...</p>
    </div>
  </div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.2.0/js/bootstrap.min.js"></script>

Answer №1

In a unique scenario like this, I would lean towards utilizing absolute positioning instead of grappling with flexbox.

.chat-tag {
  position: absolute; /* <------------- THIS */
  top: 100%; /* <------------------ AND THAT */
  background-color: rgba(181, 101, 167, 0.5); /* just for fun | #B565A7 */
  transition: all 0.3s;  /* just for fun */
  display: flex;
  align-items: center;
  justify-content: center;
  height: 50px;
  width: 200px;
}

.chat-tag:hover {
  background-color: rgba(181, 101, 167, 1); /* just for fun */
}

nav {
  background-color: red;
  height: 48px;
  position: sticky;
  top: 0;
}

.main-nav {
  display: inline-flex;
}

.masthead-fluid {
  background-color: #F7CAC9;
}

.masthead {
  height: 60vh;
  display: inline-flex;
  align-items: center;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet" />
<header class="header sticky-top bg-white">
  <div class="container-fluid">
    <div class="nav-wrapper text-center">
      <nav class="main-nav navbar navbar-expand-lg bg-white">
        <div class="container-fluid text">
          <a class="navbar-brand" href="#">
            LOGO
          </a>
          <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
          </button>
          <div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav me-auto mb-2 mb-lg-0">
              <li class="nav-item">
                <a class="nav-link active" aria-current="page" href="#what">Home</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#registry">Page 1</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">Page 2</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">Page 3</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">Page 4</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">Page 5</a>
              </li>
            </ul>
          </div>
        </div>
        <div class="chat-tag">Chat with us</div>
      </nav>
    </div>
  </div>
</header>

<div class="container-fluid masthead-fluid mb-5">
  <div class="row">
    <div class="col-12">
      <div class="color-overlay d-flex align-items-center">
        <div class="container">
          <div class="row">
            <div class="col-12 masthead">
              <h1>Hello, world!</h1>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<div class="container">
  <div class="row">
    <div class="col-12">
... (contents omitted for brevity)
    </div>
  </div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.2.0/js/bootstrap.min.js"></script>

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

Screen readers are unable to "read" text that is identified through aria-describedby

When enhancing my web application for accessibility, I encountered a challenge. I have implemented two required fields in a form that should display separate error messages as spans. To accomplish this, I am utilizing aria-invalid to signal when the fields ...

Using session variables on a different php page

Having trouble using a session variable in another page? I fetched data from a database and stored it in a variable, but when I attempt to use it on another page, I get an error saying 'undefined index'. See the code below - can someone help me s ...

I have implemented a button in PHP and now I am looking to invoke a function when that button is clicked

I have a PHP-generated HTML button and I'm having trouble calling the mybtn3() function when it is clicked. The button code looks like this: echo"<td width=14% align=center><input type=button value=Export onclick=mybtn3() /></td>"; ...

The color of the left arrow on the tooltip cannot be altered

Currently, I am using a tooltip that is positioned on the right side: <i class="fas fa-question-circle text-blue" data-toggle="tooltip" data-placement="right" title="hello" ></i> I have attempted to modify the color of the too ...

There was a type error that occurred because the property 'addEventListener' could not be read from an undefined value

Encountering an issue with the JavaScript libraries three.js and OrbitControls.js despite following a tutorial: However, an error is being displayed in the console: Uncaught TypeError: Cannot read property 'addEventListener' of undefined at ne ...

Utilizing Django fixtures: Importing HTML content as JSON into a Text Field

Currently in the process of transitioning my website to Django from another content management system, I am facing challenges with importing HTML into Django using fixtures. The specific issue revolves around "Events", which is a model within my Django app ...

How can I prevent my mouse click from being overridden by my mouse hover?

Currently, I am developing a Sudoku game using JavaScript and facing some challenges with mouse events. My goal is to enable users to hover over a square and have it change color, as well as click on a square to highlight the entire row, column, and quadra ...

Is it possible to execute ng-repeat on-click when ng-show evaluates to true?

I am currently facing an issue with my table that populates using ng-repeat. The table contains 100 rows, and clicking on any row reveals a new table (using ng-show) with more detailed product information. However, the performance of my web app is being af ...

Correct placement of elements using absolute positioning and optimized scroll functionality

I am currently working on implementing tooltips for items within a scrollable list. My goals for the tooltips are as follows: Ensure they are visible outside and not restricted by the scroll area Appear immediately after the corresponding item Be detach ...

Removing HTML tags and then reapplying HTML tags Part 1

Attempting to convert the following string: <p> string <b> bold <em>italic string</em> also(bold) </b> </p> to this modified string: <p> string </p> <!----------- ...

Easily done! Using JavaScript to generate a blinking cursor on a table

Working on a project to develop a dynamic version of the classic game Tic-Tac-Toe... However... Every table cell is displaying an irritating flashing cursor, almost like it's behaving as an input field. Any insights into why this is happening...? O ...

When reopening the modal in Bootstrap V5 with jQuery, the close event may not trigger as expected

When trying to implement special functionality in my modal close event using sweetalert2, I encountered an issue where the close event does not trigger again after closing the modal for the first time. Check out a live example here: https://codepen.io/th ...

Issue with second button in Angular Onclick() not been resolved

I'm experiencing an issue with the onClick() methods in my code. There are two onClick() methods present on the UI, but when I click on the second one, it does not call the method while the first one does. I am struggling to figure out why this is hap ...

Use jQuery to smoothly scroll a div

I created a container using a <div> element which is divided into 3 inner divs. The first two inner divs are meant to serve as previous and next buttons, with ids of prev and next respectively. At the bottom, there are 3 more inner divs, each with un ...

Ways to position a DIV on the right side of the screen using percentage margin to adjust its distance from the edge as the browser is resized

Allow me to provide a clearer explanation here: Imagine an element placed on a webpage with a margin of 10% from the left side. When the size of the page is adjusted, the margin on the left decreases accordingly - for example, 10% from 1400px - 140px, and ...

Is there a way to obtain the "rotated" coordinates of a mouse click within a canvas element?

Exploring New Features Within my image editing software, there is a canvas where users can draw shapes. These shapes are sent to a server and added to an XML file, which is then returned to the client for display. Now, I am looking to enhance the program ...

What is causing the discrepancy in CSS line-height when text spans multiple lines?

Can anyone explain why there is extra space above the first paragraph in this HTML and CSS code, but not the second or third? The line-height is consistent for all three paragraphs. How can I adjust it so that there is equal spacing around all three paragr ...

Switching the background image of a div when hovering over a particular list item

Here is my HTML: <li><a href=""><div class="arrow"></div>List Item</a></li> I'm looking to change the background image of the "arrow" class when hovering over the "List Item" with a mouse. The "arrow" class repres ...

Add a background image to the parent element while preserving the existing background color

Can you help me figure out how to apply a background image to the menu while still keeping the background color? HTML: <div class="top"> <div class="menu"> Nop. </div> Nam hendrerit erat eget tellus mollis facilisis. ...

Arranging two list items (<li>) side by side in HTML code

I am currently designing a form with a side navigation bar that will display around 15-20 elements. Each element is represented by an <li> containing a link to open a modal when clicked. This is the current structure of my code: <div id="sidebar ...