What is the best way to stack an absolutely-positioned element within the children of a sibling element?

I am currently utilizing the vue-slick-carousel package to enhance my website.

Within this carousel, there are three key elements that I need to interact with. The first element is an image containing a character identified by the class slide__person. The second element is a text with the class slide__text, and the third element is an image of a cloud with the class banner__slider-cloud.

My requirement is for the text to overlay the cloud image, which in turn should overlap the picture of the person.

The cloud must maintain its static position and cannot be adjusted within the slider.

To achieve this desired layout, I have set up the following z-index values:

.banner__slider-cloud {
   z-index: 1;
}

.slide__text {
    z-index: 2;
}

.slide__person {
   z-index: 0;
}

Despite trying various solutions, modifying the z-index property, testing different positioning properties, I have not been able to resolve this issue.

If you wish to view my code, please check out the online sandbox through this link: codesandbox

<script>
import VueSlickCarousel from "vue-slick-carousel";
import "vue-slick-carousel/dist/vue-slick-carousel.css";
// optional style for arrows & dots
import "vue-slick-carousel/dist/vue-slick-carousel-theme.css";

export default {
  name: "HelloWorld",

  components: { VueSlickCarousel },
};
</script>

<template>
  <div class="lending">
    <main class="banner">
      <div class="banner__slider">
        <VueSlickCarousel :arrows="true" :dots="true">
          <div class="slide">
            <div>
              <img
                class="slide__person"
                src="https://www.wikihow.com/images/6/61/Draw-a-Cartoon-Man-Step-15.jpg"
              />

              <p class="slide__text">
                Lorem ipsum dolor sit amet, consectetur adipisicing elit.
                Aliquam architecto, corporis deserunt distinctio dolor dolores
                ea ex hic iste magnam nihil optio perferendis perspiciatis
              </p>
            </div>
          </div>

          <div class="slide">
            <div>
              <img
                class="slide__person"
                src="https://www.wikihow.com/images/6/61/Draw-a-Cartoon-Man-Step-15.jpg"
              />

              <p class="slide__text">
                Lorem ipsum dolor sit amet, consectetur adipisicing elit.
                Aliquam architecto, corporis deserunt distinctio dolor dolores
                ea ex hic iste magnam nihil optio perferendis perspiciatis
              </p>
            </div>
          </div>
        </VueSlickCarousel>

        <img class="banner__slider-cloud" src="../assets/cloud.png" />
      </div>
    </main>
  </div>
</template>

<style>
img {
  max-width: 100%;
}

.slick-slide > div {
  display: flex;
  justify-content: center;
}

.slide {
  max-width: 500px;
  border: 2px solid orange;
  padding: 1rem;
}

.slide .slide__text {
  border: 2px solid green;
  padding: 8px;
}

.banner__slider-cloud {
  border: 1px solid;
  max-width: 960px;
  margin: auto;
  position: absolute;
  right: 0;
  left: 0;
  top: 5%;
}
</style>

img {
  max-width: 100%;
}

.slick-slide>div {
  display: flex;
  justify-content: center;
}

.slide {
  max-width: 500px;
  border: 2px solid orange;
  padding: 1rem;
}

.slide .slide__text {
  border: 2px solid green;
  padding: 8px;
}

.banner__slider-cloud {
  border: 1px solid;
  max-width: 960px;
  margin: auto;
  position: absolute;
  right: 0;
  left: 0;
  top: 5%;
}
<div class="lending">
  <main class="banner">
    <div class="banner__slider">
      <div class="slide">
        <div>
          <img class="slide__person" src="https://www.wikihow.com/images/6/61/Draw-a-Cartoon-Man-Step-15.jpg" />

          <p class="slide__text">
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam architecto, corporis deserunt distinctio dolor dolores ea ex hic iste magnam nihil optio perferendis perspiciatis
          </p>
        </div>
      </div>

      <img class="banner__slider-cloud" src="https://via.placeholder.com/500" />
    </div>
  </main>
</div>

