Bootstrap 4'text-truncate'类将文本放置在下面

时间:2019-04-09 09:57:23

标签: html css laravel bootstrap-4

如果标题太长,我会尝试在标题上添加省略号。我想尊重响应式布局,也不想在 card-title 中添加固定宽度。

我使用 text-truncate 在卡片标题上添加省略号。

卡片位于一栏中,以使用文字左侧的图像。

enter image description here

<div class="container">
    <div class="row">
        @foreach($posts as $post)
        <div class="col-lg-6 p-1">
            <div class="card border-0">
                <div class="row no-gutters">
                    <div class="col-auto">
                        <img src="{{ $post->featured_image ? $post->featured_image : '/images/placeholder-image.png' }}" class="img-fluid" width="150" height="150" alt="{{ $post->title }}" style="width: 150px; height: 150px; object-fit: cover;">
                    </div>
                    <div class="col">
                        <div class="card-body">
                            <h3 class="card-title text-truncate">{{ $post->title }}</h3>
                            <p class="card-text m-0">{{ $post->short_summary }}</p>
                            <small>{{ $post->published_at->format('M d') }} — {{ $post->read_time }}</small>
                        </div>
                    </div>
                    <a href="{{ route('post', $post->slug) }}" class="stretched-link"></a>
                </div>
            </div>
        </div>
        @endforeach
    </div>
</div>

但是,如果我添加 text-truncate 类,则标题和内容就会减少。

enter image description here

3 个答案:

答案 0 :(得分:0)

在内部flex-nowrap上使用row。这样可以防止colcol-auto包装。

     <div class="col-lg-6 p-1">
            <div class="card border-0">
                <div class="row no-gutters flex-nowrap">
                    <div class="col-auto">
                        <img src="//placehold.it/400" class="img-fluid" width="150" height="150" alt="" style="width: 150px; height: 150px; object-fit: cover;">
                    </div>
                    <div class="col">
                        <div class="card-body">
                            <h3 class="card-title text-truncate">title is longer</h3>
                            <p class="card-text m-0">summary</p>
                            <small>read time</small>
                        </div>
                    </div>
                    <a href=".." class="stretched-link"></a>
                </div>
            </div>
     </div>

https://www.codeply.com/go/szhxXTLETa

答案 1 :(得分:0)

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

<div class="container">
   
    <div class="card">
        <div class="row no-gutters">
            <div class="col-2">
                <img src="//placehold.it/200" class="img-fluid" alt="">
            </div>
            <div class="col-4">
                <div class="card-block px-2">
                    <h4 class="card-title text-truncate">Title</h4>
                    <p class="card-text text-truncate">Description</p>
                   
                </div>
            </div>
            
            <div class="col-2">
                <img src="//placehold.it/200" class="img-fluid" alt="">
            </div>
            <div class="col-4">
                <div class="card-block px-2">
                    <h4 class="card-title text-truncate">Title long textlong textlong textlong text</h4>
                    <p class="card-text text-truncate">long textlong textlong textlong textlong text</p>
                   
                </div>
            </div>
        </div>
        
    </div>
</div>

答案 2 :(得分:-1)

您在overflow: hidden;上缺少<div class="col">。如果在使用flexbox时未将其父容器的溢出设置为隐藏,则内容将始终采用其全部宽度。

.col-special {
  overflow: hidden;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<div class="container">
    <div class="row">
        <div class="col-lg-6 p-1">
            <div class="card border-0">
                <div class="row no-gutters">
                    <div class="col-auto">
                        <img src="https://via.placeholder.com/150x150" class="img-fluid" width="150" height="150" alt="{{ $post->title }}" style="object-fit: cover;">
                    </div>
                    <div class="col col-special">
                        <div class="card-body">
                            <h3 class="card-title text-truncate">Comment écrire une "story" complète ?</h3>
                            <p class="card-text m-0">Une story, c'est en faite, simplement une histoire qui racoute ce que vous voule...</p>
                            <small>Apr 07 — 1 min read</small>
                        </div>
                    </div>
                    <a href="{{ route('post', $post->slug) }}" class="stretched-link"></a>
                </div>
            </div>
        </div>
    </div>
</div>