【问题标题】:Using text-overflow ellipsis in middle of list items在列表项中间使用文本溢出省略号
【发布时间】:2015-08-31 12:56:49
【问题描述】:

我正在尝试实现 HTML/CSS,其中在全宽无序列表元素 (ul) 中有四个列表项 (li),但我正在努力让其中一项使用 @987654323 @ 命令。

结果应该是这样的......

+-------------------------------------------------------------------------------------------+
| Item One | Item Two Two Two Two Two Two Two Two Two Two Two ...  | Item Three | Item Four |
+-------------------------------------------------------------------------------------------+

第 1、第 3 和第 4 个项目要锁定到位 - 第 1 个项目在左侧,第 3 和第 4 个项目“锁定”在全宽区域的右侧。

第二项应该占据所有剩余空间,... 省略号在溢出处。

此区域将在响应式设计中使用,因此会根据可用的屏幕区域进行扩展/缩小。

所有四个项目将包含可变数量的文本,但第二个项目总是最多的。所以第一个、第三个和第四个应该总是完全可见的......但第二个应该隐藏不适合的东西。

这是我得到的最接近的(使用两个ul 控件,将第三项和第四项浮动在右侧),但是一旦我为第二项添加 CSS,一切都出错了。 (“错误”是指第二项换行到下一行,而不是留在同一行并显示...

#leftul {
    width:100%;
}
#rightul {
    float:right;
}
ul {
    margin:0;
    padding:0;
}
li {
    list-style-type:none;
    display:inline-block;
    border:1px solid black;
}
#leftul #leftlarge {
    overflow:hidden;
    white-space:nowrap;
    text-overflow:ellipsis;
}
<ul id="rightul">
    <li>Item Three</li>
    <li>Item Four</li>
</ul>
<ul id="leftul">
    <li>Item One</li>
    <li id="leftlarge">Item Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two</li>
</ul>

有人可以建议如何实现吗?

