简体   繁体   English

像容器一样放置绝对元素的宽度(在CSS中)

[英]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.

相关问题 flexbox容器的绝对宽度 - Absolute flexbox container's width CSS位置的绝对值和父容器的宽度(以百分比为单位) - CSS position absolute and width of parent container in percent Css:位置元素相对于它的容器顶部的底部 - Css: Position element by it's bottom relative to it's container's top 即使通过CSS / CSS3滚动时,是否有可能将具有绝对位置的元素“固定”到其父容器的底部? - Is it possible to have an element with absolute position to be 'fixed' to the bottom of it's parent container, even when scrolling via CSS/CSS3? CSS 定位绝对元素,自动宽度超出父边界? - CSS Positioned Absolute Element, automatic width outside of parent's bounds? 使用CSS调整位置绝对元素的宽度以适应内容 - Resize position absolute element width to content with CSS 绝对位置对绝对位置宽度的响应 - position absolute responsive to position absolute's width auto CSS - 使用绝对 position 设置子宽度大于父宽度 - CSS - Set child width greater than parent's width with absolute position 绝对位置元素的宽度在超出相对父元素的右边框时会被截断 - Absolute position element's width truncates when extending beyond relative parent's right border CSS:position absolute 的屏幕宽度与 position 在出现垂直溢出时固定在 chrome 中存在冲突 - CSS: position absolute has conflicting screen width with position fixed in chrome when there's a vertical overflow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM