To truncate text while preserving readability by avoiding word truncation, you can use a JavaScript solution that works with Webflow elements. Here's how you can achieve this for H3 titles with the class ".longtrend":
<script>
document.addEventListener("DOMContentLoaded", function() {
const textElements = document.querySelectorAll('.longtrend');
textElements.forEach(function(element) {
let text = element.textContent;
let truncatedText = truncateText(text, 100); // limit to 100 characters or adjust as needed
element.textContent = truncatedText;
});
function truncateText(text, limit) {
if (text.length > limit) {
const shortText = text.substring(0, limit).split(' ');
shortText.pop(); // remove the last word to avoid incomplete word
return shortText.join(' ') + '…';
}
return text;
}
});
</script>
To truncate text for H3 titles with the ".longtrend" class in Webflow, use the above script to avoid truncating in the middle of a word. Adjust the character limit as necessary, and ensure your project settings are set up to include the script.