There are various components displayed on my webpage:
- I require all components to have a minimum height of
150px
. - If the height is less than
150px
, I want to vertically center their text. - In case the height exceeds
150px
, I aim to truncate the text at150px
and provide a show more button for expanding if needed.
Currently, the first scenario where the height is below 150px
works perfectly, with the text centered. However, I am facing difficulty in truncating content for components with height > 150px
.
I am attempting to achieve this without the use of jQuery.
You can view the codepen showcasing the issue here.
HTML:
<div class="containerDiv">
<div id="content1" class="contentDiv">
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.
<br />
<button class="showMoreButton" onclick="showMore()">Show More</button>
</div>
</div>
<br />
<div class="containerDiv">
<div id="content2" class="contentDiv">
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.
<br />
<button class="showMoreButton" onclick="showMore()">Show More</button>
</div>
<br />
</div>
CSS:
.containerDiv {
display: table;
min-height: 150px;
width: 400px;
}
.contentDiv {
display: table-cell;
max-height: 150px;
overflow: hidden;
vertical-align: middle;
}
.showMoreButton {
display: none;
}
JS:
var contentDivs = document.getElementsByClassName("contentDiv");
for(var i = 0; i < contentDivs.length; i++) {
if(contentDivs[i].offsetHeight > 150) {
contentDivs[i].getElementsByTagName("button")[0].style.display = "inline-block";
}
}
function showMore() {
console.log(event.path[1].id);
document.getElementById(event.path[1].id).style.maxHeight = "none";
}