溢出:最后隐藏的点

时间:2009-01-28 05:27:34

标签: html css

假设我有一个字符串“我喜欢大屁股而且我不能撒谎”并且我用overflow:hidden剪切它,所以它显示如下:

  

我喜欢大屁股而且我很喜欢

切断文字。是否可以这样显示:

  

我喜欢大屁股,我喜欢......

使用CSS?

10 个答案:

答案 0 :(得分:207)

您可以使用text-overflow: ellipsis;所有主流浏览器都支持according to caniuse

这是jsbin上的a demo

.cut-text { 
  text-overflow: ellipsis;
  overflow: hidden; 
  width: 160px; 
  height: 1.2em; 
  white-space: nowrap;
}
<div class="cut-text">
I like big buts and I can not lie.
</div>

答案 1 :(得分:75)

请检查以下代码段以解决您的问题

&#13;
&#13;
div{
  width : 100px;
  overflow:hidden;
  display:inline-block;
  text-overflow: ellipsis;
  white-space: nowrap;
}
&#13;
<div>
    The Alsos Mission was an Allied unit formed to investigate Axis scientific developments, especially nuclear, chemical and biological weapons, as part of the Manhattan Project during World War II. Colonel Boris Pash, a former Manhattan P
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:12)

是的,通过CSS 3中的text-overflow属性。告诫:它在浏览器中尚未得到普遍支持。

答案 3 :(得分:10)

如果要将线条限制为3,请尝试此操作,三条线条后将显示这些点。如果我们想要增加行,只需更改-webkit-line-clamp值并给出div大小的宽度。

div {
   display: -webkit-box;
   -webkit-line-clamp: 3;
   -webkit-box-orient: vertical;
   overflow: hidden;
   text-overflow: ellipsis;
}

答案 4 :(得分:6)

<style>
    .dots
    {
        display: inline-block;
        width: 325px;
        white-space: nowrap;
        overflow: hidden !important;
        text-overflow: ellipsis;
    }

    .dot
    {
        display: inline-block;
        width: 185px;
        white-space: nowrap;
        overflow: hidden !important;
        text-overflow: ellipsis;
    }
</style>

答案 5 :(得分:6)

希望对您有帮助:

.text-with-dots {
    display: block;
    max-width: 98%;
    white-space: nowrap;
    overflow: hidden !important;
    text-overflow: ellipsis;
}
<div class='text-with-dots'>Some texts here Some texts here Some texts here Some texts here Some texts here Some texts here </div>

答案 6 :(得分:4)

bootstrap 4 中,您可以添加.text-truncate类以用省略号截断文本。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>

<!-- Inline level -->
<span class="d-inline-block text-truncate" style="max-width: 190px;">
  I like big butts and I cannot lie
</span>

答案 7 :(得分:3)

大多数解决方案都使用静态宽度。但出于某些原因,它有时可能是错误的。

示例:我的表有很多列。其中大多数是窄的(静态宽度)。但主列应尽可能宽(取决于屏幕大小)。

HTML:

<table style="width: 100%">
  <tr>
    <td style="width: 60px;">narrow</td>
    <td>
      <span class="cutwrap" data-cutwrap="dynamic column can have really long text which can be wrapped on two rows, but we just need not wrapped texts using as much space as possible">
        dynamic column can have really long text which can be wrapped on two rows
        but we just need not wrapped texts using as much space as possible
      </span>
    </td>
  </tr>
</table>

CSS:

.cutwrap {
  position: relative;
  overflow: hidden;
  display: block;
  width: 100%;
  height: 18px;
  white-space: normal;
  color: transparent !important;
}
.cutwrap::selection {
  color: transparent !important;
}
.cutwrap:before {
  content: attr(data-cutwrap);
  position: absolute;
  left: 0;
  right: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: #333;
}
/* different styles for links */
a.cutwrap:before {
  text-decoration: underline;
  color: #05c;
}

答案 8 :(得分:1)

<!DOCTYPE html>
<html>
<head>
<style>
.cardDetaileclips{
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 3; /* after 3 line show ... */
    -webkit-box-orient: vertical;
}
</style>
</head>
<body>
<div style="width:100px;">
  <div class="cardDetaileclips">
    My Name is sonia you can call me sonu.
  </div>
</div> 
</body>
</html>

答案 9 :(得分:-1)

在加载时隐藏文本并在悬停时显示

<span class="hide-text"> How to hide text by dots and show text on hover</span>

.hide-text {
    width: 70px;
    display: inline-block;
    white-space: nowrap;
    overflow: hidden !important;
    text-overflow: ellipsis;
  }

  span:hover {
     white-space: unset;
     text-overflow: unset;
  }
相关问题