简体   繁体   English

CSS:将div容器放入div容器中

[英]CSS: Place a div-Container inside a div-Container

I want to set a div container inside another div-container. 我想在另一个div容器中设置一个div容器。 The info-Container should be placeable like where i want it (for example: float:right;) and another should be inside it. 信息容器应该像我想要的地方一样可放置(例如:float:right;),并且另一个应该位于其中。

But in my case it places the containers among themselves and not inside it. 但就我而言,它将容器放置在彼此之间而不是内部。

Here my HTML-Code: 这是我的HTML代码:

<div class="info">
    s
    <div class="info-header">
    ss
    </div>
</div>

And my CSS-Code: 而我的CSS代码:

div.info {
    position: absolute;
    background: yellow;
}
div.info-header {
    position: absolute;
    background: green;
}

Thanks in advance! 提前致谢!

从第二个div CSS中删除绝对位置,然后尝试

The second div should not have absolute positioning. 第二个div不应具有绝对位置。 An element with position: absolute; 位置为:绝对的元素; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed). 相对于最近定位的祖先定位(而不是相对于视口定位,如固定)。

However; 然而; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling 如果绝对定位的元素没有祖先,它将使用文档主体,并随页面滚动一起移动

If you want to use absolute positioning on second div you have to set first div to position relative. 如果要在第二个div上使用绝对定位,则必须将第一个div设置为相对位置。

Check codepen for the example: https://codepen.io/athapliyal/pen/aGYMvP 检查codepen中的示例: https ://codepen.io/athapliyal/pen/aGYMvP

div.info {
    position: relative;
    background: yellow;
    padding: 20px;
}
div.info-header {
    position: absolute;
    background: green;
}

i hope work so: 我希望这样工作:

 body { padding: 20px; background-color: #20262e; } div.info { position: absolute; background: #ffdd57; border-radius: 3px; padding: 20px; right: 0; /* if left comment hier */ display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); grid-gap: 10px; } div.info-header { position: relativ; background: #05ffb0; border-radius: 3px; padding: 20px; font-size: 14px; } 
 <div class="info"> s <div class="info-header"> ss </div> </div> 

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

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