Having trouble making z-index work on a child div with absolute positioning

I am attempting to design a ribbon with a triangle div positioned below its parent div named Cart.

<div class="rectangle">
  <ul class="dropdown-menu font_Size_12" role="menu" aria-labelledby="menu1" style="min-width: 100px; z-index: 0">
    <li role="presentation">
       <a role="menuitem" tabindex="-1" href="#">Sign in </a>
    </li>
    <li role="presentation">
       <a rolw="menuitem" tabindex="-1" href="#">View Cart</a>
    </li>
  </ul>
  <!-- Ribbon side-fold (left and right) -->
  <div class="triangle-l triangle-left-pos"></div>
  <div class="triangle-r triangle-right-pos"></div>
</div>

Check out the JS Fiddle here My issue is that the child div is not aligning under the parent div. In the fiddle, the div appears above even though I have set the z-index lower than the parent div.

Edit: Including an image for reference on where the child div should be located.

Answer №1

Stacking contexts behave like a single unit within the parent stacking context.

The parent element with class .rectangle has established a stacking context, meaning that the children elements' z-index values are resolved within this context and cannot be placed beneath their parent.

If you adjust the z-index property of the .rectangle element, and assign negative z-index values to the children, they can be positioned as desired. This is because both the parent and children belong to the same stacking context (#root in your demo), and using negative z-index values allows the children to remain below the parent in the z-axis.

Keep in mind that if you apply a CSS3 transform or an opacity transition to the parent element, a new stacking context will be created, resulting in a rendering similar to what you see in your demo.

Reference: The stacking context

Answer №2

Are you seeking this particular solution See it in action here ?

To achieve the desired effect, simply remove the top values from both .triangle-left-pos and triangle-left-pos.

Answer №3

The current approach you are taking is not feasible unless you modify your HTML structure.

For more information on CSS Position, please refer to the documentation: https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index/The_stacking_context

However, the desired outcome is achievable. Please take a look at the example in this fiddle: https://jsfiddle.net/nileshmahaja/7coznt7o/

HTML

<div class="rectangle-wrap">
<div class="rectangle">
                            <h2 class="dropdown-toggle" data-toggle="dropdown">
                                Cart <i class="icon-shopping-cart"></i> <b class="caret"></b>
                            </h2>
                            <ul class="dropdown-menu font_Size_12" role="menu" aria-labelledby="menu1" style="min-width: 100px; z-index: 0">
                                <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Sign in </a></li>
                                <li role="presentation"><a rolw="menuitem" tabindex="-1" href="#">View Cart</a></li>
                            </ul>
    <!-- Ribbon side-fold (left and right) -->
                        </div></div>

css

.rectangle-wrap{
    position:relative;
    width:200px;
    height:32px;
}
.rectangle {
    background: none repeat scroll 0 0 #de8a03;
    float: left;
    width:200px;
    height: 32px;
    position: relative;
    top: 0;
    z-index: 2;
}


.rectangle  h2 {
    font-size: 14px;
    color: #ffffff;
    margin-top: 10px;
    text-align: center;
}

.rectangle-wrap:before {
    border-color: transparent #332619 transparent transparent;
    border-style: solid;
    border-width: 10px 10px 10px 10px;
    content:'';
    position:absolute;
    left:-10px;
    bottom:-10px;
}


.rectangle-wrap:after {
    border-color: transparent transparent transparent #332619;
    border-style: solid;
    border-width: 10px 10px 10px 10px;
    content:'';
    position:absolute;
    right:-10px;
    top:-10px;
    z-index:0
}

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

CSS selectors duplication can cause confusion and make code harder to maintain. SCSS

If we have style definitions like the following: .a.b { ... } .a.b.c { ... } Is there a method in SASS/SCSS to prevent duplication of the .a.b part? ...

Having trouble getting Jquery Ajax Post to work properly when using JinJa Templating?

Objective: My goal is simple - to click a button and post information to a database. Problem: Unfortunately, clicking the button doesn't seem to be posting to the database as expected. Setup: I am working with Flask Framework, Jquery, and Jinja Temp ...

I rely on the angular-responsive-carousel library for my project, but unfortunately, I am unable to customize the arrow and dots

When it comes to CSS, I utilize ng deep style in Angular 10 to make changes for browser CSS. However, I am facing an issue where the problem is not being resolved by my CSS code. Here is a snippet of my code: > ::ngdeep .carousel-arrow { > b ...

Placing a new item following each occurrence of 'X' in React components

Currently, I am working with a React component that uses Bootstrap's col col-md-4 styles to render a list of items in three columns. However, I am facing an issue where I need to add a clearfix div after every third element to ensure proper display of ...

Tips for stopping automatic scrolling (universal solution)

Issue or Inquiry I am seeking a way to disable actual scrolling on an element while still utilizing a scrollbar for convenience (avoiding the need for manual JavaScript implementations instead of relying on browser functions). If anyone has an improved s ...

What is causing the slideDown animation to initially transition into slideUp animation?

When I click a button, I am adding some new results. I am using the slideDown method to display these results, but for some reason, they first slide up and then down again. Additionally, when I remove the added results, they instantly disappear without any ...

A Comprehensive Guide on Creating an Expansive HTML Textbox Covering the Entire

<form method="post" class="mobile-login-form _5spm" id="u_0_0" novalidate="1" data-autoid="autoid_1"> <input type="hidden" name="lsd" value="AVpe_Wp1" autocomplete="off"> <input type="hidden" name="charset_test" value="€,´,€,´, ...

The issue with Angular-gettext is that it fails to update dynamically generated strings in the code

Whenever I try to set the language using the code gettextCatalog.setCurrentLanguage(langString);, it doesn't seem to affect my side-nav menu. My side menu has two possible states: expanded or collapsed, and I use ng-include to control its content base ...

What is the reason for the initial DOM operation taking significantly longer than subsequent ones?

Why is the first operation much more despite the subsequent manipulation of the DOM being practically identical in time when both are written with the realization in JS and jQuery? Link to Javascript source // ES5 // Sending a message function sendMessa ...

Click to reveal sliding menu bar

Trying to create a sliding menu bar (.top-bar) that activates when clicking an arrow (#hide), causing the arrow to also slide up and switch from an up arrow to a down arrow. Here is my unsuccessful attempt: $(document).ready(function(){ $("#hide").c ...

Guide to create a sliding menu transition from the viewport to the header

I am just starting to learn jQuery and I want to create a menu similar to the one on this website Specifically, I would like the menu to slide down from the viewport to the header, as shown in the template in the link. I have searched through the jQuery ...

Something seems off with Chrome

I am facing a unique issue that I need help with. Unfortunately, the code is too long to post here but essentially it involves creating a menu for a website. The menu works perfectly and looks great on all browsers except for Chrome. This is unusual as Ch ...

Incorporating JSON data seamlessly into a visually appealing Highcharts pie chart

It seems like I'm facing a minor issue here. Everything was working fine until... $(document).ready(function() { // Original data var data = [{ "name": "Tokyo", "y": 3.0 }, { "name": "NewYork", "y": 2.0 }, { "name": "Berlin", ...

Having multiple HTML select elements and utilizing jQuery AJAX

I am looking to implement a dynamic multiple select using AJAX and jQuery. The first select (c1) is functioning correctly. When I click on it, it triggers the appearance of another select (c2). Similarly, clicking on the second select (c2) should lead to ...

Footer division misbehaving

My goal is to add a footer to my website that includes a text field, but for some reason I'm encountering an issue with two of my divs not cooperating. You can observe this problem in this JSFiddle. Here is the CSS code for the footer: .footer { b ...

Guide to transforming Tailwind CSS into a class and saving it in a CSS document

Recently, I implemented Tailwind CSS to style my application. It has been a helpful tool, but I am looking to consolidate all the CSS into one file for better organization. I want to keep it separate from the HTML code. My attempt to convert the following ...

The $watchCollection function will consistently have the identical object assigned to both the new and old values

When using $watchCollection to monitor an object, how can I determine which property of the object has been changed? The problem I am facing is that in the callback function, the new and old values are both the same objects. Is there a way to watch an obje ...

Does the padding and margin of an element alter when the position is set to relative?

By utilizing relative positioning, an element can be moved in relation to its original position in normal flow. - Citation from the book "HTML&CSS: design and build websites" by John Duckett If an element has a relative position property, it opens up ...

Error message: The color shade "100" is not in the color palette. The only available shades are primary, accent, and contrast

I am in the process of upgrading from Angular 12 to 13. Encountering this error, and so far I haven't been able to find a solution for it. Could someone please guide me in the right direction? Here is my package.json file: { "name": &quo ...

Problem with IE off-canvas scrolling

Currently, I am facing an issue with the scrolling functionality of an off-canvas sidebar on my Joomla 3 website. It seems to be working fine in Chrome and Firefox, but when it comes to Internet Explorer, the visible scroll bar refuses to move when attempt ...