Troubleshooting the z-index issue with CSS checkbox hack

Using the css "checkbox hack" to trigger the display of a form when a label is clicked. The form is styled using a :before pseudo element to create a transparent background, but the z-index is not working as expected. Despite adjusting z-index values, the form elements appear on top of the pseudo element.

Attempting to resolve the issue, I wrapped the form in a div element, which solved the problem but caused validation errors with the w3c validator. Exploring the use of a pseudo element as an alternative solution.

The form uses absolute positioning while the pseudo element is set to fixed. Despite changing these attributes, the desired effect is not achieved. No other z-indexes have been altered in the CSS.

section input:checked + .modal_form:before {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  content: "";
  background-color: rgba(0, 0, 0, .3);
  z-index: 0;
}
section input:checked + .modal_form {
  display: block;
  position: absolute;
  top: 108px;
  left: 50%;
  margin-left: -290px;
  z-index: 2;
}
.modal_form {
  box-sizing: border-box;
  width: 580px;
  padding: 36px 40px 40px 30px;
  background-color: white;
}
<label for="modal_toggle">
  <span class="todo">Item 1</span>
  <input type="checkbox" id="modal_toggle" />
  <form action="#" method="post" class="modal_form">
    <fieldset>
      <dl>
        <dt>
            <label for="title">Title</label>
          </dt>
        <dd>
          <input type="text" id="title" name="title" placeholder="Todo Name" />
        </dd>
      </dl>

      <dl>
        <dt>
            <label>Due Date</label>
          </dt>
        <dd>
          <input type="number" name="day" min="1" max="31" placeholder="Day" />
          <!--
            --><span class="separator">/</span>
        </dd>
        <!--
          -->
        <dd>
          <input type="number" name="month" min="1" max="12" placeholder="Month" />
          <!--
            --><span class="separator">/</span>
        </dd>
        <!--
          -->
        <dd>
          <input type="number" name="year" min="2017" max="2099" placeholder="Year" />
        </dd>
      </dl>

      <dl>
        <dt class="top_align">
            <label for="description">Description</label>
          </dt>
        <dd class="description">
          <textarea name="description" id="description" placeholder="Description"></textarea>
        </dd>
      </dl>
    </fieldset>

    <fieldset class="field_align">
      <input type="submit" value="Save" />
      <input type="submit" value="Mark As Complete" />
    </fieldset>
  </form>
</label>

Seeking assistance to resolve this issue. Thank you.

Answer №1

Placing an element with a negative z-index behind its parent is a helpful CSS hack.

section {
  position: absolute;
  background: #ccc;
}

section:after {
  position: fixed;
  top: 0; right: 0;
  background: red;
  content: '';
  width: 100%;
  height: 100%;
  z-index: -1;
}
<section>
  <p>Lorem ipsum</p>
</section>

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

display child element at full width within a parent element that is not full width

I have a container with a maximum width of 1024px, and I want one of its child elements to take up 100% width starting from the left edge of the screen. Is there a way to achieve this without adding extra HTML markup? I know I can make the child element 1 ...

Is there a way to retrieve the widths of several <span> elements at once?

Having a few <span> elements generated dynamically, I need to find out their combined width. Wrapping them in a <div> and using alert($("#spanWrap").width()); resulted in the container's width instead of that of the <span> elements. ...

Make sure to blur all images whenever one of them is clicked

I am currently facing an issue with my webpage where I have 3 images displayed. I have implemented an event listener to detect clicks on the images, and once a click occurs on one of them, I want everything else on the page to become blurred, including the ...

Ways to identify the active anchor within an li element

Below is the code snippet I am currently working with: <li> <a href="<?php echo base_url()?>home/promos/call" class="promo-call active-call"></a> </li> <li> <a href="<?php echo base_url()?>home/promos/text ...

What could be the reason for the unexpected invisibility of the 4px margin on a static div?

