我在定位相对的绝对div时遇到了一些麻烦。我希望我的绝对div(内联块)增长到它达到给定的px-amount(max-width)。这按预期工作,直到我添加一个宽度(小于绝对div的最大宽度)到相对div。相对父母边缘绝对定位的div中断
我希望绝对div中的文本以最大宽度(400px)而不是相对父div(300px)的边缘断开。
当给予white-space:nowrap时,单词只流过绝对div的末尾。
有没有人有一个想法如何解决这个问题?
谢谢!
参见:
http://codepen.io/anon/pen/KVJvmZ
HTML
<div class="relativeContainer">
<div class="absoluteContainer">
Hello you! This breaks on relativeContainers edge.. This is not what i want. It should just go further an further (until it reaches max-width of 400px).
</div>
</div>
<div class="relativeContainer">
<div class="absoluteContainer">
This should stay small.
</div>
</div>
CSS
.relativeContainer {
width: 300px;
height: 100px;
border: 1px solid black;
position: relative;
margin-bottom: 50px;
}
.absoluteContainer {
display: inline-block;
position: absolute;
top: 0;
left: 0;
max-width: 400px; /* Word-break should happen here. */
border: 1px solid red;
}
这是更好的。如果你添加溢出:滚动到相对容器,然后它滚动 - 唯一的缺点是它滚动的方式超过了如果绝对容器只有400px宽的点。 – PaulG