【问题标题】:Truncate text with css用 css 截断文本
【发布时间】:2017-05-16 10:37:14
【问题描述】:

我遇到了文本截断的问题。当我试图缩小窗口时它不会被截断。带有 LONG TEXT 的文件会缩小,直到按钮遇到其中的最后一个字母并且不会进一步截断。没有三点的东西只会停在字段内的最后一个字母处。有点像这样 -

我在 html 布局中有这样的东西:

在所有其他

.Label:not(textarea){
  overflow:hidden;
  text-overflow:ellipsis;
  white-space:nowrap;
}

.input-group {
    position: relative;
    display: table;
    border-collapse: separate;
}

.input-group-btn {
    position: relative;
    font-size: 0;
    white-space: nowrap;
    display: table-cell;
}
<div class="input-group">
    <input type="hidden" id="field_19" value="123">
    <input type="hidden" id="field_18" value="456">
    <span class="Label" title="Some Text">SOME LONG LONG LONG LONG TEXT</span>
    <span class="input-group-btn ev-input-group input-group">
        <button type="button" class="edit_button" data-reditcontroller="some url">btn
            <i class="f-edit"></i>
        </button>
    </span>
</div>

    

【问题讨论】:

标签: html css


【解决方案1】:

出现这种奇怪行为的原因是您没有定义 ... 应该开始的 WIDTH。并确保它是display:inline-block,以确保宽度是可定义的。

CSS

  .Label:not(textarea){
      overflow:hidden;
      width: 90%; //can also use calc function as per Yudi's answer
      display: inline-block;
      text-overflow:ellipsis;
      white-space:nowrap;
    }

【讨论】:

  • 问题是宽度必须适应窗口大小。它必须改变。
  • @JDoeBloke 有多种方法可以做到这一点。如果 dom 占据了整个宽度,那么您可以使用百分比来使其响应。您也可以按照 Yudi Chang 的回答使用。如果您也在网格系统中使用它,我的也会有响应。但请确保在使用我的代码时,您还有一个 100% 宽度的包装器:)
  • @JDoeBloke 哦,我已根据要求进行了编辑。
【解决方案2】:

.Label:not(textarea){
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: calc(100vw - 45px);
    display: block;
}

.input-group {
    position: relative;
    display: table;
    border-collapse: separate;
}

.input-group-btn {
    position: relative;
    font-size: 0;
    white-space: nowrap;
    display: table-cell;
}
<div class="input-group">
    <input type="hidden" id="field_19" value="123">
    <input type="hidden" id="field_18" value="456">
    <span class="Label" title="Some Text">SOME LONG LONG LONG LONG TEXT</span>
    <span class="input-group-btn ev-input-group input-group">
        <button type="button" class="edit_button" data-reditcontroller="some url">btn
            <i class="f-edit"></i>
        </button>
    </span>
</div>

【讨论】:

    【解决方案3】:

    .Label:not(textarea){
      overflow:hidden;
      text-overflow:ellipsis;
      white-space:nowrap;
      display:inline-block;
      width:150px;
    }
    
    .input-group {
        position: relative;
        display: table;
        border-collapse: separate;
    }
    
    .input-group-btn {
        position: relative;
        font-size: 0;
        white-space: nowrap;
        display: table-cell;
    }
    <div class="input-group">
        <input type="hidden" id="field_19" value="123">
        <input type="hidden" id="field_18" value="456">
        <span class="Label" title="Some Text">SOME LONG LONG LONG LONG TEXT</span>
        <span class="input-group-btn ev-input-group input-group">
            <button type="button" class="edit_button" data-reditcontroller="some url">btn
                <i class="f-edit"></i>
            </button>
        </span>
    </div>

    【讨论】:

      【解决方案4】:
      1. 限制.Label 的宽度,使其适合max-width: 100% 的窗口。
      2. 为了使上述操作生效,您需要将.Label 上的display 设置为blockinline-block。与您已有的最接近的是inline-block
      3. .input-group 中删除display: table。我认为这里没有必要,因为您使用 div 而不是 table

      .Label:not(textarea) {
        display: inline-block;
        max-width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      
      .input-group {
        position: relative;
        border-collapse: separate;
      }
      
      .input-group-btn {
        position: relative;
        font-size: 0;
        white-space: nowrap;
        display: table-cell;
      }
      <div class="input-group">
        <input type="hidden" id="field_19" value="123">
        <input type="hidden" id="field_18" value="456">
        <span class="Label" title="Some Text">SOME LONG LONG LONG LONG LONG LONG LONG LONG LONG LONG LONG LONG LONG LONG LONG LONG LONG TEXT</span>
        <span class="input-group-btn ev-input-group input-group">
              <button type="button" class="edit_button" data-reditcontroller="some url">btn
                  <i class="f-edit"></i>
              </button>
          </span>
      </div>

      【讨论】:

        猜你喜欢
        • 2013-02-23
        • 2014-04-17
        • 1970-01-01
        • 1970-01-01
        • 2012-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多