CSS文本省略号和100%宽度

时间:2014-12-05 10:47:06

标签: html css

我有以下文本容器:

<div class="cardTitles">

<div class="title">
    <div class="titleContent">
        <i class="fa fa-star-o"></i>
        <b>Some long long long title here</b>
        <a href="some href"></a>
    </div>
</div>

... title divs ...

</div>

CSS

.cardTitles {
    width: 100%;
    height: 100%;
}

.title
{
    position: relative;

    padding-top: 8px;
    padding-bottom: 8px;
}

我想使文本全宽+文本溢出:省略号。因此,当我调整浏览器大小时,所有title div应该是父cardTitles的100%宽度,最后剪切文本为三个点。

我发现这可以使用flex。这是可能的answer

.parent-div {
    display: flex;
}

.text-div {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;    

    min-width: 0;
}

但这对我不起作用。父容器在达到某个子标题的最大长度时停止调整大小。


您可以在此处找到示例:http://88.198.106.150/tips/cpp/

尝试调整浏览器大小并使用文字What does it mean that a reference must refer to an object, not a dereferenced NULL pointer查看标题。我需要让它溢出省略号。

4 个答案:

答案 0 :(得分:16)

试试这个:

.titleContent {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

此处&{39}和updated jsFiddle

答案 1 :(得分:6)

宽度需要以像素为单位添加,百分比不会与下面给出的其他三个属性一起使用: -

.text-ellipsis{
    max-width: 160px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

答案 2 :(得分:1)

#div1 {
   white-space: nowrap; 
   width: 12em; 
   overflow: hidden;
   text-overflow: clip; 
   border: 1px solid #000000;
}

 #div2 {
   white-space: nowrap; 
   width: 12em; 
   overflow: hidden;
   text-overflow: ellipsis; 
   border: 1px solid #000000;
}

以下两个div包含一个不适合该框的长文本。如您所见,文本被剪裁。

这个div使用&#34;文本溢出:clip&#34;:

这是一些不适合框的长文本

这个div使用&#34;文本溢出:省略号&#34;:

这是一些不适合框的长文本

答案 3 :(得分:0)

没有Flex,但也可以工作...尝试。

.element {
  display: table;
  table-layout: fixed;
  width: 100%;
}

.truncate {
  display: table-cell;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}

text-overflow ellipsis with 100% width