When using div elements for tables, the width and text ellipsis properties are not enforced

I need help creating a simple table that will truncate text with ellipses if it exceeds the width.

Despite my efforts, the width is not being applied correctly. You can see the issue here:

Check out this JSFiddle for the table width

Here is the code snippet:

Including a header and a div wrapper around all rows which doesn't work with fixed layout

<div>
<div class="table">
<div class="row">
    <div class="cell">
        header
    </div>
    <div class="cell">
        123
    </div>
    <div class="cell">
        456
    </div>
    <div class="cell">
        789
    </div>
    <div class="cell">
        110
    </div>        
</div>
<div class="row">
    <div class="cell">
        This is a very long line of text that should be trimmed
    </div>
    <div class="cell">
        123
    </div>
    <div class="cell">
        456
    </div>
    <div class="cell">
        789
    </div>
    <div class="cell">
        110
    </div>        
</div>
<div class="row">
    <div class="cell">
        This is a very long line of text that should be trimmed
    </div>
    <div class="cell">
        123
    </div>
    <div class="cell">
        456
    </div>
    <div class="cell">
        789
    </div>
    <div class="cell">
        110
    </div>        
</div>
</div>
</div>
</div>

CSS

.table {
 display:table;
table-layout: fixed;
width: 100%
}

.row {
display: table-row
}

.cell {
display: table-cell;
width: 20%;
white-space: nowrap;
padding: 5px;
overflow: hidden;
text-overflow: ellipsis;
}

Your suggestions are appreciated. Thank you.

Answer №1

If you want your table to have fixed layout and a width of 100%, remember to include table-layout:fixed; and width:100%; in your CSS styling for the table:

.table {
    display:table;
    table-layout:fixed;
    width:100%;
}

Check out this jsFiddle example

.table {
    display:table;
    table-layout:fixed;
    width:100%;
}
.row {
    display: table-row;
}
.cell {
    display: table-cell;
    width: 20%;
    white-space: nowrap;
    padding: 5px;
    overflow: hidden;
    text-overflow: ellipsis;
}
<div class="table">
    <div class="row">
        <div class="cell">This is a very long line of text that should be trimmed</div>
        <div class="cell">123</div>
        <div class="cell">456</div>
        <div class="cell">789</div>
        <div class="cell">110</div>
    </div>
    <div class="row">
        <div class="cell">This is a very long line of text that should be trimmed</div>
        <div class="cell">123</div>
        <div class="cell">456</div>
        <div class="cell">789</div>
        <div class="cell">110</div>
    </div>
</div>

Answer №2

One option is to define a fixed pixel width:

.cell { max-width: 20px; } //Setting a specific width

Answer №3

Is it possible to achieve the same effect without using display:table-cell?

I experimented with an alternative approach that appeared to be successful:

.cell {
    display:inline-block;
    width: 20% !important;
    white-space: nowrap; 
    overflow: hidden;
    text-overflow: ellipsis;
}

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

The horizontal scrolling feature will not activate

I meticulously followed the instructions in a YouTube tutorial to implement this code. What's puzzling is that while the code works perfectly for the person in the video, allowing the grid items to scroll to the right, it doesn't have the same ef ...

Using AngularJS ng-style to set dynamic background images for different variables

Excuse me if this is a basic question. I am working on designing a login page with a background image, while the other pages don't have this background. I tried using ng-style but couldn't get the style to update when navigating between pages. In ...

What causes the CSS Sticky property to override neighboring elements as it moves?

My Goal: I am attempting to create a scrolling list on the page. Everything works fine until the last element, which seems to erase all the other elements. * { margin: 0; padding: 0; font-family: 'Ubuntu'; box-sizing: border-box; } bo ...

Unable to access the suggestion list within the modal

I incorporate the PrimeNG AutoComplete feature within a PrimeNG Dialog, displaying a list of elements below the AutoComplete in the dialog. My objectives are: To set the max-height of the modal dialog to 300, and have a visible scrollbar if the total ...

A guide on retrieving values from programmatically created input elements in Java