【问题讨论】:

    标签: html css


    【解决方案1】:

    这段代码运行良好,试试这个方法

    <div class="left">left</div>
    <div class="right">right</div>
    <div class="right">right</div>
    <div class="middle"><div  class="flexible_width1">Item Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two</div></div>
    

    CSS:

    .left{
        background:red;
        min-width:70px;
        float:left;
    
    }
    .middle{
        background:yellow;
    
    }
    .right{
        float:right;
        min-width:70px;
        background:green
    }
    .flexible_width1 {
        overflow:hidden;
        white-space:nowrap;
        text-overflow:ellipsis;
    
    }
    

    Demo

    【讨论】:

    • 感谢您的回答,但第 1、3、4 个项目中的文本会有所不同(它们并不总是相同,因此我无法设置固定宽度)
    • @freefaller 你可以对第 1、3、4 个项目使用 min-width
    • 想想你可能会在这里做点什么!
    • 如果要添加text-overflow:ellipsis;仅适用于一项,那么这将解决您的问题。
    • 优秀 - 正是我正在寻找的。谢谢:-)
    【解决方案2】:

    将此代码与 j 查询一起使用

    #table {
            display: table;
            width: 100%;
            table-layout: fixed;
        }
    
            #table > div {
                display: table-cell;
            }
    
                #table > div:first-child {
                    display: inline-block;
                    background: red;
                }
    
                #table > div:nth-child(2) {
                    width: 100%;
                    background: yellow;
                    overflow: hidden;
                    white-space: nowrap;
                    text-overflow: ellipsis;
                }
    
                #table > div:nth-child(3) {
                    display: inline-block;
                    background: aqua;
                }
    
                #table > div:nth-child(4) {
                    display: inline-block;
                    background: lightgray;
                }
    

    HTML

    <div id="table">
        <div>first</div>
        <div>second second second second second second second second </div>
        <div>third</div>
        <div>fourth</div>
    </div>
    

    jQuery

    $(document).ready(function () {
            var first_row = $('#table>div:first-child').width();
            $('#table>div:first-child').css('width', first_row + 'px')
            $('#table>div:first-child').css('display', 'table-cell')
    
            var first_row1 = $('#table>div:nth-child(3)').width();
            $('#table>div:nth-child(3)').css('width', first_row1 + 'px')
            $('#table>div:nth-child(3)').css('display', 'table-cell')
    
            var first_row2 = $('#table>div:nth-child(4)').width();
            $('#table>div:nth-child(4)').css('width', first_row2 + 'px')
            $('#table>div:nth-child(4)').css('display', 'table-cell')
    
        })
    

    【讨论】:

    • 如果您打算使用其他人答案中的代码作为您的答案的基础,至少要给他们信任!只是消化你写的东西
    • 这是一个非常有趣的想法...我目前正在考虑是否可以将其集成到我当前的解决方案中
    • 感谢您的回答,但已经提供了一个纯粹在 CSS 中完成的答案
    【解决方案3】:

    您必须将width 设置为元素。

    #leftul {
        width:100%;
    }
    #rightul {
        float:right;
    }
    ul {
        margin:0;
        padding:0;
    }
    li {
        list-style-type:none;
        display:inline-block;
        border:1px solid black;
    }
    #leftul #leftlarge {
        overflow:hidden;
        white-space:nowrap;
        text-overflow:ellipsis;
        width:40%;
    }
    <ul id="rightul">
        <li>Item Three</li>
        <li>Item Four</li>
    </ul>
    <ul id="leftul">
        <li>Item One</li>
        <li id="leftlarge">Item Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two</li>
    </ul>

    【讨论】:

    • 嗯...那可能是个问题。该区域应该是“全宽”,即在响应式设计中,第二项应该占据所有可用空间,第三项和第四项“锁定”在右侧
    • @freefaller 使用溢出:隐藏;表示宽度固定对吗?
    • 对不起@Lalji,我不明白。您是在提出问题,还是伪装成问题发表声明?
    【解决方案4】:

    如果locked 的意思是元素将具有fixed 宽度,那么您可以使用display:table/table-cell 来实现:

    body{margin: 0}
    
    #table{
        display:table;
        width:100%;
        table-layout: fixed;
    }
    
    #table>div{
        display:table-cell;
    }
    
    #table>div:first-child{
        width: 100px;
        background:red;
    }
    
    #table>div:nth-child(2){
        width: 100p%;
        background:yellow;
        overflow:hidden;
        white-space:nowrap;
        text-overflow:ellipsis;
    }
    
    #table>div:nth-child(3){
        width: 100px;
        background:aqua;
    }
    
    #table>div:nth-child(4){
        width: 100px;
        background:lightgray;
    }
    <div id="table">
        <div>first</div>
        <div>second second second second second second second second </div>
        <div>third</div>
        <div>fourth</div>
    </div>

    第二个单元格总是占据剩余的全部宽度。

    Check the fiddle to se on responsive

    【讨论】:

    • 不幸的是,我似乎一直含糊不清 - 当我的意思是锁定时,我的意思是锁定在全宽区域的左侧或右侧。第 1/3/4 个项目将具有可变长度的内容。但这更接近......如果我能弄清楚如何让它与可变内容一起工作
    • @freefaller “可变内容”是什么意思?
    • 对不起@Vucko,看来我的解释很糟糕。这个“全宽区域”将在响应式设计中使用。这四个面板将在不同情况下保存不同(可变)的文本,它们永远不会是固定的文本,因此我不可能在它们上设置特定的宽度。我需要第 1、第 3 和第 4 项尽可能小,但文本完全可见 - 然后第 2 项占用其余空间,隐藏不适合的内容(最好使用 ... 省略号. 这样更有意义吗?
    • @freefaller 好吧,我不相信它只能用 CSS 来完成。每天你都会学到一些新东西:)
    • 正如我已故祖母常说的,“如果你不小心,你每天都会学到新东西!” ;-)
    【解决方案5】:

    也许这可以帮助你:jsFiddle

    <div id="navcontainer">
        <ul>
            <li>Item One</li>
            <li>Item Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two</li>
            <li>Item Three</li>
            <li>Item Four</li>
        </ul>
    </div>
    
    #navcontainer ul
    {
        margin: 0;
        padding: 0;
        list-style-type: none;
        width: 100%;
    }
    
    #navcontainer ul li 
    { 
        display: inline-block;
        border:1px solid black; 
    
        overflow:hidden;
        text-overflow:ellipsis;
        white-space: nowrap;
        max-width: 150px;
    }
    

    这应该是你要找的:)

    【讨论】:

    • 感谢您的回答,但它既不会占用屏幕/区域的整个宽度,也不会扩展第二项以使用所有可用空间。
    猜你喜欢
    • 2018-07-22
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    • 2015-09-06
    • 2020-12-10
    • 1970-01-01
    • 2013-04-01
    • 2017-01-21
    相关资源
    最近更新 更多