全高度内容,尽可能小的宽度

Wes*_*rch 8 html javascript css jquery

我永远不会认为这是可能的,但这里有很多聪明人,所以我想我会问.我正在寻找一种方法来拥有一个全高容器,其宽度取决于有多少内容.我希望文本填充占据整个高度的区域,同时使用尽可能小的宽度.高度是已知的并且是硬编码的,内容的数量不是.

我正在使用这样的东西:

<div>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.
    Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
    when an unknown printer took a galley of type and scrambled....</p>
</div>
Run Code Online (Sandbox Code Playgroud)
div {
    background:red url(http://lorempixel.com/600/400/);
    float:left;
    width:600px;
    height:400px;
}
p {
    background:#fff;
    padding:20px;
    margin:20px;
}
Run Code Online (Sandbox Code Playgroud)

通常内容从上到下填充页面:

在此输入图像描述

我正在寻找的是相反的,从左到右填写:

在此输入图像描述

内容较少,应该如下所示:

在此输入图像描述

使用完整的硬编码高度width:auto会产生这种效果:

在此输入图像描述

有没有办法让文本以尽可能小的宽度填充高度,而无需硬编码宽度或文本溢出?这似乎是不可能的,我不知道如何处理它.Javascript/jQuery解决方案欢迎.

Ter*_*rry 3

我想到的是一个简单的 jQuery 解决方案:在 while 循环中,设置条件,以便每当高度超过容器高度时就运行循环。<p>在循环中,您逐像素增加宽度,直到高度不再超过容器高度:)

$(document).ready(function() {
    // The 400-80 is because you have to subtract the container padding and the element's own padding
    while($("div > p").height() > 400 - 80) {
        currentWidth = $("div > p").width();
        $("div > p").width(currentWidth + 1);    
    }
});
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/teddyrised/RwczR/4/

我也对你的 CSS 做了一些更改:

div {
    background:red url(http://lorempixel.com/600/400/);
    float:left;
    overflow: hidden;
    width:600px;
    height:400px;
    padding: 20px;
    box-sizing: border-box;
}
p {
    background:#fff;
    padding: 20px;
    width: 1px;
}
Run Code Online (Sandbox Code Playgroud)