I have a straightforward situation where I am using a fixed positioning <div> that spans the entire width with 100% width. It is designed to have a margin of 4px on all sides. However, for some reason, the right side margin is not visible. Can someon ...

Share your Facebook link with dynamic parameters

When attempting to create a share URL with GET parameters, I am encountering some issues with double encoding. <a href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.bing.com%2Fsearch%3Fq%3Dkeyword" /> This results in a faulty URL, while &l ...

tips for creating a jquery/css scales animation

I am looking to implement a unique feature on my website where I create scales with a balancing effect. The scales should mimic an up and down motion continuously. You can see an example of what I'm referring to here Is there a way in jQuery or CSS t ...

Accessing JS code from HTML is not possible in React

Attempting to create a list using React, I have utilized the module found here: https://github.com/pqx/react-ui-tree I am currently testing out the sample application provided in this link: https://github.com/pqx/react-ui-tree/blob/gh-pages/example/app.js ...

Using PHP to display arrays on the screen

Currently, I am working on manipulating an array in my code. I have already accessed the array and begun the process of displaying its contents using the following code snippet: <html> <?php $file=fopen("curr_enroll_fall.csv", "r"); $records[]=fg ...

Troubleshooting CSS Display Problem on Chrome and Firefox

While working on the design of a website offline, everything seemed to be running smoothly. However, upon uploading it to the server, various issues began to arise. Initially, the file loaded correctly upon first visit. Unfortunately, after reloading the ...

Bind event listener exclusively for click event handlers

I have a scenario where I am using 'addEventListener' to handle the opening of a menu (genres-container) only after the transition on another menu (mobile-nav) has completed. The issue I am facing is that even after closing everything and trying ...

Unable to show <LI> elements

I'm having trouble adding LI items to the #historial <UL> tag. Whenever the for loop is inside the function, the list displays with some elements duplicated. I believe the for loop should remain outside of the function. Could someone please he ...

The issue with Grid component in Nested Single file Component

I'm currently working on creating a grid component in Vue to set up a sortable and searchable chart using Single File Component. I've also integrated vue-router into the project. Below are my two .vue files. At the moment, I can only see the sear ...

In the event that the final calculated total is a negative number, reset it to zero. Inform the user of an error through the use of a prompt dialog box

I'm having trouble getting the grand total to display as 0 when I enter all amounts in positive values and then change the unit prices to negative values. However, it works fine when I only enter negative values throughout. Can someone please help me ...

Animation effects compatible with iOS devices are professionally crafted using CSS

I just implemented a custom hamburger menu with animation using HTML, CSS, and JavaScript on my website. The animation works perfectly on Android devices but not on iOS. Any suggestions for fixing this issue? I attempted to add the Webkit prefix to each p ...

React component rendering twice due to width awareness

In a React component that I've developed, I have utilized ResizeObserver to track its own width. Within this component, two divs are rendered with each having a "flex: 1" property to ensure equal width distribution. Under certain conditions, such as w ...

Is it possible to have 3 divs with the height of the tallest

I have a unique template that I employ for crafting responsive websites. It relies on boxes with percentage widths, floats, and clears. A prime instance can be viewed via this link. Notice how the three boxes vary in height. While it's simple to set a ...

Exploring the functionality of textareas within iframes on iPad

There is a known issue where textareas and inputs do not function properly on iPad Safari when placed inside an iframe. For more information, visit: This bug can easily be reproduced on iOS 7, but seems to work fine on iOS 8. Despite this issue, I am in ...

javascript/jquery - steps to design an effective modal page overlay

Struggling for the past few weeks to create a modal dialog using javascript/jQuery. While iOS and Android devices seem to work fine, Windows mobile/Opera mobile often present issues with the modal size. It either requires excessive scrolling or ends up bei ...

Implementing CSS and JS for targeted elements or functions in WordPress

I am currently managing a WordPress WooCommerce website My goal is to implement bootstrap CSS and js specifically for a function on my WooCommerce checkout page. The code I have written is as follows: if( current_user_can('administrator')) { fu ...