What is causing the unusual behavior of z-index in this specific scenario?

I have a good understanding of z-index and decided to practice with it by writing the following code:

*{
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }
        body{
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: #141214;
        }

        .card{
            /* position: relative; */
            width: 350px;
            height: 450px;
            background-color: #282729;
            border-radius: 15px;
            z-index: 1;        }
        .img{
            position: relative;
            width: 80%;
            background-color: #AAA;
            /* z-index: 1; */
        }
        .img::before{
            position: absolute;
            content: '';
            left: -70%;
            top: 10%;
            width: 350px;
            height: 350px;
            background-color: #9BDB23;
            z-index: -1;
        }
        img{
            max-width: 100%;
        }
<div class="card">
        <div class="img">
            <img src="shoes.png" alt="A Shoe">
        </div>
        <div class="content">
            <h3>Nike Shoes</h3>
            <div class="size">
                <p>size</p>
                <a href="#" class="size">7</a>
                <a href="#" class="size">8</a>
                <a href="#" class="size">9</a>
                <a href="#" class="size">10</a>
            </div>
            <div class="color">
                <p>color</p>
                <div class="color green"></div>
                <div class="color blue"></div>
                <div class="color red"></div>
            </div>
            <a href="#" class="buy">buy now</a>
        </div>
    </div>

Currently, I am facing several challenges with regards to z-index in this code snippet: (1) When img::before is given a z-index of -1, it appears behind .card instead of .img (I expected it to be placed behind its immediate parent container according to standard behavior). (2) When both img::before and .card are assigned z-index values (-1 for img::before and 1 for .card), img::before ends up behind both .img and .content. (3) If img::before has a z-index of -1, .card has a z-index of 1, and .img also has a z-index of 1 (optional), then img::before is positioned beneath .img but above .card.

There seem to be various combinations of z-indexes that are causing confusion for me in this scenario. Can anyone provide assistance in resolving this issue?

Answer №1

The lack of support for the ::before and ::after pseudo-elements is a problem with the <img> element.

Answer №2

(2)

<div class="container">
        <div class="picture">
            <img src="camera.png" alt="A Camera">

Here, container serves as the parent element and picture is the child.

Answer №3

  * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }
        body {
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: #141214;
        }

        .card {
            width: 350px;
            height: 450px;
            background-color: #282729;
            border-radius: 15px;
        }

        .img {
            position: relative;
            width: 80%;
            background-color: #AAA;
            z-index: 1;
        }

        .img::before {
            position: absolute;
            content: '';
            left: -70%;
            top: 10%;
            width: 350px;
            height: 350px;
            background-color: #9BDB23;
            z-index: -1; /* This is where the magic happens */
        }

        img {
            max-width: 100%;
        }

        .content {
            text-align: center;
            padding: 20px;
            color: #fff;
        }
 <div class="card">
        <div class="img">
            <img src="shoes.png" alt="A Shoe">
        </div>
        <div class="content">
            <h3>Nike Shoes</h3>
            <div class="size">
                <p>size</p>
                <a href="#" class="size">7</a>
                <a href="#" class="size">8</a>
                <a href="#" class="size">9</a>
                <a href="#" class="size">10</a>
            </div>
            <div class="color">
                <p>color</p>
                <div class="color green"></div>
                <div class="color blue"></div>
                <div class="color red"></div>
            </div>
            <a href="#" class="buy">buy now</a>
        </div>
    </div>

By utilizing the ::before pseudo-element in this CSS code snippet, it creates a unique visual effect by appearing behind both the .img and .content elements due to their shared stacking context defined by .img.

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

Inserting HTML content into a DIV with the help of jQuery

Looking for a solution with my index.html file. I want to load other HTML files into a DIV by clicking on buttons. Here's an example of what I'm trying to achieve: I've searched through various examples online, but none seem to work for ...

Ways to eliminate line breaks and <br> tags in text using only CSS to create a button that trunc

I am currently working on incorporating a truncated text button using only CSS. The issue I'm facing is that the "show more" button doesn't ignore the line breaks or any other relevant HTML within the teaser and text body. This is causing too muc ...

The 'in' operator cannot be used in Jquery to search for 'display' in a value that is undefined

This is the HTML code snippet I have: <div class="V_CPp Elss <?php echo $Show; ?>"> <span class="ViewC">View more comments</span> </div> <di ...

Customizing Sass Bootstrap Variables for Tailored Responsive Designs (Specifically for Mobile or Desktop)

