简体   繁体   English

CSS:如何将div彼此叠放

[英]CSS: How to place divs on top of each other

Take a look at this Fiddle 看看这个小提琴

I am trying to place the div #popup at the bottom of the page, overlapping any previous content and 我试图将div #popup在页面底部,与之前的所有内容重叠,

  • having the width of his parent ( #content ) 具有他父母的宽度( #content
  • the width of his parent is not given 没有给出他父母的宽度

I am not sure, but I think position: absolute does not work in this case, at least not in the way I implemented it. 我不确定,但我认为是这样的position: absolute在这种情况下不起作用,至少在我实现它的方式上不起作用。

How would I do that? 我该怎么做?

The key to using the position: absolute for a popup like this is to ensure that the container is set to position: relative . 对于这样的弹出窗口,使用position: absolute的关键是确保将容器设置为position: relative Additionally, you will need to set the z-index to ensure that the popup is shown above the content and set the location using the top property. 此外,您将需要设置z-index以确保弹出窗口显示在内容上方,并使用top属性设置位置。

To meet the requirement of having the pop up be the width of the content, you could just set width: 100% 为了满足使弹出窗口成为内容的宽度的要求,您可以设置width: 100%

#content {
  cursor: pointer;
  position: relative;
}
#popup {
  display: none;
  position: absolute;
  top: 0;
  z-index: 1;
  width: 100%;
}

See the updated fiddle here: https://jsfiddle.net/xsxo0xmd/18/ 在这里查看更新的小提琴: https : //jsfiddle.net/xsxo0xmd/18/

Technically, you can use position: absolute with any container that is not set to position : static (the default value for position), as described here https://developer.mozilla.org/en-US/docs/Web/CSS/position 从技术上讲,您可以将position: absolute用于未设置为position : static任何容器position : staticposition : static的默认值),如此处所述https://developer.mozilla.org/zh-CN/docs/Web/CSS/位置

The absolutely positioned element is positioned relative to nearest positioned ancestor (non-static). 绝对定位的元素相对于最近定位的祖先(非静态)定位。 If a positioned ancestor doesn't exist, the initial container is used. 如果不存在已定位的祖先,则使用初始容器。

For an illustrative guide to using absolute positioning inside relative positioning, check out https://css-tricks.com/absolute-positioning-inside-relative-positioning/ 有关在相对定位中使用绝对定位的说明性指南,请访问https://css-tricks.com/absolute-positioning-inside-relative-positioning/

Append this to your CSS for the said element: 将其附加到您的CSS中,用于上述元素:

#popup {
    position: fixed; //this is your friend
    bottom: 0;  //stick to bottom
    z-index: 1; //bring the element in front of other element.
}

I've updated the JSFiddle for you. 我已经为您更新了JSFiddle。

https://jsfiddle.net/xsxo0xmd/15/ https://jsfiddle.net/xsxo0xmd/15/

Position absolute is needed, As you want width to be taken from parent. 需要绝对位置,因为您要从父级获取宽度。 Due to postion:absolute the element is positioned relative to its first positioned (not static) ancestor element. 由于post:absolute,该元素相对于其第一个定位(非静态)祖先元素定位。 Also you want that container as pop up, which will be achieved through z-index. 另外,您还希望该容器弹出,这将通过z-index实现。

FYI Please check updated 仅供参考请检查更新

 $(document).ready(function(){ $('#content').click( function(){ popup = $('#popup'); if (popup.hasClass('show')) { popup.slideUp(250, function() { popup.fadeOut(250).removeClass('show'); }); } else { popup.slideDown(250, function() { popup.fadeIn(250).addClass('show'); }); }; } ); }); 
  .CodeMirror { height: calc(100vh - 25px); width: 100%; } #content { cursor: pointer; background-color:green; } #popup { display: none; position: absolute; bottom: 0; left: 0; width: 100%; height: 100px; background-color: red; z-index: 100; } 
 <script type="text/javascript"> var myCodeMirror = CodeMirror(document.body, { value: "function myScript(){return 100;}", mode: "javascript", lineSeparator: null, theme: "default", // theme directory indentUnit: 2, smartIndent: true, tabSize: 2, indentWithTabs: false, electricChars: true, extraKeys: null, lineWrapping: false, lineNumbers: true, firstLineNumber: 1, scrollbarStyle: "overlay", inputStyle: "contenteditable", readOnly: false, // also "nocursor" showCursorWhenSelecting: false, lineWiseCopyCut: true, undoDepth: 200, historyEventDelay: 1250, autofocus: true }); </script> <div id="content"> ABC <div id="popup"> DEF </div> </div> 

Try using z-index: 2 on .popup 尝试在.popup上使用z-index: 2

And for #content use z-index: 1 对于#content使用z-index: 1

jsfiddle jsfiddle

Example

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

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