如何用CSS实现文本第3/4行后显示省略号及「查看更多」展开功能
Got it, let's break this down step by step. We'll use CSS for the multi-line truncation (with a little trick to get the "查看更多" link right next to the ellipsis) and vanilla JavaScript to handle the expand/collapse functionality. No fancy frameworks needed—just plain, reliable code.
Step 1: CSS for Multi-Line Truncation
First, we'll leverage the webkit-line-clamp property to control the line limit (works in most modern browsers). The key is wrapping the text in a truncatable container, then positioning the "查看更多" link so it sits seamlessly after the ellipsis.
/* Main content wrapper */ .mycontent { position: relative; width: 100%; /* Adjust to your desired width */ max-height: calc(1.5em * 3); /* 1.5em = line height, 3 = target line count */ overflow: hidden; line-height: 1.5em; } /* Truncated text container */ .mycontent .truncated-text { display: -webkit-box; -webkit-line-clamp: 3; /* Change to 4 for 4-line truncation */ -webkit-box-orient: vertical; overflow: hidden; margin: 0; } /* "查看更多" link styling */ .mycontent .read-more { position: absolute; bottom: 0; right: 0; background: #fff; /* Match your page background to cover truncated text */ padding-left: 0.5em; cursor: pointer; color: #007bff; /* Adjust link color to fit your design */ } /* Hide link when content is expanded */ .mycontent.expanded .read-more { display: none; } /* Show full content when expanded */ .mycontent.expanded .truncated-text { display: block; -webkit-line-clamp: unset; max-height: none; }
Step 2: JavaScript for Toggle Functionality
We need a simple script to toggle an expanded class when the link is clicked. This switches between truncated and full content views, and even updates the link text for better UX.
// Grab all content containers const contentContainers = document.querySelectorAll('.mycontent'); contentContainers.forEach(container => { const readMoreBtn = container.querySelector('.read-more'); const truncatedText = container.querySelector('.truncated-text'); // Only show the button if text is actually longer than the line limit const needsTruncation = truncatedText.scrollHeight > truncatedText.clientHeight; if (!needsTruncation) { readMoreBtn.style.display = 'none'; return; } // Add click handler for expand/collapse readMoreBtn.addEventListener('click', () => { container.classList.toggle('expanded'); // Update button text (optional but user-friendly) readMoreBtn.textContent = container.classList.contains('expanded') ? '收起' : '查看更多'; }); });
Step 3: Full HTML Structure
Here's how to structure your HTML to work with the above CSS and JS:
<div class="mycontent"> <p class="truncated-text"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </p> <span class="read-more">查看更多</span> </div>
Quick Tips
- Adjust
line-heightandmax-heightin.mycontentto match your design's typography. - Swap
-webkit-line-clamp: 3to4if you want truncation after 4 lines instead of 3. - The script checks if text needs truncation before showing the link—so short content won't have an unnecessary button.
- Make sure the
backgroundcolor on.read-morematches your page's background, so it covers any truncated text underneath.
内容的提问来源于stack exchange,提问作者hahalol

