【问题标题】:“Float” two absolutely positioned nested divs“浮动”两个绝对定位的嵌套 div
【发布时间】:2017-04-10 04:47:38
【问题描述】:

我想将两个未知宽度的嵌套 div 定位在其内部内容的稍上方。这是一个小提琴:https://jsfiddle.net/rdvzsxnq/

绝对定位

position: absolute;
top: -15px 

将两者放在一起,而我希望第二个位于第一个的右侧。我可以添加

left: 150px;

到第二个,就像我在小提琴中所做的那样,但这只有在我知道第一个的宽度时才有效。而在真实的情况下我没有。

还有

float: left;

不适用于绝对位置 div。

有没有办法在这些绝对定位的 div 上获得一个实际上是 float:left 的?

(如果这可以响应,那就更好了。可能有几个这样的注释,而不仅仅是两个,而且它们可能不适合狭窄的窗口。)


编辑:到目前为止,我最好的方法是假设最大数量的此类音符并将每个音符的宽度设置为分数(例如 1/3),然后在第二个上设置 left:33%,在第三个上设置 66% .不理想,但很简单。

div > .overlay + div > .overlay { left: 33% }
div > .overlay + div > .overlay + div > .overlay { left: 66% }

https://jsfiddle.net/ak3mpe2n/4/

仍在寻找理想。 . .

【问题讨论】:

  • 那些 2div 总是在那里吗?那些 2 div 总是由单行组成吗?这些 div 的目的是什么?填充顶部可能很有用,也可以左右坐标。
  • 请描述一下您对这部分的功能需求,它有什么作用?
  • 在 DIV 的嵌套中,每个 DIV 在其主要内容之前都有一些注释——#outerNote 和#innerNote 在小提琴中。基于某些用户操作,Javascript 将打开或关闭该注释。启用时,注释将覆盖主文本,并锚定在主文本上。在一个嵌套中,anchors 是一样的,左上角的内部内容。所以笔记会重叠。只有顶部是可读的。要求:将笔记四处移动,以便所有人都能阅读。

标签: css


【解决方案1】:

如果你只是将两者都包装在一个绝对定位的 div 中,Float 确实有效:

[relative] {
  position: relative;
}
[absolute] {
  position: absolute;
  width: 100%;
}
[float] { 
  float: left;
  height: 100px;
  width: 100px;
  border: 1px solid black;
  margin: 1rem; 
  background-color: rgba(255,255,255,.8);
}
[float][right] { 
  float: right;
}
<div relative>
  <div absolute>
    <div float>First float</div>
    <div float>Second float</div>
    <div float right>I float, right?</div>
  </div>
</div>
<p>[32] But I must explain to you how all this mistaken idea of denouncing of a pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?

【讨论】:

  • 是的,但我试图“浮动”的 div 是嵌套的。
  • 网页设计就是根据视觉规范显示信息。标记无关紧要,编程语言无关紧要。重要的是根据所需规格显示所需信息。这里的规格是什么,@JPM?有什么要求?在你告诉我标记必须保持不变之前,我会告诉你,如果它保持不变,它就行不通。您需要动态创建一个绝对父级并抢夺其中的所有浮动子级,使用javascript 才能以这种方式显示此数据。
  • 但是,如果归结为JavaScript,您可以即时计算绝对元素的位置,就像 pinterest 使用它们的引脚一样。无需在 DOM 中移动它们。底线是:仅使用 CSS,您想要的是不可能的。您绝对定位的内容不再影响正常流程中的内容。关联两个或多个绝对定位元素(使用 CSS)的唯一方法是删除它们的 position:absolute 并将它们放在同一个绝对定位的父元素中。
  • 我现在正在考虑强制每个音符的宽度为 25%。然后用CSS设置left:第二个25%,第三个50%,第四个75%(假设不会超过四个)。
  • @JPM,我现在更好地理解了你的问题(我承认我最初只是在对角线上阅读它——我很抱歉——只是尽可能地在问题上花更少的时间)。这是我的做法:如果您只需要显示一次便笺,则为便笺构建一个容器,并在任何使便笺可见的动作上将便笺推送到该容器,从它所在的位置(隐藏,内联)。如果您需要打开/关闭它们,您可以克隆它们并保留参考。切换时,如果它们存在于“显示板”中,则只需删除克隆。如果没有,你加一个。需要一个例子吗?
【解决方案2】:

我不确定这是否会满足,因为我重新安排了一些标记,但它可能值得考虑作为一种方法:

div {
  position: relative;
  line-height: 1.8;
  box-sizing: border-box;
}

#outer {
  width: 100%;
  display: block;
  z-index: 100;
  background-color: white;
  position: absolute;
  top: 30px;
  left: 0px;
  box-sizing: border-box;
}

#outerNote {
  width: 50%;
  border: 1px solid blue;
  float: left;
}

#innerNote {
  width: 50%;
  border: 1px solid red;
  float: left;
}
The cause of America is in a great measure the cause of all mankind. Many circumstances have, and will arise, which are not local, but universal, and through which the principles of all lovers of mankind are affected, and in the event of which, their
affections are interested.

<div id="outer">

  <div id="outerNote">
    Note for the outer div.
  </div>

  <div id="innerNote">
    And then a note for the inner div.
  </div>

</div>

Ipsum lorem and some more ipsum lorem. Ipsum lorem and some more ipsum lorem. Ipsum lorem and some more ipsum lorem. Ipsum lorem and some more ipsum lorem. Ipsum lorem and some more ipsum lorem. Ipsum lorem and some more ipsum lorem. Ipsum lorem and somemoreipsum

【讨论】:

  • 是的,如果浮动项是标记中的同级项,则浮动有效。但他们不是。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-25
  • 1970-01-01
相关资源
最近更新 更多