Answer №1

z-index is effective only on elements that share the same stacking context

To achieve the desired overlapping effect, you can reposition the cloud image element to be a sibling of the text and person image elements

<div>
  <img
    class="slide__person"
    src="https://www.wikihow.com/images/6/61/Draw-a-Cartoon-Man-Step-15.jpg"
  />

  <p class="slide__text">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit.
    Aliquam architecto, corporis deserunt distinctio dolor dolores
    ea ex hic iste magnam nihil optio perferendis perspiciatis
  </p>
  <img class="banner__slider-cloud" src="../assets/cloud.png" />
</div>
.slide .slide__person {
  position: relative;
  z-index: 0;
}
.slide .slide__text {
  border: 2px solid green;
  padding: 8px;
  position: relative;
  z-index: 2;
}

.banner__slider-cloud {
  border: 1px solid;
  max-width: 960px;
  margin: auto;
  position: absolute;
  left: 17%;
  top: 5%;
  z-index: 1;
}

Check out the updated codesandbox here

Answer №2

While achieving a complete solution may not be feasible due to the DOM manipulation in the Vue Slick Carousel library, there is a clever workaround for the z-index limitation using transform-style: preserve-3d.

If all manipulated elements in the DOM hierarchy have the preserve-3d CSS attribute applied (note that this property is not automatically inherited), you can utilize transform: translateZ() to adjust elements' layering even when they are not direct siblings in the stacking context. Refer to the modified code snippet below.

img {
  max-width: 100%;
}

/* ### Added this block ### */

.banner * {
  transform-style: preserve-3d;
}

.slick-slide>div {
  display: flex;
  justify-content: center;
}

.slide {
  max-width: 500px;
  border: 2px solid orange;
  padding: 1rem;
}

.slide .slide__text {
  border: 2px solid green;
  padding: 8px;
  transform: translateZ(1px); /* ### Added this line ### */
}

.banner__slider-cloud {
  border: 1px solid;
  max-width: 960px;
  margin: auto;
  position: absolute;
  right: 0;
  left: 0;
  top: 5%;
}
<div class="lending">
  <main class="banner">
    <div class="banner__slider">
      <div class="slide">
        <div>
          <img class="slide__person" src="https://www.wikihow.com/images/6/61/Draw-a-Cartoon-Man-Step-15.jpg" />

          <p class="slide__text">
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam architecto, corporis deserunt distinctio dolor dolores ea ex hic iste magnam nihil optio perferendis perspiciatis
          </p>
        </div>
      </div>

      <img class="banner__slider-cloud" src="https://via.placeholder.com/500" />
    </div>
  </main>
</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

Adjust the width of a float-right container to its maximum width

Looking for a solution similar to the one discussed in this question: CSS - Float to max width In my project, I am working on a layout that involves a <pic> / <info> style setup, with a twist - I want it to be responsive when the user resizes ...

Executing functions in Vue TypeScript during initialization, creation, or mounting stages

Just a few hours ago, I kicked off my Vue TypeScript project. I've successfully configured eslint and tslint rules to format the code as desired, which has left me quite pleased. Now, I'm curious about how to utilize the created/mounted lifecycl ...

What is the best way to retrieve the parent element quickly using Jquery?

html: <span class="ui-spinner ui-widget ui-widget-content ui-corner-all"><input class="spinner1 ui-spinner-input" id="q1" name="value" value="1" min="1" aria-valuemin="1" aria-valuenow="2" autocomplete="off" role="spinbutton"><a class="ui ...

Having trouble triggering a click event with JQuery

I have a hidden link for downloading Audio Files that I want the user to access using a button. However, I am having trouble triggering the click event. Below is the HTML code: <a class="APopupDown" data-icon="home" id="DownloadFile" href="http://yaho ...

Tips for ensuring font-face displays correctly for every character

