将子项设置为内容宽度,忽略父级宽度,并进行父级滚动

JSu*_*uar 2 css

仅使用CSS,是否可以获得以下示例http://jsfiddle.net/LdJ7t/,而无需明确知道子元素的宽度?

最终结果如下:

  • 父元素可滚动到子元素
  • 子元素的宽度设置为内容

父元素可滚动到子内容

#Parent {
    width: 100px;
    height:200px;
    background: #ccc;
    overflow:auto;
    padding: .5em;
    margin: .5em;
}

#Child {
    width:300px;
    height:100px;
    background:yellow;
}?

<div id="Parent">
    <div id="Child">
        This is a test. This is a test.
    </div>
</div>?
Run Code Online (Sandbox Code Playgroud)

看起来display:inline-block;几乎可行:http://jsfiddle.net/LdJ7t/1/

我认为这是可能的.我只是找不到解决方案.

jfr*_*rej 5

您的inline-block解决方案是正确的 - 如果您将更长的单词放入图像或图像中,则会出现滚动条.默认情况下,文本在空白处被破坏.

如果你不想在空格上打破文本,你可以white-space: nowrap;像这里一样添加到子div:http://jsfiddle.net/LdJ7t/2/