Is it possible to layer more than 2 divs on top of each other using Bootstrap?

I'm attempting to stack 4 divs on top of each other using z-index

It's working properly for only 2 divs

When I try to blur the main background (1st bg), it affects all of the divs

What mistake am I making?

Check out the code below:

HTML-

<div class="bg1">
  <div class="bg2">
    <div class="overlay">
      <div class="text-layer">
        <p>Eleven years ago, wizards rejoiced all over the world, and Muggles (non-magic folks) were puzzled. They celebrated because He-Who-Must-Not-Be-Named was defeated. In other words, Voldemort (the most evil wizard around) killed Harry Potter's parents,
      but for some reason, he couldn't kill baby Harry. Now Voldemort seems to have vanished. Overnight, baby Harry has become a hero known as "The Boy Who Lived." Homeless and orphaned, Harry finds a new home with his Muggle extended family, the Dursleys.</p>

        <p>Fast forward to present day, ten-year-old Harry lives with his mean aunt, uncle, and cousin Dudley. During Dudley's birthday trip to the zoo, Harry encounters a friendly snake and inexplicably frees it from its cage against glass. Strange letters begin arriving
      for Harry after the zoo visit. Unable to stop them, Harry's furious uncle takes the family to an island in an attempt to escape the onslaught of mail.</p>

        <p>Their hiding place doesn't last long as Hagrid appears on Harry's birthday bearing news that Harry is a wizard and has been accepted at Hogwarts School of Witchcraft and Wizardry. The next day, Harry goes shopping for school supplies at Diagon Alley, where he learns more about his magical heritage. He makes a few acquaintances, including Draco Malfoy and his Defense Against the Dark Arts teacher, Quirrell. And of course, Harry gets his first wand. Exciting stuff! Hagrid also retrieves a mysterious package from Gringotts, the goblin-owned bank.</p>
       </div>
     </div>
   </div>
 </div>

CSS-

.bg1 {
 position: relative;
 background-image: url("http://www.hdnicewallpapers.com/Walls/Big/Flowers/Sun_Flower_HD_Image.jpg");
 left: 0px;
 top: 0px;
 z-index: -1;
 filter: blur(4px);
 -webkit-filter: blur(4px);
}

.bg2 {
 background-image: url("https://www.w3schools.com/howto/img_girl.jpg");
 height: 100%;
 z-index: 1;
 position: relative;
 background-position: center;
 background-repeat: no-repeat;
 background-size: cover;
 -webkit-filter: opacity(30%);  /* Safari 6.0 - 9.0 */
 filter: opacity(40%);
}

.overlay {
 z-index: 2;
 position: relative;
 top: 0;
 width: 100%;
 height: 100%;
 background: rgba(0, 0, 0, 0.3);
}

.text-layer {
 height: 100%;
 height: 100vh;
 z-index: 300 !important;
}

View the live example on jsfiddle- jsfddle

Answer №1

To achieve the desired outcome, I recommend adjusting your HTML structure slightly. One approach is to make the text-layer the top layer and set backgrounds and overlay layers to cover its entire width and height using absolute positioning.

<div class="text-layer">
    <div class="bg1"></div>
    <div class="bg2"></div>
    <div class="overlay"></div>
    <div class="text-content">
        <!-- Consider adding a container for organization -->
        <div class="container">
            <!-- text goes here -->
        </div>
    </div>
</div>

Below are the corresponding styles:

Note: The z-index property only functions on positioned elements, such as relative, absolute, or fixed.

.text-layer {
  position: relative;
}

.bg1, .bg2, .overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}

.bg1 {
  background-image: url("http://www.hdnicewallpapers.com/Walls/Big/Flowers/Sun_Flower_HD_Image.jpg");
  background-repeat: no-repeat;
    filter: blur(4px);
  -webkit-filter: blur(4px);
  z-index: 1;
}

.bg2 {
  background-image: url("https://www.w3schools.com/howto/img_girl.jpg");
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
    -webkit-filter: opacity(30%);
  filter: opacity(40%);
  z-index: 2;
}

