[英]Position absolute element's width like it's container (in css)
So there is the following layout: 因此,存在以下布局:
We are focusing at the 'SERVICEUSER' button and its submenu problem. 我们将重点放在“ SERVICEUSER”按钮及其子菜单问题上。
So is there a way to make the minimum width of the submenu (with position absolute) the same as its parent. 因此,有一种方法可以使子菜单(具有绝对位置)的最小宽度与其父菜单相同。
I have recreated the situation on this jsfiddle link . 我已经在jsfiddle链接上重新创建了这种情况。
So basicly what is needed is to make the .collection min-width same as the width of the li
. 因此,基本上需要使.collection的min-width与
li
的宽度相同。
The html structure of this situation is like this: 这种情况的html结构如下:
<ul>
<li>
<a href="">SERVICEUSER</a>
<div class="collection">
<div class="item">Item-1</div>
<div class="item">Item-2</div>
<div class="item">Item-3</div>
</div>
</li>
</ul>
The css of this situation is like this: 这种情况的css是这样的:
ul { list-style-type: none; }
li { display: inline; }
a { background: green; }
.collection {
position: absolute; // this is necessary for the situation
background: white;
}
An element positioned with "absolute" is contained in the flow of its closest relative parent. 定位为“绝对”的元素包含在其最接近的相对父元素的流程中。 If you make
li
relative, you can set the width for collection. 如果使
li
相对,则可以设置收集的宽度。
li { display: inline; position: relative; }
.collection {
position: absolute;
background: white;
min-width: 100%;
white-space: nowrap;
}
Edit: I added white-space: nowrap
to allow the collection (subitems) to be larger than the main item. 编辑:我添加了
white-space: nowrap
以允许集合(子项)大于主要项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.