Can we create a table-like layout where columns expand based on whitespace and display ellipses if text overflows?

For the past 3 days, I've been struggling to implement a table design that our UX designer is requesting.

The challenge is to create a table with N columns, where the first N-1 columns align to the left of the screen and the last column aligns to the right. Each column should adjust its width based on the largest element in its rows, with a consistent 24px spacing between columns. As the screen size decreases and white space becomes limited, text overflow should be displayed as ellipses while ensuring the table does not overflow off the screen.

I have tried various solutions found online and even experimented with chatbot suggestions, but none have fully met the requirements.

When using table layout fixed, the text does not expand to fill available white space; table layout auto expands text but fails to display ellipses for overflowing text. Wrapping content with specific width settings did not produce the desired result.

Another attempt with grid-template-columns resulted in the screen shrinking from left to right instead of utilizing the space between the N-1 and N columns.

The intended look on larger screens:

|Column A  | |Column B| |Column C          |                                              |Column D|
|----------| |--------| |------------------| -------------------------------------------- |--------|
| Cell 1234| |Cell 2  | |Cell 5            |                                              |Cell 6  | 
| Cell 3   | |Cell 4  | |Cell 5555555555555|                                              |Cell 6  |

Intermediate screen appearance:

|Column A  | |Column B| |Column C          |     |Column D|
|----------| |--------| |------------------| --- |--------|
| Cell 1234| |Cell 2  | |Cell 5            |     |Cell 6  | 
| Cell 3   | |Cell 4  | |Cell 5555555555555|     |Cell 6  |

On small screens:

|Column A  | |Column B| |Column C | |Column D|
|----------| |--------| |---------| |--------|
| Cell 1234| |Cell 2  | |Cell 5   | |Cell 6  | 
| Cell 3   | |Cell 4  | |Cell 5...| |Cell 6  |

And further minimized:

|Column A | |Column B| |Column C | |Column D|
|---------| |--------| |---------| |--------|
| Cell 1..| |Cell 2  | |Cell 5   | |Cell 6  | 
| Cell 3  | |Cell 4  | |Cell 5...| |Cell 6  |

Is it possible to achieve this desired behavior?

Answer №1

I may have come up with a possible solution using grid.

This involves manipulating the actual grid positioning of elements and utilizing the minmax property.

While it may not address your question directly, it could provide you with a helpful starting point.

#grid {
  display: grid;
  grid-template-rows: 1fr 1fr 1fr;
  grid-template-columns: minmax(50px, auto) minmax(50px, auto) minmax(50px, auto) minmax(24px, 1fr) minmax(50px, auto);
  gap: 24px;
  width: 100%;
  height: 100%;
}

#grid>div {
  min-height: 100px;
}

#grid>div span {
  display: inline-block;
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#div1 {
  grid-area: 1 / 1 / 2 / 2;
  background-color: rgba(134, 224, 208, 0.5);
  justify-self: start;
}

#div2 {
  grid-area: 1 / 2 / 2 / 3;
  background-color: rgba(152, 79, 175, 0.5);
  justify-self: start;
}

#div3 {
  grid-area: 1 / 3 / 2 / 4;
  background-color: rgba(44, 184, 77, 0.5);
  justify-self: start;
}

#div4 {
  grid-area: 1 / 5 / 2 / 6;
  background-color: rgba(196, 154, 3, 0.5);
  justify-self: start;
}

#div5 {
  grid-area: 2 / 1 / 3 / 2;
  background-color: rgba(82, 17, 37, 0.5);
  justify-self: start;
}
  
#div6 {
  grid-area: 2 / 2 / 3 / 3;
  background-color: rgba(249, 154, 182, 0.5);
  justify-self: start;
}

#div7 {
  grid-area: 2 / 3 / 3 / 4;
  background-color: rgba(144, 252, 111, 0.5);
  justify-self: start;
}

#div8 {
  grid-area: 2 / 5 / 3 / 6;
  background-color: rgba(254, 14, 250, 0.5);
  justify-self: start;
}

#div9 {
  grid-area: 3 / 1 / 4 / 2;
  background-color: rgba(224, 23, 19, 0.5);
  justify-self: start;
}

#div10 {
  grid-area: 3 / 2 / 4 / 3;
  background-color: rgba(75, 240, 154, 0.5);
  justify-self: start;
}

#div11 {
  grid-area: 3 / 3 / 4 / 4;
  background-color: rgba(249, 8, 188, 0.5);
  justify-self: start;
}

#div12 {
  grid-area: 3 / 5 / 4 / 6;
  background-color: rgba(155, 205, 226, 0.5);
  justify-self: start;
}

@media (max-width: 768px) {
  #grid {
    grid-template-columns: minmax(50px, auto) minmax(50px, auto) minmax(50px, auto) minmax(24px, 24px) minmax(50px, auto);
  }
  #grid>div {
    justify-self: stretch;
  }
}
<div id="grid">
  <div id="div1"><span>div1div1div1div1</span></div>
  <div id="div2"><span>div2div2div2div2</span></div>
  <div id="div3"><span>div3div3div3div3</span></div>
  <div id="div4"><span>div4div4div4div4</span></div>
  <div id="div5"><span>div5div5div5div5</span></div>
  <div id="div6"><span>div6div6div6div6</span></div>
  <div id="div7"><span>div7div7div7div7</span></div>
  <div id="div8"><span>div8div8div8div8</span></div>
  <div id="div9"><span>div9div9div9div9</span></div>
  <div id="div10"><span>div10div10div10div10</span></div>
  <div id="div11"><span>div11div11div11div11</span></div>
  <div id="div12"><span>div12div12div12div12</span></div>
