Enhancing the appearance of the final column within static columns of a data table

I am facing an issue where I need to add a shadow next to a set of fixed columns in order to indicate that there is content beneath them. These fixed columns are marked with the class dtfc-fixed-left which is assigned by the FixedColumns library within DataTables. The number of fixed columns can vary from 0 to 2, depending on the configuration set using the datatables colvis functionality.

The table structure is as follows:

<table>
  <thead>
    <tr>
      <th class="dtfc-fixed-left">Column 1</th>
      <th class="dtfc-fixed-left">Column 2</th>
      <th>Column 3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="dtfc-fixed-left">test data 1</td>
      <td class="dtfc-fixed-left">test data 2</td>
      <td>test data 3</td>
    </tr>
    <tr>
        <td class="dtfc-fixed-left">test data 4</td>
        <td class="dtfc-fixed-left">test data 5</td>
        <td>test data 6</td>
    </tr>
    <tr>
      <td class="dtfc-fixed-left">test data 7</td>
      <td class="dtfc-fixed-left">test data 8</td>
      <td>test data 9</td>
    </tr>
  </tbody>
</table>

I have attempted to target the last occurrence of the dtfc-fixed-left class within each <tr> using the following CSS code:

td.dtfc-fixed-left:last-child {
   background: red !important;
}

Unfortunately, this did not produce the desired result. While I prefer not to hardcode styling logic with JS, I am willing to do so if no other solution is feasible.

If possible, I am looking for a selector that consistently identifies the last instance of the class dtfc-fixed-left within each <tr>.

Answer №1

One defining characteristic of that td is its lack of a subsequent sibling with the same class.

Most major browsers support the :has selector:

  td.dtfc-fixed-left:not(:has(~.dtfc-fixed-left)) {
    background: red;
  }

[However, please note that on Firefox a flag must be set].

<style>
  td.dtfc-fixed-left:not(:has(~.dtfc-fixed-left)) {
    background: red;
  }
</style>
<table>
  <thead>
    <tr>
      <th class="dtfc-fixed-left">Column 1</th>
      <th class="dtfc-fixed-left">Column 2</th>
      <th>Column 3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="dtfc-fixed-left">test data 1</td>
      <td class="dtfc-fixed-left">test data 2</td>
      <td>test data 3</td>
    </tr>
    <tr>
      <td class="dtfc-fixed-left">test data 4</td>
      <td class="dtfc-fixed-left">test data 5</td>
      <td>test data 6</td>
    </tr>
    <tr>
      <td class="dtfc-fixed-left">test data 7</td>
      <td class="dtfc-fixed-left">test data 8</td>
      <td>test data 9</td>
    </tr>
  </tbody>
</table>

Answer №2

Applying the last-child in the class is not possible. For more information, you can check out this resource

.dtfc-fixed-left:nth-last-child(2) {
   background: red !important;
}
<table>
  <thead>
    <tr>
      <th class="dtfc-fixed-left">Column 1</th>
      <th class="dtfc-fixed-left">Column 2</th>
      <th>Column 3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="dtfc-fixed-left">test data 1</td>
      <td class="dtfc-fixed-left">test data 2</td>
      <td>test data 3</td>
    </tr>
    <tr>
        <td class="dtfc-fixed-left">test data 4</td>
        <td class="dtfc-fixed-left">test data 5</td>
        <td>test data 6</td>
    </tr>
    <tr>
      <td class="dtfc-fixed-left">test data 7</td>
      <td class="dtfc-fixed-left">test data 8</td>
      <td>test data 9</td>
    </tr>
  </tbody>
</table>

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

Ways to trigger a 406 Error (Not Acceptable) using jQuery

