如何在bootstrap响应表中使用省略号

sib*_*age 16 html css css-position twitter-bootstrap twitter-bootstrap-3

text-overflow:ellipsis数据增加时,响应表不起作用th(随着col-xs-2宽度的增加).

在此输入图像描述

代码如下:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

<div class="table-responsive">
  <table class="table">
    <thead>
      <tr>
        <th class="col-xs-2" style="text-overflow: ellipsis;">Lorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem Ipsum</th>
        <th class="col-xs-1">Firstname</th>
        <th class="col-xs-1"> Lastname</th>
        <th class="col-xs-4">Age</th>
        <th class="col-xs-2">City</th>
        <th class="col-xs-2">Country</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>1</td>
        <td>Anna</td>
        <td>Pitt</td>
        <td>35</td>
        <td>New York</td>
        <td>USA</td>
      </tr>
    </tbody>
  </table>
</div>
Run Code Online (Sandbox Code Playgroud)

kuk*_*kuz 33

文本溢出属性仅影响在其内联进度方向MDN中溢出块容器元素的内容

为了text-overflow工作,text-overflow: ellipsis单独指定不会有任何好处 - 您应该一起使用以下样式:

overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
Run Code Online (Sandbox Code Playgroud)

span, div, th, td {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  max-width: 100px;
}
Run Code Online (Sandbox Code Playgroud)
<span>Inline element overflow ellipsis do not work</span>
<div>Block elements overflow ellipsis works</div>
<table>
  <tr><th>Table - Overflow test</th></tr>
  <tr><td>This is a long text</td><td>This is a long text</td></tr>
</table>
Run Code Online (Sandbox Code Playgroud)

表格布局中的文本溢出

所以text-overflow适用于block元素但是td是一个table-cell元素 - 表总是很难处理,因为它们是使用默认的表格布局算法渲染的.调整表格及其单元格的宽度以适合其内容.

  1. 通常指定获取省略号的常用属性可能有效:

    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    
    Run Code Online (Sandbox Code Playgroud)
  2. 如果它们不起作用或者您开始​​看到表格算法对您进行欺骗,那么您可以将它们一起使用max-width: 0

    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    max-width: 0;
    
    Run Code Online (Sandbox Code Playgroud)

    .table .text {
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      max-width: 0;
    }
    Run Code Online (Sandbox Code Playgroud)
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
      <div class="table-responsive">
        <table class="table">
          <thead>
            <tr>
              <th class="col-xs-2 text">
              Lorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem Ipsum
              </th>
              <th class="col-xs-1">Firstname</th>
              <th class="col-xs-1">Lastname</th>
              <th class="col-xs-4">Age</th>
              <th class="col-xs-2">City</th>
              <th class="col-xs-2">Country</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>1</td>
              <td>Anna</td>
              <td>Pitt</td>
              <td>35</td>
              <td>New York</td>
              <td>USA</td>
            </tr>
          </tbody>
        </table>
      </div>
    Run Code Online (Sandbox Code Playgroud)

  3. 另一个是包装在一个文本span定位absolute内部的tdwidth: 100%沿着与inline-block 伪构件.

    .table .text {
      position: relative;
    }
    
    .table .text span {
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      position: absolute;
      width: 100%;
    }
    
    .text:before {
      content: '';
      display: inline-block;
    }
    Run Code Online (Sandbox Code Playgroud)
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
      <div class="table-responsive">
        <table class="table">
          <thead>
            <tr>
              <th class="col-xs-2 text">
                <span>
              Lorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem Ipsum</span>
              </th>
              <th class="col-xs-1">Firstname</th>
              <th class="col-xs-1">Lastname</th>
              <th class="col-xs-4">Age</th>
              <th class="col-xs-2">City</th>
              <th class="col-xs-2">Country</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>1</td>
              <td>Anna</td>
              <td>Pitt</td>
              <td>35</td>
              <td>New York</td>
              <td>USA</td>
            </tr>
          </tbody>
        </table>
      </div>
    Run Code Online (Sandbox Code Playgroud)


yea*_*lad 15

从Bootstrap 4开始,您可以使用.text-truncate该类执行以下操作.

<th class="col-xs-2 d-inline-block text-truncate" style="max-width: 150px;">
Run Code Online (Sandbox Code Playgroud)

https://getbootstrap.com/docs/4.0/utilities/text/#text-wrapping-and-overflow


小智 8

在 Bootstrap 4 中,您可以使用该类.text-truncate。它会自动为相关组件添加这些样式。

.text-truncate{
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
Run Code Online (Sandbox Code Playgroud)

然后您可以设置元素的最大宽度以获得更可靠的输出。