There seems to be a CSS clash that I cannot pinpoint on the site below: You can find the code snippet here: /*Font face settings for h3*/ @font-face { font-family: carrotflower; src: url('/wp-content/uploads/fonts/Carrotflower.eot') format(& ...

Enhance user experience by enabling clickable links in Angular comment sections

Currently, I'm developing an application that allows users to add comments to specific fields. These comments may contain links that need to be clickable. Instead of manually copying and pasting the links into a new tab, I want the ability to simply c ...

What is the best way to create a line break in a React caption without it being visible to the user?

Is there a way to add a break or invisible character between "we make it simple" and "to create software" in order to match the Figma design for the React web app caption? https://i.stack.imgur.com/Z4rpt.png https://i.stack.imgur.com/06mMO.jpg https://i.s ...

Enhance link with dynamic content using an AJAX call

I need help appending a picture after a link with the same URL as the link. The current AJAX code is producing the following result: <a href="images/Draadloos.png" data-lightbox="gallerij"></a> Here is an example of what I would like to achie ...

Exploring the 3D Carousel Effect with jQuery

After creating a 3D carousel using jQuery and CSS3, I am looking to enhance it with swiping support. While there are plenty of libraries available for detecting swipes, I specifically want the carousel to start rotating slowly when the user swipes slowly. ...

Tips for maintaining the responsiveness of both images and text within a div container

I'm completely new to HTML and CSS. Everything looked perfectly aligned until I added text inside the three divs and now my images are unbalanced, even though I've set the same width and height: Here is the code snippet: #wrapper{ display:fl ...

What is the CSS code to apply a color to a white area on a

How do I add color to the white space between the blue blocks? Here is the code snippet: #nav{ padding:0; } .rech{ margin-left: 785px; } .px{ margin-left: 3px; } #nav li{ display:inline; } #nav li a, .rech{ font-family:Arial; font- ...

I'm trying to create a transparent v-card, but for some reason it's not turning out the way I want

How can I make the v-card transparent while keeping its content opaque using CSS? card.vue <v-card class="cardColor"> <v-card-text> TEXT </v-card-text> <v-card-actions> <v-btn color="prima ...

`Certain browsers are experiencing issues with text overlay functionality.`

I have managed to successfully create the necessary code to incorporate an image with a child element containing text overlaying the image. However, this functionality does not appear to be working on Internet Explorer (IE), and I am struggling to find a C ...

Choosing Vue select options depending on a condition

I am working on a dropdown template with Vue.js and have encountered some challenges. Here is the basic structure of my dropdown: <select v-model="selectedClient" class="stat-select text-u-c"> <option disabled value="">Please select a Client ...

What is the method for creating a checkbox in CSS that includes a minus symbol inside of it?

Can you create a checkbox with a minus "-" symbol inside using just html and css? I attempted to achieve this with JavaScript by using: document.getElementsByTagName("input")[0].indeterminate = true; However, the requirement is to accomplish this using ...

CSS rule for targeting an element that does not have any child elements

I have a variety of different elements that are structured similarly to the following.. <div id="hi"> <div class="head"> </div> <div class="footer"> </div> </div> ..however, some of these elements do no ...

Guide to Changing the Value of a Textbox Using Form jQuery

For instance: <form id="example1"> <input type="text" name="example_input"> </form> <form id="example2"> <input type="text" name="example_input"> </form> In the code above, both text boxes have the same name. H ...

Sending new props when navigating back using $navigateBack

I created an overview page where users can view their results. They have the option to edit a value by clicking on the edit button, which navigates them to another page for making changes. However, upon returning to the overview page after editing, I notic ...

Refreshing a specific div within an HTML page

I am facing an issue with the navigation on my website. Currently, I have a div on the left side of the page containing a nav bar. However, when a user clicks a link in the nav bar, only the content on the right side of the page changes. Is there a way t ...

"Utilizing Vuex: Fetch data from API when the first getter is accessed, and subsequently retrieve it from the local

I have a Vuex instance that is responsible for fetching data from an API. The initial access to the store should trigger a call to load the data from the API. Subsequent accesses should return the previously loaded data stored in store.empresas. This is th ...