Currently, I am in the process of working on a website that has opted for the adaptive approach, using separate mobile and desktop templates instead of responsive design. Despite this choice, Bootstrap is still being utilized on these templates, loading al ...

Broken links detected in the Full Page Navigation menu on a one-page website

The hyperlinks on this particular page seem to be malfunctioning despite the fact that the li.a tags are correctly targeting specific section IDs. Markup: <header> <a href="#0" class="nav_icon"><i></i></a> </header> ...

The Wonders of Flex Box and Media Queries

Can a website still be made responsive without relying on media queries when flexbox is already implemented? Are there specific scenarios where the utilization of media queries offers enhanced control? ...

It is not possible for me to modify the attributes in responsive mode without first generating a new class within the tag

In order to make the attribute work in a responsive manner, I believe I need to create a class within the h2 tag. However, I am unsure where the issue lies. Could it possibly be related to the browser? Check out the image below for reference: this is my p ...

How to strip HTML tags from a string in PHP while still showing them?

Let's explore the functionality of strip_tags(). strip_tags("<b>TEXT</b>"); The result will be: TEXT But what if I want to keep the tags visible without their effect? The output would look like this: <b>TEXT</b> Is i ...

Injecting background images with CSS in Vue3

After exploring answers to my previous inquiry, I am now seeking guidance on how to inject both a variable and a URL image into my scoped CSS. Despite trying various combinations, none seem to be effective: <template> <header class=< ...

The code is malfunctioning on this site (yet functions perfectly on a different website)

So I have a question that may seem silly, but it's worth asking. I'm attempting to create a sticky div element that stays in place when you scroll past a certain point on the page. This is the script I have: <script type="text/javascript"> ...

Remove any javascript code from the ajax modal when it is closed or hidden

I am in the process of creating a music website that showcases songs along with their lyrics. One of the features I have added is a lyrics button that, when clicked while a song is playing, opens up a modal displaying the live lyrics. Everything works per ...

Clicking to close tabs

I am currently working on implementing a tab functionality on my website and I want these tabs to be responsive. Here is the code snippet I have been using: function openCity(evt, cityName) { var i, tabcontent, tablinks; tabcontent = document.ge ...

SharePoint 2013 on a mobile device does not support scrolling within iframes

Recently, I set up a SharePoint 2013 website with a few iframes. However, when I access the site on my mobile devices (iPad and Android), I am unable to scroll through the entire page by touching within the iframe. I attempted to solve this issue by inclu ...

What is the best way to ensure uniformity in my boxes (addressing issues with whitespace and box-sizing using flexbox)?

Below is the code I have written: body { padding-top: 20px; text-align: center; background-color:black; } h1 { display: inline-block; background: white; } h1 { font-size: 30px } p { background: yellow; } .container { display: flex; ...

The Android webview fails to load the page, but the app successfully loads it when accessed through the

I'm experiencing an issue with my HTML5 mobile web app loading fine in a browser but getting stuck on an Android webview. We have implemented a splash screen before loading the web app, but the webview seems to be stuck on the splash screen and does n ...

Extracting Data from Imgur Videos: Measuring their Duration

Hey there! Lately, I've been faced with the challenge of extracting specific information from videos hosted on the imgur website. Specifically, I'm interested in retrieving the video length indicated in the attached image. Despite my best efforts ...

Importing JSON data from a URL to display in an HTML table

Looking to utilize the Untappd API for a beer menu project, I've encountered an issue. The goal is to showcase the JSON data received from the server and organize it into an HTML table. Despite successfully importing data from a local list.json file ...

How can the seating arrangement be optimized for a seating chart and what is the most effective way to transition away from a traditional table structure?

Currently, I am attempting to create a dynamic seating chart on a webpage within an asp.net-mvc site using data retrieved from a database. Initially, I used a table to organize the grid layout of rows and columns, which resembled this structure: The datab ...

Tips on transforming button-controlled tab pages in Bootstrap 2 to Bootstrap 3

I am currently in the process of upgrading my website from Bootstrap 2 to Bootstrap 3, and I have encountered an issue with a specific control that consists of multiple sets of tab pages controlled by toggle buttons. Here is an example of the functional c ...

Learn how to maintain floating labels in a floating state even after text input using only CSS

Figured out how to make labels float above form input fields when focused, but struggling with keeping them floating when text is entered and field is unfocused. Been searching for CSS examples online, but none seem to clarify how to achieve the desired e ...