Is it possible to enlarge a div using "display: table-cell" property when clicked on?

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 at 150px 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";
}

Answer №1

After some investigation, I came to the realization that it's impossible to set a maximum height for an element when it's displayed as a table cell. To work around this limitation, I enclosed my text within a div, where I defined all the necessary properties such as max-height and overflow, resulting in successful implementation.

For those interested, I've shared an updated version of the codepen which can be accessed here.

HTML:

<div class="containerDiv">
  <div id="content1" class="contentDiv">
    <div class="content">
      Lorem Ipsum is simply dummy text...
      <button class="showMoreButton" onclick="showMore()">Show More</button>
    </div>
    <br />
  </div>
</div>
... <!-- more HTML code here -->

CSS:

.containerDiv {
  display: table;
  min-height: 150px;
  overflow: hidden;
  width: 400px;
}
.contentDiv {
  display: table-cell;
  vertical-align: middle;
}
.content {
  max-height: 150px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.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() {  
  document.getElementById(event.path[1].id).getElementsByClassName("content")[0].style.maxHeight = "none";
}

Answer №2

Experiment with the CSS property text-overflow:ellipsis. You'll see great results!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is a more effective method for linking values with variables in an object?

Can you suggest a more efficient way to write this in JavaScript? let foo; if(bar) { foo = bar.value; } I am attempting to prevent a react error from occurring when `bar` is null if I were to use const foo = bar.value. ...

Engage with React JS arrays of objects

I have a specific object structure that looks like the following: [ { "periodname": "Test", "periodtime": "" }, { "periodname": "", "periodtime&quo ...

Storing data in a JSON format

Our team requires a service that can periodically generate up-to-date JSON data every 15 minutes for our AJAX services to consume. This JSON will be used for metric analysis and charts among other things. The reason behind the 15-minute interval is due to ...

A guide on invoking an onclick function within a Template literal in React.js

I am seeking assistance with implementing an on-click function using template literals in React JS. When a button within the template literal is clicked, I want to trigger another function in React JS. addPopupToLayer = (surface, layer) => { cons ...

Encountering obstacles when attempting to save form information to a MongoDB database using Node.js

I've been struggling to save the data entered in my form to my database, but it's not working at all. No errors are showing up and nothing is being logged. Here's the HTML code I'm using: <!DOCTYPE html> <html lang="en"> & ...

Convert the object containing arrays to a boolean value, which will be true if at least one of the arrays is not empty

Here's the scenario: searchCriteria: { filter1: [?], filter2: [?], filter3: [?], and so on } I am aiming for a boolean output that indicates whether any of the filter arrays contain data (i.e. are not empty). Something along the lines of: co ...

Encountering a "Page Not Found" error while configuring Passport in Express 4

Struggling with integrating passport into my Node.js application. Despite rearranging my requirements in app.js, I'm unable to resolve the issue. The error message reads: Not Found 404 Error: Not Found at /home/salma/Desktop/my-project/app.js:5 ...

AngularJS deferred rendering (not deferred loading views)

Imagine having over 3000 items to display within a fixed-height div using an ng-repeat and setting overflow: auto. This would result in only a portion of the items being visible with the rest accessible via scrollbar. It's anticipated that simply run ...

Make sure to always display the browser scrollbar in order to avoid any sudden page

Question: How can I ensure the scrollbar is always visible in a browser using JavaScript? I've noticed that some of my web pages have a scrollbar while others do not, causing the page to jump when navigating between them. Is there a way to make t ...

Customize the jQuery datepicker by assigning a class to the first 17 days

How can I apply a class to only the first 17 days on a jquery datepicker calendar? I've attempted the following code, but it ends up adding the class to every day... beforeShowDay: function(date) { for (i = 0; i < 17; i++) { return [t ...

Generating JSON data on the fly using D3.js scripting

I am attempting to create a JSON object dynamically by pulling data from an array in D3 JavaScript. (The code below is not the exact one I used, but similar) let radius = [10,20,30]; let jsonradius = '[{'; for (let i = 0; i < radius.le ...

Adjusting the React Material UI TextField behavior upon a change in value while maintaining the

I have an MUI TextField component that I would like to trigger a function when it changes, while still allowing it to function as usual (without being a controlled input). <TextField onChange={(e)=>{ doSomething(e.target.value) //perhaps call ...

The toggle button appears to be lacking in CSS styling

I'm trying to create a dropdown button using Bootstrap and ng-bootstrap in my Angular project. Here's the HTML I've written: <div ngbDropdown class="d-inline-block"> <button class="btn btn-outline-primary" id="dropdownBasic1" n ...

In what way can the result of the code displayed be considered as truthful?

this.someService.findDevices() .subscribe((segments) => { this.segments = Array.from(segments.segments); this.packs.forEach((pack) => { pack.segments = Array.from(segments.segments); pack. ...

Automatically add a table row at the bottom displaying the overall calculation

I have data to display in an HTML table. The data is coming from a third-party server (PHP) using AJAX requests. The data is currently being displayed effectively with the following code: <table id="tbl-product"> <tr> < ...

Encountering difficulty in accessing game.html following button clicks

Why isn't the redirection to game.html happening after clicking on the buttons in index.html? The file structure consists of server/server.js, public/index.html,public/game.html. <!DOCTYPE html> <html> <title>QUIZ GAME</title ...

What is the best approach: Loading all database results at once or making multiple requests?

Looking to refine a vast database with these specific columns: Name - Maximum Height - Minimum Height - Range - Maximum Angle - Minimum Angle The goal is to filter the data based on user-inserted criteria, such as if a user inputs a maximum height of 80c ...

Tips for smoothly transitioning from a simple transform to a rotate effect

I need help with an HTML element that needs to rotate when hovered over. Here is the code I have: The issue I'm facing is that I don't want a transition for translateX, only for the rotation. What steps should I take to achieve this? .cog { ...

Implement an innovative solution to automatically close the navbar component in tailwindcss on

I've been attempting to create a unique tailwind navbar component for React, but I've encountered issues when trying to make it close on scroll for mobile view. I found the initial code on this website. After that, I tried implementing the code ...

Issue with Jquery firing function during onunload event

I'm having trouble adding a listener to a form in order to trigger an ajax call when the user leaves it. I can't seem to identify any errors in Firefox and nothing is getting logged in the console, indicating that my code might be incorrect. As s ...