【问题标题】:HTML table truncate text but fit as much as possibleHTML 表格截断文本但尽可能适合
【发布时间】:2016-12-31 02:49:36
【问题描述】:

我在 Stack Overflow 中找到了一些帖子,但它对我不起作用。我需要一些具体的帮助。

这是我的board 页面:

当我输入长标题帖子时,它看起来像这样:

正如您在此处看到的,它破坏了每个表格单元格的宽度,以及不被截断的文本。\

我想做什么:

  1. 如果文本到达title 字段的末尾,它应该被截断
  2. 任何东西都不应破坏表格格式(宽度..等)

这是我的html 代码(用于django):

{% extends 'chacha_dabang/skeleton/base.html' %}

{% block content %}
    <div class="container inner">
        <div class="row">
            <div class="col-md-8 col-sm-9 center-block text-center">
                <header>
                    <h1> 차차다방 게시판 </h1>
                    <p> 회원들의 게시글을 볼 수 있는 페이지 입니다.</p>
                </header>
            </div><!-- /.col -->
        </div><!-- /.row -->
    </div><!-- /.container -->

    <div class="container inner-bottom">
        <div class="table-responsive">
            <table class="table">
                <col width="65%">
                <col width="15%">
                <col width="13%">
                <col width="7%">
                <thead>
                    <tr>
                        <th>제 목</th>
                        <th>작성자</th>
                        <th>작성일</th>
                        <th>조회수</th>
                    </tr>
                </thead>
                <tbody>
                    {% for post in posts %}
                    <tr>
                        <td class="td-title-area"> <a href="{{ post.get_absolute_url }}" class="td-title"> {{ post.title}} </a></td>
                        <td> {{post.author}} </td>
                        <td> {{post.created_at|date:"SHORT_DATE_FORMAT"}} </td>
                        <td> 11 </td>
                    </tr>
                    {% endfor%}
              </tbody>
            </table>
        </div>

        <br>
        <br>

        {% if is_paginated %}
        <div class="pagination text-center" style="position:center;">
            <span class="page-links">
                {% if page_obj.has_previous %}
                    <a href="{% url 'posts:list' %}?page={{ page_obj.previous_page_number }}">previous</a>
                {% endif %}
                <span class="page-current">
                    Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
                </span>
                {% if page_obj.has_next %}
                    <a href="{% url 'posts:list' %}?page={{ page_obj.next_page_number }}">next</a>
                {% endif %}
            </span>
        </div>
        {% endif %}
    </div>

{% endblock %}

需要你的帮助。谢谢

编辑

这是我想要做的: 1.我创建了一个名为truncate的类并为它定义了css

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

并将truncate 类添加到我的表中:

<tbody>
    {% for post in posts %}
    <tr>
        <td class="td-title-area">
            <a href="{{ post.get_absolute_url }}" class="td-title truncate">   
                {{ post.title}}
            </a>
        </td>
        <td> {{post.author}} </td>
        <td> {{post.created_at|date:"SHORT_DATE_FORMAT"}} </td>
        <td> 11 </td>
    </tr>
    {% endfor%}
</tbody>

结果是:

【问题讨论】:

标签: html css django


【解决方案1】:

你可以试试这样的:

    .td-title-area {
      position: relative;
    }

    .td-title-area a {
      position: absolute;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
<table class="table">
                <col width="65%">
                <col width="15%">
                <col width="13%">
                <col width="7%">
                <thead>
                    <tr>
                        <th>제 목</th>
                        <th>작성자</th>
                        <th>작성일</th>
                        <th>조회수</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td class="td-title-area"> <a href="" class="td-title"> sdkjghbdslkjgbshdfjgbsdfjkhgbsdfjkghbsdgsdfhjkgbsdfkjhgbsdfkhjgbsdfkjghbsfdkjhg </a></td>
                        <td> author </td>
                        <td> date </td>
                        <td> 11 </td>
                    </tr>
                  <tr>
                        <td class="td-title-area"> <a href="" class="td-title"> sdkjg </a></td>
                        <td> author </td>
                        <td> date </td>
                        <td> 11 </td>
                    </tr>
                  </tbody>
                </table>

【讨论】:

  • 效果很好!你看到我的编辑部分了吗?唯一的区别是position。我无法清楚地理解为什么它依赖于 position
  • 我更改了示例,现在示例中的标题更短,看起来它仍然有效,除非我误解了你。定位将其从文档流中取出,使内容不控制单元格的宽度。
  • 哦,是的,它有效。那是我的错误。我在css 的某个地方为td 定义了text-align:center。谢谢:)
猜你喜欢
  • 2011-07-11
  • 1970-01-01
  • 1970-01-01
  • 2013-07-07
  • 2020-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多