</div>

Experiment with viewing on a "small screen," where small is defined as 768px, to observe how it appears. You can also adjust the definition of the 4th column based on your needs.

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

Return to home page

Could a button or link be created on an HTML page using HTML/JavaScript that directs to the URL saved as 'homepage,' similar to clicking on the home icon in Internet Explorer? Warm regards ...

Issue with multiplication and division when chaining calculations on click

As I continue to work on my React calculator project, I am gradually nearing its completion. However, today I encountered a math logic error that needs to be addressed. For example: 2+2*5 - currently returns 20 instead of the correct answer, which shoul ...

Tips for displaying an image larger than its container using CSS or Bootstrap

I'm attempting to include an image on the left side of text within a dark container. However, I want the picture to start at the very bottom of the container and extend above the top of the container, so that the image appears outside the top of the c ...

Trigger an event prior to the loading of a div

I have been working on a jQuery code that needs to run just before a specific div is displayed on the page. Take a look at the snippet below as an example of what I am trying to achieve: $(document).ready(function(){ $('.article').on('m ...

Retrieve a variable from list using FreeMarker within a loop that is chosen from a table

When clicking on the edit or delete anchor, I am trying to obtain the variable department.id for the selected department. I have attempted using <#assgn departmentId = "${department.id}">, but it retrieves the last id from the table as it i ...

Issue with vertical alignment within nested divs

I have been attempting to center a search bar inside a top banner vertically. My understanding was that the following code could achieve this: #banner { height: 35px; width: 100%; } #searchbar { height: 15px; position: relative; top: 50%; ...

Seeking guidance on embedding an HTML page within PHP code

I have a question regarding embedding an HTML page within a PHP page, specifically when using the include function like this: <?php include ("../../forms/login/form.html"); ?> My issue is that when I try to display form.php within index.php, it doe ...

React-Toastify revolutionizes the way styles are broken

For some time, I have been using react-toastify to show warnings. However, when I attempt to display the warnings now, it simply opens a page without any style and only the warning message. It looks like this: https://i.sstatic.net/HLNq2.png Is there a w ...

Steps for transferring a checked statement from an HTML document to a JavaScript file

Struggling to transfer checked statements from HTML to JS file {{#each readValues }} <br> Plug: {{@index}} => {{this}} <input type='checkbox' class="plug" onclick="sendData();" /> {{/each}} The code above is written i ...

Is there a way to create an animation for changing .text in response to an on click event?

I'm currently developing a simple calculator that increases a variable when a button is clicked, displaying the updated value in a div. Each click on the 'amount-upwards' button adds 200 to the displayed number. Below is the jQuery code I a ...

The configuration of flexDirection seems to be making images vanish

I have successfully arranged my images to display one after another, but I want them to be positioned horizontally instead of vertically. I attempted to achieve this by using flexDirection: 'row' and setting scrollview to horizontal, however, the ...

Looking for assistance with jQuery mouseover or hover functionality

My issue involves a button with the text: Add to bag and CSS hover properties of background-color:#A9885D; color:#fff; Using jQuery, I want the text to change to Added when the button is clicked, with a green background color. Then, after 5 seconds, it sh ...

Show the entered user data in a separate Div when the button is clicked

Whenever a button is clicked, I am currently able to show the user input from the textbox. However, it always displays in the same Div. The HTML file with the textbox and button- <input type="text" name="inputText"><br> <tr> &l ...

What is the process for extracting HTML code from a URL in a web browser?

Is there a way to view HTML code in preview mode on a browser? similar to this example The output of the link should display as "{{ color.name }}" ...

Tips for highlighting text in a textarea using jQuery or JavaScript

I'm intrigued by Facebook's functionality. They utilize textareas in their comments and status sections, but whenever I tag someone in a post, the selected text is highlighted with a light blue background within the textarea. As far as I know, th ...

How to prevent page refresh when hitting enter in jQuery form?

Currently, my form does not refresh the page when I click the button to submit it. However, if I press Enter while a text input is selected, the page will refresh. Is there a way to make pressing Enter on the text input perform the same action as clicking ...

Effortlessly Achieving Full Height Section Scrolling in Vue3

I have two sections, named One and Two, each taking up full-screen height and width (ignoring the top navbar in the video). As a user scrolls through my page, I want to prevent the sections from being partially scrolled. The window should display either ...

Positioning Data Labels Outside of Pie or Doughnut Charts in Chart.js

I am currently working on a large-scale project and I am in need of a way to position the labels outside of pie charts or doughnut charts. I came across a plugin called outerLabels on GitHub which seems to be the perfect solution for what I need, but I am ...

retrieve the selected option from the dropdown menu. (slightly challenging)

I am new to PHP and coding, so forgive any mistakes in advance. I have a web form that needs to retrieve information from a text file located in a server folder. I know how to fetch data from the file and create a dropdown list displaying all files in the ...

How can I align a single button to the left side while positioning the rest of the elements to the right in Bootstrap 4?

I am attempting to align the B1 button on the left side and have the remaining elements positioned on the right side. Below is my code snippet using Bootstrap 4.1: <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/b ...