【问题标题】:Full height content with the smallest possible width具有最小可能宽度的全高内容
【发布时间】:2013-03-05 15:15:07
【问题描述】:

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

我正在使用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;
}

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

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

内容少了,应该是这样的:

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

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

【问题讨论】:

标签: javascript jquery html css


【解决方案1】:

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

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

【讨论】:

  • 看起来是一个很棒的解决方案,我现在只是想将它插入我的项目中并且失败了。编辑:啊,我忘了先设置width: 1px;。我最终选择了 10px 的间隔(足够好)。非常好,小巧,简单的解决方案。
  • 使用.outerHeight(true) 获得全高(带内边距和边距)。
  • @PavloMykhalov 确实如此,但我一般会避免在这种情况下使用边距,因为边距折叠的变化会导致计算复杂。
  • 这太棒了,谢谢!我喜欢不必猜测宽度,它适用于不同的字体大小和内容(客户端可以编辑文本)。我最终还是使用了原始 CSS,因为我必须支持几个不知道 box-sizing 的浏览器(猜猜是谁)。
【解决方案2】:

这对于 JavaScript 来说并不是难。我不知道没有 JS 怎么办(如果可能的话)。

您可以使用另一个“不可见”的 div 来测量尺寸,直到它达到 320 像素的高度,同时将其减少一定的量(如果您想尽可能精确的话,一次甚至可以减少 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/

【讨论】:

    【解决方案3】:

    试试这个:

    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/

    【讨论】:

    • 它什么也没做。也许演示中缺少一些东西。
    • @WesleyMurch 您需要单击以将文本更改为随机长度的字符串
    • 啊我的错,看起来很棒!一个小调整:self.height(height+60)
    【解决方案4】:

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

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

    下面的代码也同时考虑了边距和内边距;高度和宽度并进行相应调整。

    相应地更改外部 div ajdust 的高度。

    $(document).ready(function(){
        var $container = $("div");
        var containerHeight = $container.height();
        var containerWidth = $container.width();
    
        var $textWrapper = $(">p", $container);
    
        var paddingMarginHeight = $textWrapper.outerHeight(true) - $textWrapper.innerHeight();
        var paddingMarginWidth = $textWrapper.outerWidth(true) - $textWrapper.innerWidth();
    
        $textWrapper.innerHeight(containerHeight - paddingMarginHeight);
    
        //SetMinWidth();
        var maxWidth = containerWidth - paddingMarginWidth;
        var visibleHeight = 0;
        var actualHeight = 0;
    
        for(i = 50; i <= maxWidth; i++){
            $textWrapper.innerWidth(i);
    
            visibleHeight = $textWrapper[0].clientHeight;
            actualHeight = $textWrapper[0].scrollHeight;
    
            if(visibleHeight >= actualHeight){
                break;
                console.log("ouyt");
            }
        }
    });
    

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


    【讨论】:

    • @WesleyMurch:您不需要 jQuery,但它可以更轻松地动态获取填充/边距。否则,这是一个动态解决方案。 50 最小宽度仅用于演示。当然,您可以扩展它以包括clientWidth`scrollWidth` 检查以确保也涵盖了最小宽度。仍然非常有活力。
    • 很好的解决方案。我选择了较短的那个。抱歉我的回复延迟,我不得不测试所有这些答案(没想到这么多)。不过,它们几乎都是相同的方法。
    【解决方案5】:

    我们可以给段落一个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";
        }
    }
    

    【讨论】:

    • 这似乎让浏览器崩溃了,也许我做错了什么。
    猜你喜欢
    • 2022-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多