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

时间:2013-03-05 15:15:07

标签: javascript jquery html css

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

我正在使用something like this

<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>
div {
    background:red url(http://lorempixel.com/600/400/);
    float:left;
    width:600px;
    height:400px;
}
p {
    background:#fff;
    padding:20px;
    margin:20px;
}

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

enter image description here

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

enter image description here

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

enter image description here

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

enter image description here

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

5 个答案:

答案 0 :(得分: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);    
    }
});

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;
}

答案 1 :(得分:1)

这不是很难用JavaScript做。我不知道没有JS怎么做(如果可能的话)。

您可以使用另一个“不可见”div来测量尺寸,直到它达到320px高度,同时将其减少一定量(如果您想尽可能精确,则每次减少1个像素)。

var $measurer = $("<div>").css({'position': 'fixed', 'top': '100%'})
    .text($("p").text()).appendTo('body');

var escape = 0;
while ($measurer.height() < 320) {
    console.log($measurer.height());
    $measurer.width(function (_, width) { return width - 1; });
    console.log($measurer.width());
    escape++;
    if (escape > 2000) {
        break;
    }
}
$("p").width($measurer.width());
$measurer.remove();

http://jsfiddle.net/RwczR/2/

答案 2 :(得分:1)

试试这个:

var p = $('p');
var height = parseInt(p.height())-40;
p.height('auto');
p.width('auto');
for(var i=p.width(); i--; ) {
    p.width(i);
    if (p.height() > height) {
        p.height(height+20);
        p.width(i-1);
        break;
    }
}
p.height(height);

http://jsfiddle.net/RwczR/6/

答案 3 :(得分:1)

您可以使用jQuery / JavaScript并检查客户端与滚动高度不断增加宽度,直到它适合文本,类似于下面的内容。

您还需要在overflow: hidden;的{​​{1}}标记的CSS中设置p,以便为您提供包括溢出在内的实际高度。

以下代码也考虑了两者的保证金和填充;高度和宽度并相应调整。

相应改变外部div的高度。

scrollHeight

DEMO - 增加宽度,直到文字完全可见


答案 4 :(得分:0)

我们可以给段落overflow:auto;

如果段落需要垂直滚动条,则会创建一个。

诀窍是不断收紧宽度,直到创建滚动条。

var hasScrollBar = false;
var p = document.getElementById('myParagraph');
while(!hasScrollBar)
{
    if(p.scrollHeight>p.clientHeight)
    {
        //Has Scroll Bar
        //Re-Increase Width by 1 Pixel
        hasScrollBar=true;
        p.style.width=(p.clientWidth+1)+"px";
    }
    else
    {
       //Still no Scroll Bar
       //Decrease Width
       p.style.width=(p.clientWidth-1)+"px";
    }
}