How to implement CSS ellipsis to text when resizing the browser window

I've got a <table> that's set to 100% width. Inside one of the <td> elements, there's a div containing a text string that I'd like to truncate with an ellipsis if it overflows.

Initially, in my JavaScript code, I'm setting the width of the <div> element equal to the width of the corresponding <td>, which works as expected upon page load or refresh. However, I'm having trouble making it work when the browser window is resized.

(Please disregard the actual purpose behind this scenario. It's just a simplified example for this question.)

Can someone guide me on how I can achieve automatic truncation with ellipsis on resize?

function adjustWidth() {
    $('.text-div').css('width', $(".container-td").css('width'));
}
.text-div {
    width: 0px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body onload="adjustWidth()" onresize="adjustWidth()">

<table style="width: 100%">
    <tr>
        <td class="container-td">
            <div class="text-div">
                The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
            </div>
        </td>
    </tr>
</table>

</body>

Answer №1

Attempt to connect the resize event to the browser's window if it is not functioning properly with the body attribute.

$(window).resize(function(){
   doThis()
});

Answer №2

It seems possible to achieve this using only CSS. Would you mind testing out the following code?

<!DOCTYPE html>
<html>
<head>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>

<style>
   /* Introducing a new style attribute */
table,tr,td,tbody{
    width:100%;
    display:inline-block;

}
td{
    text-overflow:ellipsis;
    overflow:hidden;
    white-space:nowrap;
}
  /* New style attribute ends here */
</style>

</head>
<body>  <!-- //load & resize removed -->

<table>
    <tr>
        <td>  <!-- // div removed -->
             the quick brown fox jumps over the lazy dog the quick brown fox jumps over the lazy dog the quick brown fox jumps over the lazy dog the quick brown fox jumps over the lazy dog the quick brown fox jumps over the lazy dog the quick brown fox jumps over the lazy dog 

        </td>
    </tr>
</table>

</body>
</html>

Answer №3

The div element is a block-level element, which means that it will fill the entire width of its parent element by default. Therefore, no additional styling or scripting is needed to control its width.

Answer №4

To optimize the responsiveness of a table, consider using the display:block property to ensure that the div element adjusts its width properly based on the td element when resized.

table, thead, tbody, th, td, tr {
    display: block;
    height: auto;
}

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

Managing dependencies for controllers during the testing of directives

Currently, I am conducting a test on an Angular directive using the spec provided below: 'use strict'; describe('playerInfo directive', function() { var element; var scope; beforeEach(module('gameApp')); beforeEach( ...

Steps for transforming an array into a complex array

Here is an array I have: [2, 1, 2, 1, 1, 1, 1, 1] I am looking for a way to create new arrays within the original array when the sum of values exceeds four. The desired result should look like this: [[2,1],[2,1,1],[1,1,1]] ...

Basic game of tic tac toe using JavaScript and jQuery

Teaching myself the ropes of JavaScript/jQuery, I decided to dive into building a simple tic tac toe game. Following this example as a guide, I embarked on creating my own version. While most parts seem to be working smoothly, there's one problem that ...

I am facing a dilemma with Expressjs when it comes to managing numerous users simultaneously

I'm grappling with a conceptual dilemma as I delve into the world of building an expressjs app. My goal is to create a system where each user, upon starting the app, has their own temporary folder for storing various files. Despite my lack of experien ...

The browser prevented the script located at “http://127.0.0.1:5500/assets/platform.png” from loading due to an invalid MIME type of “image/png”

Sorry if this question seems repetitive, but despite extensive searching, I haven't been able to find a solution to my specific problem. I am currently working on developing a basic JavaScript game, but I'm facing challenges when it comes to impo ...

Ways to make a function inside a Node.js script get called by JavaScript in HTML?

At the moment, I am integrating Node.JS with various systems as the backend. My goal is to trigger a function in the Node.JS script from my webpage and retrieve specific values. This illustration outlines my objective: JS --> triggers function --> ...

Struggling to create a unique grid layout with bootstrap framework

After working with Bootstrap for about a month, my HTML code may be a bit messy. The current task requires me to create a custom slick carousel in order to achieve a user interface similar to the image linked below: https://i.sstatic.net/vBIFP.png With my ...

Why does JavaScript interpret the input [ 'foo' ] as accessing a property rather than declaring an array?

I find this particular dilemma on the Mocha Github issue tracker quite fascinating. https://github.com/mochajs/mocha/issues/3180 This snippet of code is functioning as intended: describe('before/after with data-driven tests', () => { befo ...

Steps to confirm if a route has been passed a prop

Within the GridRow.vue file, I have a function that redirects to a specific route while passing along parameters: redirectWithData () { this.$router.push({ name: 'payment.request', params: { per ...

Securing routes in Node.js using Passport.js

Within my express app, the main server code is contained in the server.js file. In this file, there is a route defined as app.get('/dashboard',require('./dashboard/dashboard.js'). The dashboard.js file contains multiple routes such as ...

The problem with -webkit-center in HTML

I have encountered an issue with some code that I wrote. When utilizing -webkit-center and entering text into a textbox, all the items shift to the right. I have attempted other -webkit alignment settings without any problems, only -webkit-center seems to ...

The Vue 3 chart.js does not have a defined chart when clicking

I'm currently in the process of upgrading my website from vue 2 to vue 3. As part of this upgrade, I also needed to update vue-chartjs from version 4 to 5. Unfortunately, it seems that this update has caused some issues with my click event functionali ...

Using dual ngFor in Angular 4: A step-by-step guide

I am encountering an issue in my Angular 4 project where I receive two different arrays in response to a server request, and I want to utilize both of them in the template. Here is my controller code: this.contractService.getOwnerContract() .subscribe( ...

Optimizing PHP Execution Speed

I have a query regarding PHP and CRON-Jobs. I've created a PHP script that sometimes takes a long time to load due to the numerous commands it contains. To address this issue, I split the script into smaller ones to reduce the number of commands. I th ...

Modifying the div based on the color it is positioned in front of

I am attempting to create a sticky, transparent div that changes its color based on the background behind it. I have both dark and light colored divs, and I want the sticky div to adjust its text color accordingly (white on dark backgrounds, black on light ...

Trigger event when the useRef element's height surpasses zero

I have a large list of photo thumbnails, and I need to ensure that one of the thumbnails scrolls into view when the list is loaded. The photos are generated using the map function, and the container div of one of the thumbnails will be assigned a ref. I ...

Understanding and aggregating data from JSON files

I am in possession of a group of json values outlined below {"labels":[ "time", "softirq", "user", "system", "nice", "iowait" ], "data":[ [ 1490088352, 0, 14.64646, 3.53535, 0, 1.0101 ], [ 1490088351, 0, 27.77778, 3.0303, 0, 0 ], [ 1490088350, 0.49751, 12 ...

Angular Appreciation Meter

Looking to create a rating system using Angular. The square should turn green if there are more likes than dislikes, and red vice versa (check out the stackblitz link for reference). Check it out here: View demo I've tried debugging my code with con ...

Utilize Bootstrap to combine two tables within a single column efficiently

I am facing an issue with my layout that involves organizing 3 tables in a specific way. Two smaller tables are supposed to take up 3 bootstrap columns on the left side, while one larger table should occupy 9 columns on the right side. However, when I impl ...

Revamp a dynamically generated class using ajax

I am currently developing an email-based application using PHP and the CodeIgniter framework. My goal is to change the status of unread messages to read when they are clicked on, utilizing two different classes: read0 and read1. While I can view the messag ...