Using ellipses in HTML tables

I'm in need of some assistance once again. I've been trying to update my current HTML table so that it highlights on hover and includes ellipsis when the text is too long. However, I seem to be having trouble getting both features to work simultaneously. The ellipsis works fine on its own, but once I add the highlight on hover, it stops working. I attempted a different approach by adding some script, which fixed the hover highlighting issue but caused the ellipsis to malfunction.

If you have any expert advice or tips on how to incorporate both features into my code below, I would greatly appreciate it. Thank you in advance for your help.

Here is the script for the hover highlight:

<script>
if (!('select' in HTMLTableCellElement)) {
HTMLTableCellElement.prototype.select = function() {
var range = document.createRange();
range.selectNodeContents(this);
window.getSelection().addRange(range);
    }
}
</script>

CSS:

<style type="text/css">

table{
    table-layout: fixed;
    width: 170px;
    height: 35px;
}

.table {font-size:12px;color:#333333;width:100%;border-width: 1px;border-color: #9dcc7a;border-collapse: collapse;}
.table th {font-size:12px;background-color:#abd28e;border-width: 1px;padding: 8px;border-style: solid;border-color: #9dcc7a;text-align:left;}
.table tr {background-color:#ffffff;}
.table td {font-size:12px;border-width: 1px;padding: 8px;border-style: solid;border-color: #9dcc7a;}
.table tr:hover {background-color:#ffff99;}
.table td text-overflow:
.table td width: 225px;
.table td white-space: nowrap;


::selection {
background-color: orange;
color: blue;
}

</style>

Table sample:

<table class="table" border="1">
<tr><th>Header</th></tr>
<tr><td onclick="this.select()">This sentence should show ellipsis. This sentence should show ellipsis.</td><td onclick="this.select()">This sentence should show ellipsis. This sentence should show ellipsis.</td><td onclick="this.select()">This sentence should show ellipsis. This sentence should show ellipsis.</td><td onclick="this.select()">This sentence should show ellipsis. This sentence should show ellipsis.</td><td onclick="this.select()">This sentence should show ellipsis. This sentence should show ellipsis.</td></tr>

I am eagerly awaiting your response.

Kind regards,

Jason

Answer №1

Your CSS code is incorrect. The correct version should be:

.table td { 
    text-overflow: ellipsis;
     width: 225px; 
     white-space: nowrap; 
     overflow:hidden; 
} 

Answer №2

After some tinkering, I finally cracked the code! Here's the updated version of my CSS:

    <style type="text/css">

table{
    table-layout: fixed;
    width: 170px;
    height: 35px;
    font-size:12px;color:#333333;width:100%;border-width: 1px;border-color: #9dcc7a;border-collapse: collapse;
}

table td {
    font-size:12px;border-width: 1px;padding: 8px;border-style: solid;border-color: #9dcc7a;
    overflow: hidden;
    text-overflow: ellipsis;
    width: 225px;
    white-space: nowrap;
}

table th {
    font-size:12px;background-color:#abd28e;border-width: 1px;padding: 8px;border-style: solid;border-color: #9dcc7a;text-align:left;   
}

#table tr {background-color:#ffffff;}
#table tr:hover {background-color:#ffff99;}

::selection {
background-color: orange;
color: blue;
}

    </style>

Big thanks to Malk for the inspiration!

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

Tips for saving the circular slider value to a variable and showcasing it in the console

I have coded a round slider and need assistance with storing the slider value in a variable and displaying it in the console using JavaScript. I want to store the tooltip value in a variable for future use. $("#slider").roundSlider({ radius: 180, min ...

Send input values to a JavaScript function from an onchange event

I'm facing an issue with using AJAX to update a section of my website when the drop-down box is changed. The problem I am encountering is passing the parameters correctly to the function. Below is the HTML code: <script type='text/javascript ...

The second JavaScript code consistently replaces the first code

I am implementing 2 different JavaScript references in my code, as shown below: <script src="../scripts/jsKeyboard.js" type="text/jscript"></script> <script src="../scripts/DOB_Validate.js" type="text/jscript"></script> To cal ...

Leveraging Bootstrap with Less, Grunt, and Node for enhanced development capabilities

I recently started using Grunt and I have set it up with grunt/node. I want to add the foundation of Bootstrap for responsiveness, but not the entire css/js. Can someone help me understand how to do this? I've looked at different documentation but sti ...

Incrementing values in ng-repeat object automatically

My project involves extracting game information from mlb.com and utilizing angularjs along with the ng-repeat directive to display it. A sample of the JSON feed is shown below. { "data": { "games": { "next_day_date": "2017-08-19", "mo ...

Using dynamic values in Angular functions

Here is the code snippet I am working with: $scope.testByValue = function() { $scope.fChanged($scope.lists.title); }; But I am facing an issue on the page where the value for: $scope.testValue = testValue; The variable testValue is being assigned a ...

Unable to use the ordered tag correctly in typing

Please review the HTML and CSS Code provided below: HTML : <ol> <li>Test</li> <li> <ol> <li>Sub-Chapter</li> <li>Sub-Chapter</li> </ol> < ...

Breaking apart paragraphs in a text using JavaScript

Hello, I am working on a code that is supposed to extract paragraphs from text entered into a textarea and create an array containing each paragraph. For example, it should turn text like this: Hello, that's the first paragraph Hello, that's th ...

Creating a top-to-bottom pull effect on a single page, similar to the Android title bar, can be achieved using CSS3

Is there a way to achieve an effect in HTML that resembles the pull title bar animation from top to bottom in Android? Any suggestions on how to create this effect or what tools I would need? Currently, when I swipe, the page simply displays a following ...

Tracking accurate responses with button click

In my quiz, I want to keep track of the correct answers selected by the player. When a player clicks on the correct answer, the counter should increase by one. At the end of the quiz, the HTML should display a message like "You got" + correct + "answers co ...

Adjust the wait time for sliding on the jQuery rcarousel

I am currently utilizing rcarousel for my website and I would like to adjust the duration of time that each slide is displayed. At the moment, each slide stays up for just a few seconds, but I want to extend this so that each slide remains on screen for at ...

Utilizing Reveal and Interchange for an Adaptive Lightbox in Foundation 4

Can Foundation 4 Reveal be used with images in a way that they are only loaded when the user clicks an element to open the modal? My objective is to create an Adaptive Lightbox using Interchange. When a user clicks on a thumbnail, it should display an ima ...

Maintaining active navigation state in JQuery/JavaScript after clicking a link: tips and tricks

While many resources discuss adding the active class to a nav link using jquery, there is less information on maintaining the active state after the nav link has been clicked. After experimenting with code from various sources, I have attempted to set ses ...

I'm having difficulties with separators in CSS when I attempt to insert one

Hey there, I've encountered an issue with my separator: #menu li:not(:first-child):before { content: " | "; } Despite this code working, the first li item isn't being separated like the others. This is the current output I'm getting: t ...

Creating your own personalized menu in PHP

Although I am relatively new to PHP, I have successfully developed a membership framework for my website that is fully functional. However, before diving into the design phase, there is one particular thing I am keen on achieving but unsure of how to go ab ...

Utilizing shared functions defined across different controllers

Can I utilize the code within these controllers for other purposes? .controller('GenericController', ['$scope', '$controller', '$rootScope', '$dialogs', '$state', '$http', '$modal& ...

I can't seem to figure out why my characters keep disappearing from the HTML string when I attempt to dynamically add HTML using JavaScript

I am currently facing an issue with dynamically adding links to a page. The links are being added successfully, however, the '[' and ']' at the beginning and end of the line are disappearing. Here is the code snippet from my .js file: ...

Redirect data from a server's database to a local environment for testing purposes

I have been given a project at a company where I need to make some enhancements and modifications as a side project. Currently, I have an almost finished website with around 13,000 lines of code in C# and JavaScript. To avoid any mishaps on the live websit ...

Bootstrap columns incorrectly stacking in vertical orientation

I'm currently in the process of learning about the Bootstrap grid system. I've implemented the following HTML code to create a row with three columns: <body> <div class="container"> <div class="row"> ...

Having trouble with Selenium and Java when sending keys to a text field - no errors though

I am currently using Selenium with Java in Eclipse for automating test cases on a website as a way to improve my skills. Below is the HTML code snippet. https://i.sstatic.net/I2lCP.jpg My project is set up using Page Object Model in Maven, however, I am ...