Bootstrap4 text-truncate致标题内容下移,如何实现响应式标题省略号?
text-truncate on Card Titles I see the issue you're facing: adding the text-truncate class to your card title is causing the title and associated content to shift downward. Let's break down why this happens and how to fix it while keeping your responsive, left-image-right-text card layout intact.
Why the Shift Happens
Your card uses a row no-gutters flex container to align the image and content columns. By default, Bootstrap's .row uses align-items: stretch, which forces both columns (the image column and content column) to match the height of the tallest element—your 150px tall image.
The content column contains a .card-body which has default padding (1.25rem), so when the column is stretched to match the image height, the padding pushes your title and content downward relative to the image, creating that shifted look. Additionally, text-truncate enforces white-space: nowrap, which changes how the title occupies vertical space compared to a multi-line title, exacerbating the shift.
Solution
We'll fix this by adjusting the flex alignment of the row and ensuring the text-truncate works correctly without breaking the layout. Here's the modified code:
<div class="container"> <div class="row"> @foreach($posts as $post) <div class="col-lg-6 p-1"> <div class="card border-0 position-relative"> <!-- Ensure parent is relative for stretched-link --> <div class="row no-gutters align-items-start"> <!-- Align items to top instead of stretching --> <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 d-flex flex-column"> <!-- Use flex column for proper content spacing --> <h3 class="card-title text-truncate mb-2">{{ $post->title }}</h3> <!-- Add mb-2 for consistent spacing --> <p class="card-text m-0 mb-1">{{ $post->short_summary }}</p> <small class="text-muted">{{ $post->published_at->format('M d') }} — {{ $post->read_time }}</small> </div> </div> </div> <a href="{{ route('post', $post->slug) }}" class="stretched-link"></a> </div> </div> @endforeach </div> </div>
Key Changes Explained
align-items-starton the inner row: This tells the flex container to align columns to the top instead of stretching them to match the image height. Your content will now sit flush with the top of the image, eliminating the downward shift.d-flex flex-columnon.card-body: This ensures the content inside the card body is laid out vertically with consistent spacing, preventing any unexpected layout jumps from the truncated title.- Added margin utilities (
mb-2,mb-1): These replace the default spacing with explicit Bootstrap utilities to keep content spacing consistent, even when the title is truncated to a single line. - Explicit
position-relativeon.card: Ensures thestretched-linkworks correctly to make the entire card clickable.
Bonus: Ensuring text-truncate Works Reliably
Since you don't want to set a fixed width on .card-title, the col class on the content column already provides a responsive width constraint (it takes up the remaining space in the flex row). The text-truncate class will work here because:
.card-title(an<h3>) is a block-level element by default- The parent
.card-body(and its parentcol) have a defined width from the flex layout text-truncateappliesoverflow: hidden,text-overflow: ellipsis, andwhite-space: nowrapto handle the truncation
This setup keeps your layout fully responsive while fixing the content shift issue.
内容的提问来源于stack exchange,提问作者pirmax




