Why is it that the z-index doesn't seem to affect the stacking order of my background image?

I am facing an issue with my Bootstrap carousel where the z-index is not working as expected. The decoration I have in the background is overlapping the text instead of appearing behind it. I want the illustration to be positioned under the text.

.carousel-fade .carousel-item.active,
.carousel-fade .carousel-item-next.carousel-item-start,
.carousel-fade .carousel-item-prev.carousel-item-end {
  z-index: 0;
}

article {
  z-index: 1;
}

.illustration {
  z-index: 0;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="63010c0c17101711021323564d524d50">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<section>
  <div class="container position-relative">
    <div class="carousel slide carousel-fade" id="ExampleCarousel" data-bs-ride="carousel" data-bs-interval="false">
      <div class="carousel-indicators">
        <button type="button" data-bs-target="#ExampleCarousel" data-bs-slide-to="0" class="active" aria-current="true" aria-label="Slide 1"></button>
        <button type="button" data-bs-target="#ExampleCarousel" data-bs-slide-to="1" aria-label="Slide 2"></button>
      </div>
      <div class="carousel-inner">

        <div class="carousel-item active">
          <div class="row py-0">
            <article class="col-12 position-relative">
              <div class="row">
                <header>
                  <h2>The Title</h2>
                </header>
                <div class="col-12">
                  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vel elementum sapien. Fusce ac pellentesque mi. Nulla sollicitudin euismod quam efficitur aliquet. Integer convallis tempus elit, aliquet ullamcorper arcu rutrum vitae.
                    Vivamus sit amet lobortis erat, ut congue est. Etiam sit amet ipsum rhoncus, vestibulum</p>
                </div>
              </div>
            </article>
            <div class="col-md-5">
              <img src="https://via.placeholder.com/80" alt="Image" class="img-fluid">
            </div>
          </div>
        </div>

        <div class="carousel-item">
          <div class="row py-0">
            <article class="col-12 position-relative">
              <div class="row">
                <header>
                  <h2>The Title</h2>
                </header>
                <div class="col-12">
                  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vel elementum sapien. Fusce ac pellentesque mi. Nulla sollicitudin euismod quam efficitur aliquet. Integer convallis tempus elit, aliquet ullamcorper arcu rutrum vitae.
                    Vivamus sit amet lobortis erat, ut congue est. Etiam sit amet ipsum rhoncus, vestibulum</p>
                </div>
              </div>
            </article>
            <div class="col-md-5">
              <img src="https://via.placeholder.com/80" alt="Image" class="img-fluid">
            </div>
          </div>
        </div>

      </div>
    </div>
  </div>
<div class="illustration position-absolute">
<svg width="17" height="24" viewBox="0 0 17 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M8.5 22.48A54.831 54.831 0 0 1 5.076 18c-1.044-1.53-2.072-3.23-2.837-4.918C1.469 11.382 1 9.77 1 8.4a7.35 7.35 0 0 1 2.192-5.228A7.552 7.552 0 0 1 8.5 1c1.993 0 3.902.782 5.307 2.172A7.35 7.35 0 0 1 16 8.4c0 1.37-.468 2.982-1.239 4.682-.765 1.688-1.793 3.388-2.837 4.917a54.819 54.819 0 0 1-3.424 4.48ZM5.65 11.232A4.054 4.054 0 0 0 8.5 12.4a4.054 4.054 0 0 0 2.85-1.167A3.982 3.982 0 0 0 12.536 8.4a3.982 3.982 0 0 0-1.186-2.833A4.054 4.054 0 0 0 8.5 4.4a4.054 4.054 0 0 0-2.85 1.167A3.982 3.982 0 0 0 4.465 8.4c0 1.065.428 2.083 1.187 2.833Z" stroke="#B5B5C3" stroke-width="2"/></svg>
</div>
</section>

<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e6c6167">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

EDIT

For easy reference, check out this codepen: https://codepen.io/andrea121/pen/WNZBYPL If you remove "carousel-fade" from the code, the two svg images will appear behind the text. But when "carousel-fade" is added, the issue arises.

Answer №1

For z-index to work effectively, elements must be positioned. Simply assigning a z-index to a CSS class is not enough.

article { z-index: 2; }

article { position: absolute; z-index: 2; }

or

article { position: relative; z-index: 2; }

If multiple children have positioned elements, the last child will appear on top.

Note: When using !important, it is often a sign that rules are being forced, indicating potential issues in the code (though not always).

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

Exploring Instagram post layouts: A comparison of card-columns in Chrome versus other web browsers

Utilizing Bootstrap 4 card-columns for showcasing Instagram posts on a website has showcased some discrepancies across different browsers. Notably, Chrome seems to be losing the second column as compared to other browsers. Screenshots highlighting these di ...

Turning a string retrieved from the element's data attribute into a JSON format

I am running into an issue with the code snippet below. It seems that $.parseJSON() is having trouble with single and double quotes. I'm stuck on finding a solution to this problem. Any help would be greatly appreciated! <div data-x='{"a":"1" ...

What is the best way to iterate through anchor links surrounding an image and dynamically load content into the figcaption element using AJAX?

Seeking assistance for an issue related to my understanding of the $this keyword in jQuery. I am facing a problem where I have 3 images on a page, each wrapped in an anchor link. My goal is to loop through these links, retrieve the URL of each anchor lin ...

Retrieving checkbox values from HTML and storing them in a temporary JavaScript/HTML variable

My form has multiple checkboxes all with the same id "cbna". When I submit the form, I want the JavaScript code to check if at least one checkbox is checked. If no checkbox is checked, an alert should appear. If a checkbox is checked, the value stored in ...

Can someone explain the significance of this in an HTML form?

Can anyone explain the following code snippet found in a web form? I found this button code on a webpage, but I'm having trouble understanding it. Could someone provide some clarity? <input type="submit" name="ctl00$ContentPlaceHolder1$Button8" v ...

Is there a way to utilize the function body onload in jQuery?

I have some code that needs to be fixed. Currently, when I see <body onload="start()" onresize="resize()" onorientationchange="resize()">, I want the following code snippet to work: $("button").click(function(){ $("body").onload = start; or win ...

evolving the design of mui stepper

I am looking to customize the code below for my specific needs I want to change the icon from https://i.sstatic.net/dtPHU.png to this: https://i.sstatic.net/ct7Zv.png I am unable to modify the style and also need to add an intermediate step I have incl ...

Cloned bootstrap nav tabs are controlled just like the original version

I have a unique scenario where I need to generate a series of "cards" with tabs on top (each card having tabs). To accomplish this, my plan was to use a template element that I can clone and then populate. Everything seems to work fine, except for the tabs ...

Avoiding HTML code within XML

I'm currently experiencing an issue with an invalid XML character appearing during interaction with a SOAP web service. When sending a formatted HTML message to a specific web service, I encountered a failed message that read as follows: Fault messa ...

Tips for accessing a DOM element's ::before content using JavaScript

Is there a way to retrieve the content of a DOM element's ::before pseudo-element that was applied using CSS3? I've attempted several methods without success, and I'm feeling very confused! // https://rollbar.com/docs/ const links = docum ...

Do not display large numbers within an HTML card

I have https://i.sstatic.net/DkowD.png this card here and displaying dynamic data inside it. The number is quite large, so I would like it to appear as 0.600000+. If a user hovers over the number, a tooltip should display the full number. How can I achieve ...

What causes the CSS Position Fixed Property to fail?

Can someone help me fix my navbar so that it remains fixed when I scroll? Currently, it moves up as I scroll down. Below are the source codes I'm using: * { margin: 0; padding: 0; box-sizing: border-box; } h1 { margin: 1rem; } ul { posit ...

Adjust the height of the image using CSS while maintaining its aspect ratio within the Gall-Peters projection

I've implemented a Gall-Peters projection on my website and I'm looking for the right CSS code to maintain its aspect ratio. I want the image to take up 100% of the available width without elongating the countries' shapes. Can anyone help wi ...

Monitoring changes within the browser width with Angular 2 to automatically refresh the model

One of the challenges I faced in my Angular 2 application was implementing responsive design by adjusting styles based on browser window width. Below is a snippet of SCSS code showing how I achieved this: .content{ /*styles for narrow screens*/ @m ...

Is there a way to make the fixed table header scroll along with the table body data?

I am facing an issue with my table where I have multiple columns. I have managed to fix the table header successfully, however, when I scroll horizontally through the table body columns, the header remains fixed and does not move accordingly. How can I res ...

Is there a way to alter the color of radio buttons?

Is there a way to use CSS to change the background color of a radio button? I've searched online, but all the solutions I found involve JavaScript, which I don't fully understand. I attempted this CSS code, but it didn't have the desired eff ...

Looking to implement a dual-column layout for posts on WordPress using Bootstrap

Creating an intranet website for a client which functions similar to a Facebook or Twitter feed. The layout includes three columns using Bootstrap's grid system. The first column serves as a navigation sidebar, while the other two columns should disp ...

Tips for resolving vertical alignment styling for images of varying sizes within Vue.js transition-group?

I have created an image slider example, but I'm facing a styling issue when using images of different dimensions. The element leaving the array is positioned absolutely for smooth transitions, but this causes the image to move up. I am trying to vert ...

What is causing this animation to malfunction?

Could someone assist me in hiding this div-container for a brief period of 2 seconds? If you have any alternative suggestions, feel free to share them but, please refrain from using jQuery or JavaScript. #vcontainer { height: 450px; width: 100%; ba ...

Using Jquery to utilize next() function along with removeClass()

https://i.sstatic.net/6xlR6.png The image above demonstrates what I am trying to achieve - when a row is clicked, it should hide, the next row turns red and clicking on that hides it too, moving on to the next available row in the sequence. $(document).r ...