About this snippet

Keep your layouts clean by truncating long blocks of text. Users can click "Read More" to expand the full content, which is perfect for blog post excerpts, reviews, or product descriptions.

Size
1.7 KB
Uses
HTML, CSS, JavaScript
Loads
Nothing external
Views
394
<div class="text-card">
  <h3>About Us</h3>
  <p class="text-content collapsed" id="textContent">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident.
  </p>
  <button id="toggleBtn" class="read-more-btn">Read More</button>
</div>
.text-card {
  font-family: system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  background: #fff;
  padding: 30px;
  border-radius: 12px;
  box-shadow: 0 4px 15px rgba(0,0,0,0.05);
  max-width: 400px;
  margin: 0 auto;
}
h3 {
  margin-top: 0;
  color: #1f2937;
}
.text-content {
  color: #4b5563;
  line-height: 1.6;
  overflow: hidden;
  transition: max-height 0.3s ease;
}
.text-content.collapsed {
  max-height: 60px;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  text-overflow: ellipsis;
}
.read-more-btn {
  background: none;
  border: none;
  color: #2563eb;
  font-weight: 600;
  padding: 0;
  margin-top: 10px;
  cursor: pointer;
  font-size: 0.9rem;
}
.read-more-btn:hover {
  text-decoration: underline;
}
const content = document.getElementById("textContent");
const btn = document.getElementById("toggleBtn");

btn.addEventListener("click", () => {
  if (content.classList.contains("collapsed")) {
    content.classList.remove("collapsed");
    btn.innerText = "Read Less";
  } else {
    content.classList.add("collapsed");
    btn.innerText = "Read More";
  }
});
Esc
↑↓ moveEnter select

Start from a template

Open an .html file, or HTML, CSS, JS, Markdown, Less or TypeScript files. You can also drop them here. Styles and scripts go into their panes.

Share

Source

Keyboard shortcuts

Save