.overlay {
  background: rgba(0, 0, 0, 0.3);
  z-index: 3;
}

.text-content {
  /* 
   * It's necessary to set this to relative to utilize z-index since the 
   * default setting is static. Refer to the note above.
   */
  position: relative;
  padding: 1rem;
  z-index: 4;
}

Fiddle: http://jsfiddle.net/aq9Laaew/101650/

Answer №2

 .bg1 {
            position: relative;
            background-image: url("http://www.hdnicewallpapers.com/Walls/Big/Flowers/Sun_Flower_HD_Image.jpg");
            left: 0px;
            top: 0px;
            z-index: -1;
            overflow: hidden;

        }
        .bg1:before{
            content: "";
            position: absolute;
            width : 100%;
            height: 100%;
            background: inherit;
            z-index: -1;

            filter        : blur(10px);
            -moz-filter   : blur(10px);
            -webkit-filter: blur(10px);
            -o-filter     : blur(10px);

            transition        : all 2s linear;
            -moz-transition   : all 2s linear;
            -webkit-transition: all 2s linear;
            -o-transition     : all 2s linear;
        }

        .bg2 {
            background-image: url("https://www.w3schools.com/howto/img_girl.jpg");
            height: 100%;
            z-index: 1;
            position: relative;
            background-position: center;
            background-repeat: no-repeat;
            background-size: cover;
            -webkit-filter: opacity(30%);
            /* Safari 6.0 - 9.0 */
            filter: opacity(40%);
        }

        .overlay {
            z-index: 2;
            position: relative;
            top: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.3);
        }

        .text-layer {
            height: 100%;
            height: 100vh;
            z-index: 300 !important;
        }
<div class="bg1">
    <div class="bg2">
        <div class="overlay">
            <div class="text-layer">
                <p>Many years ago, sorcerers around the globe celebrated as the darkest wizard was defeated and a young orphan became a hero known as "The Boy Who Lived." With the help of powerful allies, this orphan's journey from tragedy to triumph unfolds, revealing a world of magic, mystery, and danger.</p>

                <p>After receiving an invitation to attend Hogwarts School of Witchcraft and Wizardry, Harry embarks on a magical adventure filled with new friendships, challenges, and secrets waiting to be uncovered. As he explores this hidden world, he discovers his own unique abilities and confronts dark forces that threaten to destroy everything he holds dear.</p>

                <p>With the guidance of wise mentors and the support of loyal friends, Harry learns to harness his powers and embrace his destiny as the chosen one fated to face his greatest enemy. The path ahead is fraught with peril, but Harry's courage, compassion, and determination will lead him towards a final confrontation that will test his strength and resolve like never before.</p>
            </div>
        </div>
    </div>
</div>

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

Implement drop-shadow filtering specifically for Internet Explorer

