【发布时间】:2016-02-12 23:57:50
【问题描述】:
我在将绝对 div 定位在相对 div 中时遇到了一些麻烦。我希望我的绝对 div(内联块)增长,直到达到给定的 px 数量(最大宽度)。这可以按预期工作,直到我将宽度(小于绝对 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;
}
【问题讨论】: