In order to determine the number of lines to display, you must calculate the line-height multiplied by the desired number of rows. The example provided below demonstrates this calculation for three rows.
document.querySelector('.show-more .button').addEventListener('click', function() {
this.parentNode.previousElementSibling.classList.add('expand');
});
body {
font-family: Arial, Helvetica, sans-serif;
}
p {
margin: 0 0 5px;
}
.text-box {
font-size: 14px;
line-height: 14px;
max-height: 42px;
overflow: hidden;
margin-bottom: 10px;
}
.text-box.expand {
max-height: none;
overflow: visible;
}
@media screen and (min-width: 480px) {
.show-more {
display: none;
}
.text-box {
max-height: none;
overflow: visible;
}
}
<div class="box31 col-md-3 col-md-offset-0 col-sm-6 col-sm-offset-0 col-xs-8 col-xs-offset-2">
<div class="text-box text-box2">
<p>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.</p>
<p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.</p>
</div>
<div class="show-more">
<button class="btn btn-primary">Show More...</button>
</div>
</div>
The CSS code above follows the mobile-first approach.
The JavaScript function is a simple click event listener attached to the button that targets the relevant text-box
element in the DOM and adds the class expand
to it. This action removes or resets the max-height and overflow properties.
Here's the jQuery equivalent of the JavaScript mentioned above:
$('.show-more .button').on('click', function() {
$(this).parent().prev().addClass('expand');
});