【问题标题】:Child div ignoring parent width without using white-space nowrap子 div 忽略父宽度而不使用空格 nowrap
【发布时间】:2014-05-10 03:47:19
【问题描述】:

对于一个非常具体的用例,我有:

  • 固定宽度的父 div
  • 具有 未知宽度最大宽度 的子 div

这是一个示例:http://jsfiddle.net/t9xUV/

CSS:

.parent{
  width: 400px;
  height: 400px;
  background-color: grey;
}

.child{
  background-color: red;
  height: 200px;
  max-width: 500px;
}

HTML:

<div class="parent">
    <div class="child">dddd ddddddd dddddddd ddddddddddddd ddddd dddddddddd ddddddd dddddddd dddddddddddd
    </div>
</div>

我希望孩子采用自己的宽度,忽略父宽度。

我试过了:

  • position:absolute 用于子级,但单词是根据父级宽度包装的
  • white-space: nowrap 用于孩子,但 max-width 属性随后被单词“忽略”

有什么想法吗?

【问题讨论】:

    标签: html css


    【解决方案1】:

    这是一个可行的解决方案:http://jsfiddle.net/t9xUV/1/

    只需将此 css 属性设置为子元素:

      margin-right:-999999px;
    

    来源:Forcing Block Elements to Not Wrap in IE (no fixed width parent)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-09
      • 1970-01-01
      • 2012-12-13
      • 2013-02-17
      • 1970-01-01
      相关资源
      最近更新 更多