Currently, I am implementing error handling in my.js file. This involves making cross-domain calls to another server and utilizing Mustache.js for dynamic HTML templates. $.getJSON(url, function(data, textStatus, xhr) { $.each(data, function(i, data) { ...

What is the best way to adjust the opacity of a Bootstrap 4 card while keeping the form and buttons contained within unaffected?

Trying to implement the Bootstrap 4 login form available at: Currently troubleshooting two issues with this form: Attempting to adjust the opacity of .card-signin without affecting the elements inside it. How can I change only the card's opacity? S ...

Connecting to a web service using WSDL with jQuery

Is there a way to access WSDL operations using jQuery? I have a WSDL file with 3 operations (add, update, delete). How can I pass parameters for these operations from jQuery? Is there a specific method for doing this? Thank you all in advance for your gu ...

Having trouble with retrieving data from a JSON file through a service in Angular 4? Getting an "unexpected end of input" error?

I've been attempting to retrieve data from a JSON file stored in the assets folder, but all I keep encountering are errors. heroes.service.ts getPeopleData(): Observable<people[]>{ return this.http.get<people[]>('assets/data/someD ...

Only one tab can be open at a time, and when clicking on another tab, the focus will shift to that tab

My main concern is opening one tab at a time. The issue lies in the fact that when a tab is open and another tab is clicked, the height of the first tab reduces and the section tab title moves to the top of the screen making it invisible to the user. I wan ...

The eventMouseover and eventMouseout Events in FullCalendar do not seem to be functioning as expected when using a custom

I created a unique calendar using FullCalendar, featuring both a standard view and a customized view that presents events in a list-style format. While everything displays correctly, I've encountered an issue where the default eventMouseover and even ...

Angular JS element cannot be located

My Angular object is not being passed when I write a new function, and I'm unsure why. The object, named person, is directly bound in the HTML and returns 2 items from the cardsCollection array. The function I'm attempting to create is called cl ...

Display a CSS box continuously

I have a code for a dropdown function that is working smoothly. Now I would like the comment box to show permanently when clicked and close when clicked outside of it. Currently, when I click on the comment, the box shows up but if I try to input a commen ...

After converting TypeScript to JavaScript, the import functionality appears to be malfunctioning

homepage.ts export function homepage () {...rest of function} app.ts import { homepage } from "./homepage"; homepage(); index.html <script src="/dist/app.js" type="text/javascript"></script> Error: Uncaught Sy ...

Utilizing the text field feature within Twitter Bootstrap 3.0 in-line styling

I am attempting to create a horizontal form with an inline text field split into two sections: one on the left and one on the right. The left side will contain horizontal text fields, some of which are inline, while the right side will have a mix of inline ...

jQuery Fullcalendar displays the date December 25, 2011 incorrectly as January 25, 2012

I've noticed that all the dates I input into fullcalendar seem to be displaying a month ahead. For example, 10 shows as November and 11 shows as December. Does anyone have an idea why this may be happening? If you need to see any of my code, just let ...

Resolving typescript error in my custom hook

Implementing this ResizeObserver hook in my project using typescript const useResizeObserver = () => { const [entry, setEntry] = useState<ResizeObserverEntry>(); const [node, setNode] = useState<Element | null>(null); const observer = ...

Tips for causing the JavaScript confirm alert to appear just a single time

My latest project involves creating a confirm alert that notifies users when their password is about to expire, prompting them to change it. The functionality for this alert is located in the header section of the website. Upon successful login, users are ...

Should we avoid styling HTML tags?

Would it be considered poor practice, for instance, to apply the following CSS rules to all images on my webpage in order to make them responsive: img { display: inline-block; height: auto; max-width: 100%; } Instead of using img-responsive on each ...

The functionality of the Ajax script seems to be optimized for Firefox browsers exclusively, as it is encountering issues with compatibility on other

My code works perfectly in Firefox, but I'm encountering an issue in all other browsers. Specifically, I'm getting the error message "SyntaxError: JSON Parse error: Unexpected EOF" on line if (this.readyState == 4 && this.status == 200). ...

where is the yarn global registry located?

After updating yarn to point to my custom registry and verifying the changes, here is what I found: $yarn config list -g yarn config v1.22.10 info yarn config { 'version-tag-prefix': 'v', 'version-git-tag': true, ' ...

Having difficulty swapping out images on an HTML website template for Internet Explorer

Recently, I obtained an HTML website template that I am currently working on customizing. One of the tasks I have been tackling is replacing some of the existing images with my own. After resizing the new images to match the dimensions of the originals, ...

Include a "Click for more" and "Click to collapse" button on all thumbnails, not limited to just one

I am struggling to make the toggle script below work for all thumbnails in the gallery, not just the first thumbnail. Each card is filled with paragraphs, bullets, and links, making the page overwhelming. That's why I need the "Read More" and "Read Le ...

Troubleshooting: Unable to trigger click event on Vuejs component

Working with Vuejs and Vuikit components, I have set up the following structure: <template> <div class="uk-scope"> <vk-modal :show="isShow" v-if="config"> <vk-modal-close @click="alert('hello!')" large& ...

Guide on creating uniform heights and widths for images with varying dimensions utilizing CSS (and percentage values)

Is there a way to ensure that all images maintain the same height and width using CSS percentages, rather than set pixel values? I'm working on displaying images in circles, where most are uniform in size but a few outliers distort the shape. The wide ...