简体   繁体   English

CSS“复选框黑客” z-index不起作用

[英]CSS “Checkbox hack” z-index not working

I'm using the css "checkbox hack" so that when someone clicks on a label, a form pops up. 我正在使用CSS“复选框黑客”,以便当有人单击标签时,会弹出一个表单。 The form element is using a :before pseudo element to create a transparent background, but for some reason I can't get the z-index to take effect on the form so that the background appears behind the form. 表单元素使用:before伪元素来创建透明背景,但是由于某些原因,我无法使z-index在表单上生效,因此背景显示在表单后面。 If you go past a certain difference in z-indexes, the inputs of the form appear on top of the :before pseudo element, but not the background or button colors, etc. 如果您经过z索引的一定差异,则表单的输入将显示在:before伪元素的顶部,但不会显示背景或按钮的颜色等。

I've already tried wrapping the form in a div , which I was able to get to work. 我已经尝试过将表格包装在div ,然后就可以开始工作了。 However, w3c validator doesn't like a div as a child of a label element. 但是,w3c验证程序不喜欢将div作为label元素的子元素。 Trying to figure it out using a pseudo element, since I've seen it work before. 尝试使用伪元素弄清楚它,因为我以前看过它可以工作。

The form position attribute is set to absolute and the pseudo element to fixed . 表单位置属性设置为absolute ,伪元素设置为fixed I read that it might have something to do with it, but I changed them all & it still doesn't have the desired effect. 我读到它可能与它有关,但我全部更改了它,但仍达不到预期的效果。 There are no other z-indexes edited in the rest of the css file. 在css文件的其余部分中,没有其他Z索引被编辑。

 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> 

Thanks in advance. 提前致谢。

Using a negative z-index on an element will place it behind it's parent. 在元素上使用负Z索引会将其放置在其父项之后。

 form { position: absolute; background: #ccc; } form:after { position: fixed; top: 0; right: 0; background: red; content: ''; width: 100%; height: 100%; z-index: -1; } 
 <form> <label>asdf</label> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM