【问题标题】:How to place two divs side by side where LEFT one is sized to fit and other takes up remaining space?如何将两个 div 并排放置,其中 LEFT 的大小适合,而其他的则占用剩余空间?
【发布时间】:2013-09-20 08:51:03
【问题描述】:

我正在尝试使用以下条件将两个 div 并排放置:

  1. 两个 div 必须保持在同一行。
  2. 必须优先考虑左侧 div。应在左侧 div 中显示尽可能多的文本,直至溢出时使用省略号。
  3. 右侧 div 的文本应右对齐。在溢出的情况下,应该使用省略号。
  4. 文本是动态的,因此不能使用百分比或固定宽度。
  5. 只需要在基于webkit的浏览器上工作,所以首选CSS3解决方案。

以下是一些外观示例:

输入

<div class='left'>I should always fit. If not, ellipsis should be used.</div><div class='right'>Right align and fit me if space available here.</div>

输出

输入

<div class='left'>I should always fit. If not, ellipsis should be used. And some more text and more, and more text.</div><div class='right'>Right align and fit me if space available here.</div>

输出

输入

<div class='left'>This text is left aligned.</div><div class='right'>This text is right aligned.</div>

输出

【问题讨论】:

    标签: css html ellipsis


    【解决方案1】:

    容器宽度除外,可以用%来定义,这里有一个解决方案。 唯一有效的方法是将容器背景设置为孩子的背景。

    否则最后一个条件真的很难实现:) 只是真实。

    这是小提琴链接

    width fiddle

    这是CSS

    .container {
    width: 100%;
    overflow:hidden;
    whitespace:nowrap;
    max-width:100%;
        background-color:red;
    }
    .left {
        width:auto;
        background:red;
        white-space:nowrap;
        overflow:hidden;
        text-overflow:ellipsis;
        -ms-text-overflow:ellipsis;
        float: left;
        position:absolute;
        max-width:inherit;
    }
    .right {
        background:yellow;
        white-space:nowrap;
        overflow:hidden;
        text-overflow:ellipsis;
        -ms-text-overflow:ellipsis;
        text-align: right;
        width:auto;
        float:right;
    }
    

    看它是否合适。如果有人对您粘贴的最后一张图片有另一种解决方案,那么最后一个条件真的很难,那么请分享:)

    【讨论】:

    • 您的绝对位置对您的浮动没有意义,或者如果容器没有相对定位,则可能对页面没有意义,或者将容器设置为relative,但您可能无法做到这一点尊重我们不知道的他页面的其余部分。摆脱所有这些,然后你就会得到我的答案:)
    • 我猜在所有 5 个条件的一轮中 :) 我尽可能地测试了所有。我知道有些样式可能是额外的 :) 不好解决
    • 感谢您的努力。正确的 div 应该被推离视图并且应该出现省略号,因此我奖励了 Matthew。
    • 好的 :) 试一试永远不会伤害实际上我不喜欢 jquery 代码 :)
    • 其实我试着坚持最后一点。只需要在基于 webkit 的浏览器上工作,所以首选 CSS3 解决方案。因为即使使用 jquery 和 javascript 也很容易。 :)
    【解决方案2】:

    我有它,但当有空白空间时,我的右 div 正在吃它(文本右对齐)。您没有将其列为要求,所以我不确定这是否只是您如何绘制的?在这里摆弄:http://jsfiddle.net/mdares/fSCr6/

    HTML:

    <div class="container">
        <div class="left">Some Text, Repeat, Repeat, Repeat, ,Some Text, and then: </div>
        <div class="right">other Text ttt other Text tttother Text tttother Text ttt</div>
    </div>
    
    <p />
    
    <div class="container">
        <div class="left">Some Text, Repeat, Repeat, Repeat, ,Some Text, Some Text, Repeat, Repeat, Repeat, ,Some Text,</div>
        <div class="right">other Text ttt other Text tttother Text tttother Text ttt</div>
    </div>
    
    <p />
    
    <div class="container">
        <div class="left">Some Text, Repeat, Repeat, Repeat, ,Some Text, </div>
        <div class="right">other Text ttt</div>
    </div>
    

    CSS:

    .container {
        width: 600px;
    }
    .left {
        max-width: 100%;
        background:red;
        white-space:nowrap;
        overflow:hidden;
        text-overflow:ellipsis;
        -ms-text-overflow:ellipsis;
        float: left;
    }
    .right {
        background:yellow;
        white-space:nowrap;
        overflow:hidden;
        text-overflow:ellipsis;
        -ms-text-overflow:ellipsis;
        text-align: right;
    }
    

    最后:

    【讨论】:

    • 嗨,马修,看起来确实不错。我可以在它们两者之间添加一个空格字符来解决您提到的空格问题。因为我不想使用固定宽度,所以我在容器元素上没有“宽度:600px”的情况下对其进行了测试,它看起来工作得很好。它在 Chrome 中完美运行,但是,在 Safari 上,当右 div 完全推离视图时,它(即右 div)将换行到下一行。有可能解决这个问题吗?
    • 容器只是为了模拟将包含这些到 div 的任何内容 - 即不打算成为解决方案的一部分,只是模拟它们所在的页面。至于 Safari - 我想我有它在工作中安装,并会尝试看看那里有什么。
    • 好的 - 已复制。我没有看到它,因为它仅在容器没有固定大小(数字)时发生。并且仅当您调整大小时(刷新会使它再次看起来正确)。现在正在尝试解决它。​​
    • 好的 - 尝试了几件事,但我没有看到任何 CSS 唯一方法来处理在容器上没有固定大小的窗口调整大小。如果页面开始时小于左侧div 的宽度,它在 Safari 中对我有用。你可以在 JS 中使用类似的东西来做到这一点:$(window).resize(function(){ $('#right').width($(window).width() - $('#left').width()); });
    • 您好 Matthew,我将接受您的答案,因为它在加载页面并满足问题中最初陈述的所有标准时工作。在 Safari 上调整窗口大小时,由于文本换行到下一行,它会被破坏。我认为这在实践中不会成为问题,因为它适用于您无法调整大小的移动浏览器。只是出于好奇和更好的理解,您知道为什么文本会在调整大小时换行吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-05
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    相关资源
    最近更新 更多