How can a hidden div be shown within a parent div that is set to display: none without altering the parent div?

I'm dealing with a wrapper div that has the classes d-none d-lg-block, making it visible only on large screens.

Inside this wrapper, there's another div that should become visible when the user clicks a button. I've tried various methods to override the display, z-index, and position properties, but it seems like the d-none class of the wrapper is stubbornly unchangeable. I need a solution that doesn't involve altering the wrapper div (i.e., removing the d-none class), as other elements within it must remain hidden from the user.

Take a look at the example below:

$("#btn").on('click', function() {
  document.getElementById("text-to-display").classList.toggle("display-me");
});
.display-me {
  position: absolute;
  top: 30px;
  width: 100%;
  z-index: 9999;
  display: block !important;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <div class="row">
    <div class="col d-none">
      <h2 id="text-to-display">Should be visible</h2>
    </div>
  </div>
  <div class="row">
    <div class="col" id="btn">
      <button>Click me</button>
    </div>
  </div>
</div>

Check out Codeply Example

Answer №1

By using the d-none class, you have effectively hidden the entire div element, causing the elements within it to remain hidden as well due to the parent element's invisibility.
To resolve this issue, simply adjust your JavaScript code so that instead of toggling the .display-me class on the <h2> element, you toggle it on the <div>.

Since Bootstrap's d-none class includes display: none !important, it is recommended to either define your class in a <style> block or ensure that your CSS file is linked after Bootstrap's. Take a look at this resource for more information.

$("#btn").click(function() {
    $(".col.d-none").toggleClass("display-me");
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<style>
  .display-me {
      position: absolute;
      top: 30px;
      width: 100%;
      z-index: 9999;
      display: block !important;
  }
</style>

<div class="container">
    <div class="row">
        <div class="col d-none">
            <h2 id="text-to-display">Should be visible</h2>        
        </div>
    </div>
    <div class="row">
        <div class="col" id="btn">
            <button>Click me</button>
        </div>
    </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

Your Javascript code currently consists of both jQuery and vanilla JS which may not work together seamlessly.

Vanilla JS code:

document.getElementById('btn').onclick = function(){
    document.querySelector(".col.d-none").classList.toggle("d-block");
}

Additional Note: If you are using Bootstrap, consider using d-block instead of display-me for simplicity. The .display-me class adds unnecessary properties for display other than the display property.
Alternatively, if you prefer not to toggle the .col.d-none class, you can move the ID from the h2 to the div and apply an onclick event to the ID.

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

Automatically close the previously flipped card in Bootstrap 4

I have a card container with multiple cards inside. When I click on a card, it flips to show the back. However, I want the previously clicked card to close when I click on another card. My current code is not working as expected despite trying various Jque ...

What adjustments can be made to the Bootstrap code to prevent it from causing layout issues with the Signin Form and Footer?

Recently, I've encountered some challenges with the footer formatting on my website's home page and the sign-in form on a separate page. It appears that the Bootstrap framework at the top of the page is affecting these elements exclusively. How c ...

Eliminating the need for horizontal scrolling in the window causes a change in the height of an internal DIV element

Check out this snippet of code, a simplified version of a larger piece: html, body { margin: 0 !important; padding: 0 !important; /* overflow-x: hidden; /* uncomment this line */ } .app { position: relative; width: 100%; text-align: center !important; } ...

Exploring the world of animation with Jquery and images

I'm delving into the world of jQuery to create some captivating animations. Currently, I've been tasked with an assignment involving images that expand, contract, change opacity, and eventually hide. Below is my code snippet: function play_pic1 ...

Increase the space around the text while zooming out to create a larger surrounding area

When zooming out, I would like the black area surrounding my text to expand instead of a white area appearing to the right. I attempted setting width:auto; on both div elements, but the black part disappeared completely. Below is my HTML code: <!DOCTYP ...

Conflicting CSS selection elements in WordPress causing theme theme selection conflicts

Despite trying various edits to my custom CSS, such as including ::selection{color: white; background: #2f3f58;}, copying and pasting code from sites like W3Schools and stack overflow, nothing seemes to work. I am using the Hueman theme from , which has a ...

I'm having trouble opening my input file dialog. Can anyone help me troubleshoot this issue in

I've been developing a website for a school project and I encountered an issue with styling the file input button. When I click on it, nothing happens except for a color change due to the hover effect applied. How can I troubleshoot this problem? < ...

When the *ngFor directive disrupts the CSS Grid Layout, resulting in all items being displayed in a single column

I am a beginner in the world of programming and web development. Currently, I am working on building my own personal website. My goal is to arrange boxes in a grid with 4 columns, similar to the layout you can find at this link: Each box represents an ob ...

Issue with mouse hover not triggering overlay effect between two div elements

I am trying to display articles in a 2x3 matrix with article details like image, title, author, and description wrapped inside a div. I want this entire div to be overlapped by another div of the same dimensions containing a single image. Below is a snipp ...

What is the best method to obtain the following id for an image?

Whenever I hover over the li with the number 3, I am getting the wrong number for the next id in the img tag. The first time, I get next id = 4 which is incorrect. But on the second hover, I get next id = 0 which is what I actually need. How can I correctl ...

Achieving Dynamic Alignment in CSS: How to Responsively Align a Div to the Width of its Parent Using Padding and Margins

My dilemma could not be clearer - I am struggling to properly align 4 divs within a width of 1150px (the maximum width of the content div). When resizing the screen, I want them to adjust accordingly: 4 in a row when possible, then 3 in the center, and so ...

What is the best way to ensure uniform spacing between header elements?

I am having an issue with aligning elements in my header while using justify-content: space-between. When I add padding-right: 20px to my navigation links, it causes my h2 element to shift to the right and disrupts the alignment. How can I maintain the pad ...

Watir /Cucumber Element not found: dd-field div.dd-arrow-box (CSS Selector)

Chief complaint: The element cannot be located using CSS Selector Custom Css path: html.ng-scope body.ng-scope div.snap-content div.content-container div.ng-scope div.content-box div div.cb-landing-module div.deposit-acct-box div.dropdown div.dd-field ...

What is the best way to align an element next to another using id or class?

I am looking to align the search element next to either "Media Heading 1" or "Media Heading 2" based on their id/class. For instance: Assume I have an element with the class of "media-item-1" and I aim to position the search div alongside that element us ...

Change justify-content-between to something else

After successfully fixing the issue with aligning the navbar brand in the middle of my navigation bar, I am now facing a new challenge. I want to add a button right before the right navigation toggle, but all my attempts have been unsuccessful so far. I&ap ...

Utilize the CSS content property to embed an image without causing it to distort in size

Struggling to swap out one image for another without stretching it? In my demo, I replaced an elephant with a lion using only CSS - no changes allowed to the HTML. Unfortunately, the lion image ends up stretched. Even adding background-size: cover doesn&ap ...

I seem to be experiencing difficulties with my dropdown menu as it is not

Having trouble creating a dropdown menu in HTML and CSS? If the subset of items in the parent items <ul> tags is not displaying properly on hover, you're not alone. The child items may be invisible or displayed incorrectly across the page. Chec ...

How to automatically open JQuery Accordion panels when the page loads

My Accordion loads with the 1st panel open upon page load, but I am looking for a way to have the entire Accordion closed by default. Any help or suggestions on how to achieve this would be highly appreciated. Thank you for your assistance. FIDDLE http:// ...

Using angular 7 to apply a dynamic CSS class on a span element

I want to dynamically change the CSS class of a span element in an ngx datatable based on the value. Here is my HTML code: <ngx-datatable #orderinvoice class="bootstrap" [rows]="invoiceSource" [headerHeight]="50" [footerHeight]="50" [rowHe ...

What is the best way to conceal a canvas or another item?

Within my main canvas, there are three smaller canvases and a text element. <canvas id="Background" width="350" height="300" style="border:6px solid black; position: absolute; left: 10px; top: 10px;"> </canvas> <canvas id="Canvas1" width ...