I am currently implementing a drop-shadow filter on a curved png, and while it works perfectly in most browsers, IE seems to be the exception. You can see the issue with the boxes on this page: (Three boxes located just below 'Yoga Sequence Builder ...

Generate a fresh DOM element when a particular height threshold is achieved, utilizing a portion of the previous DOM element's content

Update: 27th December 2016 The heading has been modified because any DOM element can be the target, whether it is a <p> element or not. Additional information has been provided about the tools being used and the desired outcome. Are there nativ ...

Align a bootstrap navigation item to the right without a dropdown menu

I'm having trouble positioning the Login/Register link to the right of my navbar. I've checked other solutions on this platform, but they either involve dropdown menus or don't work for me. Here's my Navbar code: <link href="http ...

Ways to retrieve parameters using $_GET

I am having trouble retrieving the parameter $_GET with a value that contains the character: '#'. Here is the code I am using: <iframe src="http://localhost/wp352/wp-content/plugins/heloworld/templates/options-rte.php?text_content=<span s ...

Vue: The enigmatic world of ghost properties?

During my project work, I encountered the following code snippet: Main component - <ParameterModal>: <template> <modal-wrapper props="..."> <!-- ... other similar templates are present... --> <template v-else-if="moda ...

"Create a dynamic and user-friendly responsive navbar using Bootstrap with toggle

I need assistance with creating a new project template to convert a WordPress template into a Bootstrap template. Currently, I am working on the navbar and facing issues with responsive design and toggle navigation. On laptop devices, the navbar looks goo ...

Is it feasible to place an ordered list within a table row <tr>?

Below is a code snippet demonstrating how to create an ordered list using JavaScript: Here is the JavaScript code: <script type="text/javascript"> $(document).ready(function() { $("ul").each(function() { $(this).find("li").each(functio ...

Issue TS2304: Unable to locate the $ in Angular 5 component

In my Angular component, I have a side-navbar that expands when clicked, similar to the functionality shown here. Here is the HTML snippet used to create the navbar: HTML: The following HTML code with openNav() function will expand the navbar: <div&g ...

Tips for showcasing array values on an HTML webpage

Having trouble displaying the "latitude" and "longitude" in html. I attempted to use a forEach loop, but it was not successful https://i.sstatic.net/FnLMP.png this.otherService.getList().subscribe( (response: any) => { this.atm = response; H ...

What is the best way to save personalized CSS styles into a database?

Appreciation for assisting others and contributing to open source projects. I have developed a Vue.js to-do application that stores data in Firebase using vue-fire and Firestore. One issue I am experiencing is that the styling, such as "line-through" for ...

What is the best way to bring in an image when the resolution falls below 700 pixels?

Is there a way to import an image only if the screen resolution is greater than 700px? HTML: <picture> <img id="logo_mobile"> <!-- I have also tried this --> <source srcset="logomobile.jpg ...

PHP not capturing multiple checkbox selections in email form submissions

I have recently started coding in PHP and I am facing an issue with my checkboxes when submitting the form. All fields work correctly except for receiving multiple checkbox values, where I only receive the text "ARRAY" in the email. Please review my code b ...

Can someone explain the significance of this CSS code (specifically regarding font-size division)?

Forgive me, but I'm struggling to come up with a more suitable title for my query. I've come across this piece of code font: 9pt/18px Tahoma; Can anyone shed some light on what it signifies? ...

The background image is not adjusting properly to the browser when resized

I'm having trouble keeping my background image fitted to the browser when I resize the window. Can someone please assist me with this issue? Here are links to images that illustrate what's happening: View images. The first image shows how it shou ...

How do I retrieve a specific object value from a JSON API using an index within a forEach loop in JavaScript?

Over the weekend, I decided to delve into learning how to utilize fetch() APIs. While exploring an interesting API service, I encountered a little hiccup with javascript. The Issue Although I successfully fetched the data from a .Json file, I faced diffi ...

Creating a SASS rule for targeting an element only if its parent does not have a specific classname can be achieved by using the '&' symbol in

Before anything else, I want to express my apologies if a similar query has been raised previously. Unfortunately, finding such specific cases can be quite challenging. My objective is to style every "a" element, excluding those enclosed within h1, h2, h3 ...

establishing irregular edges for the div container

Looking to create a class that can transform an element into a specific shape using CSS. Not entirely sure if this is achievable with pure CSS alone, but applying a class to the image element will morph it into the desired shape as shown below. <style ...

Using CSS to create shape gradients

I've been attempting to replicate the shape gradient found on Laracasts for hours, but unfortunately, my code isn't displaying anything. Can anyone lend a hand? a{ background: linear-gradient(118deg,#328bf2,#1644ad); border-radius: 54% ...

Adjust the floated item's width when it is moved downwards

I have two divs positioned side by side. The first div is 600px wide and the second is 200px wide, with some spacing in between. Both divs are floated to the left so that if the window size is reduced, the second div will move below the first. My goal is ...

What are some ways to enhance the loading time of my PHP-powered website?

Having troubles with my PHP-driven website that showcases real-time stock market data due to slow loading speeds. The site utilizes PHP for scraping financial information from external sources and presenting it on the front end. However, the performance is ...