“浮动”两个绝对定位的嵌套div

时间:2017-04-09 17:49:33

标签: css

我想将两个未知宽度的嵌套div放在略高于其内部内容的位置。这是一个小提琴:https://jsfiddle.net/rdvzsxnq/

绝对定位

position: absolute;
top: -15px 

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

left: 150px;

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

另外

float: left;

不会使用绝对的位置div。

有没有办法实际上得到一个浮点数:留在这些绝对定位的div上?

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

编辑:我到目前为止最好的是假设这样的音符的最大数量并将每个音符的宽度设置为分数(比如1/3),然后设置左:第二个为33%,第三个为66% 。不理想,但很简单。

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

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

仍在寻找理想。 。 。

2 个答案:

答案 0 :(得分:1)

Float实际上是有效的,如果你只是将它们都包装在一个绝对定位的div中:



[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?
&#13;
&#13;
&#13;

答案 1 :(得分:0)

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

&#13;
&#13;
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;
}
&#13;
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
&#13;
&#13;
&#13;