Here's the HTML code that I am currently working with: <form method="get" action="#" id="startForm"> <input type="text" name="period" id="period" placeholder="The number of days"/> <input type="submit" name="submit" value="subm ...

What is the best way to ensure that all the divs within a grid maintain equal size even as the grid layout changes?

I have a grid of divs with dimensions of 960x960 pixels, each block is usually 56px x 56px in size. I want to adjust the size of the divs based on the changing number of rows and columns in the grid. Below is the jQuery code that I am using to dynamicall ...

What is the reason behind the body element's background styling impacting the entire screen?

Why does styling the background of the body element affect the entire screen instead of just the body itself? For example, consider this CSS rule: body { width: 700px; height:200px; border: 5px dotted red; background-color: blue; } While the ...

Populate any void area when <td> is not occupied in <tr>

I have a table with two rows and columns. If one of the columns is empty, it should move up to the upper row. For example: |td1 | td2| |td3 | | |td5 | td6| This is what I want: |td1 | td2| |td3 | td5| |td6 | | Below is the code snippet that ...

Incorporating a scroll bar into a horizontal display

In troubleshooting this web interface, I encountered an issue related to the horizontal view. The problem is evident in the image below - when in horizontal view, more vertical space is required, prompting the need for a scrollbar to be added between the ...

Return to the previous location on the page when clicking the browser's back button

I'm looking to incorporate the following functionality: When on page a, clicking a link takes me to page b and displays all content from the beginning. Pressing the browser back button should then return me to page a and show the content starting fro ...

Javascript class for creating random quotes

I am utilizing the JavaScript fetch API to display a random quote. The quote changes upon page load, however, I encounter a type error when clicking on the new quote button. Error message: TypeError: Cannot read properties of undefined (reading 'len ...

Responsiveness of a div containing various content

I am currently working with Bootstrap 4 and my HTML code looks like this: <div class="font-weight-bold col-12"> <span>Filter :</span> <input type="text" class="form-control" /> </div> Initial ...

Using percentages to position Div elements

Currently, I am working on an HTML page that requires a div element spanning the full width of the page. My goal is to arrange other divs within this full-width div using percentages rather than pixels. The class associated with this div is .question. Thi ...

Utilizing Puppeteer to interact with dropdown menu options through selection and clicking

To log out, I am trying to use Puppeteer to select and click on a dropdown menu item. This particular menu requires hovering over it with a mouse in order to reveal its options, as opposed to clicking directly. When attempting to copy the selector for th ...

What is the best way to ensure Bootstrap columns adapt to different screen sizes on all devices?

Seeking assistance with creating a responsive Login/Register page layout. At present, the desktop version appears like this on 1920x180: http://prntscr.com/cl4ms8 Utilizing <div class="col-xs-6"> for both forms to ensure equal division on the page. ...

Whenever the download bar emerges at the bottom, the slideshow content on the page suddenly shifts upwards

Each time the download bar shows up at the bottom, the slideshow content on the Homepage suddenly moves up. It returns to its original position after I close the download bar.https://photos.app.goo.gl/F482eMfkXyfEZkdA9. My assumption is that this issue is ...

How to centralize a div using jQuery when its width is not known

I have a fixed position div that I want to center in my browser window. Although it can be done with CSS, the issue is that I am dynamically changing the element's width multiple times (due to loading content via ajax), which means the width changes ...

The click event fails to trigger while trying to parse external HTML

Currently, I am working on a project that requires me to load HTML from an external file and insert it into an existing div element. Although the process is successful overall, I have encountered an issue where the .click() events do not trigger when click ...

Alternative background for browsers that do not have support for border-image styling

I am experimenting with CSS3 border-image to create a simple button design: the left slice of the image is intended to be the left border of the text, the right slice as the right border, and the middle slice should either repeat or stretch as a background ...

Creating tags in HTML and displaying them on a webpage

I have a code that sends a message from a textarea after completion tags are entered as I wrote. The desired output should be: <h1> Thanks </h1> The expected output is: Transmitter Thanks instead of <h1> Thanks </h1> ...