【问题标题】:How to use text-overflow inside Bootstrap column?如何在 Bootstrap 列中使用文本溢出?
【发布时间】:2016-01-14 21:17:24
【问题描述】:

假设我有一个固定高度的行,我在它的列中插入了一些文本。如果它太长,我想将其切断,并在行尾添加三个点,如下所示:

我在我的行中为此使用 text-overflow: ellipsis; 属性,但无法使其正常工作。

JsFiddle

我做错了什么?

HTML

<div class="container">
  <div class="row text">    
    <div class="col-xs-4" style="border: solid">
 Some really long text!  Some really long text!  Some really long text!  Some really long text! The end! Some really long text! The end! Some really long text! The end! Some really long text! The end!
    </div>
  </div>        
</div>

CSS

.row {
    margin: 2px 0;
}

.text {
    word-wrap: break-word;
    height: 50px;
    text-overflow: ellipsis;
}

【问题讨论】:

    标签: twitter-bootstrap css twitter-bootstrap-3 overflow


    【解决方案1】:

    如果您想使用 css 执行此操作,请尝试以下代码:

    HTML:

    <div class="container">
      <div class="row">    
        <div class="col-xs-4" style="border: solid">
            <div class="text">
                Some really long text!  Some really long text!  Some really long text!  Some really long text! The end! Some really long text! The end! Some really long text! The end! Some really long text! The end!
            </div>
        </div>
      </div>        
    </div>
    

    CSS:

    .row {
        margin: 2px 0;
    }
    
    .text {
        display: block;
        display: -webkit-box;
        max-width: 400px;
        height: 50px;
        margin: 0 auto;
        line-height: 1.2;
        -webkit-line-clamp: 3;
        -webkit-box-orient: vertical;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    

    这里是jsfiddle

    为了理解 css 中的换行符Line Clamping

    【讨论】:

    • jsfiddle 似乎没有消失
    【解决方案2】:

    定义您的 CSS 类并将其分配给您分配 col 大小的 div,例如:-

    <div class="container">
      <div class="row">    
        <div class="col-xs-4 trimText" style="border: solid">
    
                Some really long text!  Some really long text!  Some really long text!  Some really long text! The end! Some really long text! The end! Some really long text! The end! Some really long text! The end!
    
        </div>
      </div>        
    </div>
    

    “trimText”CSS 在哪里:-

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

    Output Image:

    【讨论】:

      【解决方案3】:

      尝试使用 js 来做。它将仅显示 50 个字符。 更新的小提琴是 - http://jsfiddle.net/y6oatnzy/3/

      $(document).ready(function() {
          var showChar = 50;
          $('.more').each(function() {
              var content = $(this).html();
      
              if(content.length > showChar) {
      
                  var c = content.substr(0, showChar);
                  var h = content.substr(showChar-1, content.length - showChar);
      
                  var html = c +  '..';
      
                  $(this).html(html);
              }  
          });   
      });
      

      【讨论】:

      • JS 应该只用于功能性工作,CSS 可以自己处理。最佳经验法则:HTML = 结构,CSS = 样式,JS = 操作
      猜你喜欢
      • 1970-01-01
      • 2016-06-01
      • 2012-07-04
      • 2013-02-24
      • 2015-04-23
      • 1970-01-01
      • 2022-01-23
      • 2015-05-13
      • 2020-10-30
      相关资源
      最近更新 更多