How can I ensure that my Overlay doesn't obstruct my dropdown content?

I'm trying to create a dropdown in my fixed navbar. When I click the open button, I want the overlay to cover everything except the dropdown content.

Even after setting the z-index of my dropdown content to 10000, it still didn't work.

$("#dropDownButton").click(function () {

    $(".dropdown-content").toggleClass("show");

    if ($(".dropdown-content").hasClass("show")) {
        $(".overlayT").fadeIn(500);
    } else {
        $(".overlayT").fadeOut(500);
    }

});
body {
    font-family: "Lato", sans-serif;

}

.overlayT {
    position: fixed;
    top: 0;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 2;
    display: none;
    height: 100%;
    width: 100%;
}

.dropdown {
    position: relative;
    display: inline-block;

}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
    min-width: 200px;
    border-radius: 5px;
    padding-top: 2rem;
    z-index: 10000;
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.dropdown-content a:hover {
    background-color: #f1f1f1
}

.show {
    display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="overlayT">
  </div>

  <div style="width: 100%;  background-color: white; height:100px; border-style: solid; position: fixed; z-index: 1"
      id="dropDownButton">
      <span style="font-size:30px;cursor:pointer; float:left; margin-left: 3rem">&#9776; open
          <div class="dropdown-content">
              <a href="#">Home</a>
              <a href="#">Account</a>

              <a href="#">Sign Out</a>
          </div>
      </span>
  </div>

Is there a way for my overlay not to cover the dropdown content while keeping the navbar fixed? Your help is greatly appreciated. Thank you!

Answer №1

To ensure proper layering, you can reposition the .overlayT element to become a sibling of the .dropdown-content within the span element as they will then be on the same level:

<div style="width: 100%;background-color: white;height:100px;border-style: solid;position: fixed;z-index: 1;" id="dropDownButton">
    <span style="font-size:30px;cursor:pointer; float:left; margin-left: 3rem">
        ☰ open
        <div class="overlayT"></div>
        <div class="dropdown-content">
            <a href="#">Home</a>
            <a href="#">Account</a>
            <a href="#">Sign Out</a>
        </div>
    </span>
</div>

Afterwards, apply the CSS property left: 0 to the overlay for correct positioning:

.overlayT {
    position: fixed;
    top: 0;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 2;
    display: none;
    height: 100%;
    width: 100%;
    left: 0;
}

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

creating a div that functions similarly to a radio button

Is it possible to create a div that mimics the behavior of a radio button? I'm looking for a solution that doesn't involve jquery, as many people have recommended. After doing some research, I found a few potential options. In the past, I'v ...

Tips for dynamically adjusting the color of link text within jQuery Mobile?

When using jQuery Mobile, links are styled according to the theme stylesheet. If you want to change this styling, you can use !important as follows: .glossaryLink{ color: white !important; background-color: #CC8DCC; } I am looking to dynamically cha ...

Choose from two dropdown lists to select values and then load the page

I am interested in creating a simple feature using PHP or JavaScript: It should either take the user to a post/page, or load the content of the post/page on the post/page itself (using AJAX or similar methods). The desired functionality is as follows: U ...

Utilizing AngularJS to invoke REST API calls within a for loop and efficiently manage priority for the visible content area

Currently, I am dealing with an array containing over 400 data IDs related to different fruits. My goal is to make API calls in a for loop to retrieve relevant information about each fruit. While this works fine for smaller lists, it fails when dealing wit ...

Is it possible to utilize $(this) within the $.post() function?

I seem to be having trouble accessing $(this) from inside the $.post section. It works perfectly fine outside of it. Here is the JavaScript code: $('.idea').each(function(){ var title = $(this).html(); $.post("votes.php", { t ...

Apply a unique style to a child element of a JavaFX LineChart

Is it possible to utilize 2 objects, each with a single line? https://i.sstatic.net/402Qp.png Currently, I have set the color like this: .chart-series-line {-fx-stroke: #ff0099; }, which changes the color of both lines. However, I need the lines to have d ...

Having difficulty changing the visibility of a div element

I am currently working on a project that involves jQuery and ASP.Net. My main goal is to create a button that can hide/show a div using jQuery. Below is the code that I have implemented: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLI ...

Setting the child elements of a CSS div to have the same width and be larger than the

Welcome everyone to my inaugural post! I hope I've followed the right steps. I'm encountering an issue where I have child divs that need to have equal widths. The #content can sometimes exceed the browser window's width (hence the 3000px), ...

What's causing this unexpected wrapping issue?

I must be overlooking something simple. Could someone please review this page and explain why the BYOB section is appearing on the right, rather than directly below the American section? Thank you in advance. ...

Difficulty with the Jquery <span> tag on Mozilla 3.6.1

I am currently utilizing jQuery.post to display some data retrieved from a servlet. <div class="Label"> <span id="result" >Click on Check.</span> </div> <script> $(document).ready(function() { $("a").c ...

Highlight all text in the textbox upon selection

I discovered a helpful script on how to select all contents of a textbox when it receives focus using JavaScript or jQuery. My attempt to implement this in IE10 revealed that the focus was being cleared at a later time, and my efforts to prevent this (whi ...

Adjust the background color and transparency of a specific section using HTML

Is there a way to modify the background color or opacity of a specific area within an image? Take a look at my HTML, JavaScript, and CSS: function changeColor() { document.getElementById('testid').setAttribute("class", "style1"); } ...

Having trouble implementing Bootstrap Progress Bar with AJAX in webpy

I am currently working on a web application using webpy and dealing with a Bootstrap progress bar in the template HTML file. To update this progress bar dynamically, I aim to utilize jQuery AJAX calls to fetch data from the server. In order to achieve thi ...

CrossBrowser - Obtain CSS color information

I'm attempting to retrieve the background color of an element: var bgcolor = $('.myclass').first().css('background-color') and then convert it to hex function rgbhex(color) { return "#" + $.map(color.match(/\b(\d+ ...

Setting the height of the text area in a three-column layout cannot be achieved using a percentage value

I'm working on creating a 3-column layout with two text areas. The issue I'm facing is that when I adjust the width of the text areas to create columns, the height shrinks. I need a solution that will be compatible with HTML5 browsers - support f ...

Solved: Resolved the issue of Jquery Mobile header and footer malfunctioning on iPhone

Having an issue with the fixed header and footer not working properly when using Jquery mobile in an iPhone webview app. It is working fine on iPad, but on the iPhone, it keeps moving instead of staying fixed. I've spent countless hours trying to fig ...

How can I adjust the Bootstrap container width without it changing when resizing the browser window?

As I work on developing my website, I am utilizing Twitter's bootstrap grid system. Despite my efforts, I have been unable to find a solution for fixing the width of the site. My goal is to lock the size in place so that it remains constant regardless ...

Ways to remove content that exceeds the boundaries of a div element

I am facing a challenge with a parent div that has a specified size. Inside this parent div, there are other child divs, and if any of those child divs start displaying their content outside of the parent div, I want to completely remove that div. I do not ...

Glitch in jQuery UI Tooltip

While using the jQuery UI tool for tooltips, an unusual bug has been noticed that cannot be replicated or resolved. Typically, this tool utilizes the title="whatever" portion of a tag to create a stylish CSS hover effect. However, it seems to also captur ...

Determining the identity of an HTML element using its surrounding values

I have a webpage with a table displaying various services and their corresponding download buttons. Here is an example of the content: nov. service 1 [ click to download pdf ] nov. service 5 [ click to download pdf ] nov. service 3 [ ...