Create a border surrounding the matColumn within a sticky matTable

I am currently working with a matTable that has the first two or three columns set as sticky. However, I am facing an issue where the scrolling behavior of the non-sticky columns under the sticky ones looks like a glitch due to existing border styling on the table:

https://i.stack.imgur.com/hMSMG.png

I would like to apply a unique border style (or any other type of styling) but I am encountering challenges with targeting the headers and body cells separately:

  1. All column headers share the same list of classes, making it difficult for me to select only the sticky ones:

https://i.stack.imgur.com/6no1Y.png

  1. The body cells only have 'mat-table-sticky' on the actual sticky columns, allowing me to target them individually. However, using sibling selectors is proving to be tricky when trying to select the last sticky column; 'last-child' and 'last-of-type' selectors do not seem to acknowledge that I specifically want to target the sticky columns.

Does anyone have suggestions on how I can apply a border (or any form of styling) exclusively to the last sticky column?

Edit: Here is the HTML code defining the sticky status:

<ng-container *ngFor="let column of columns">
  <ng-container matColumnDef={{column.displayName}} [sticky]="column.sticky"> <!-- column.sticky determines whether a given column is sticky -->
    <th mat-header-cell>{{column.displayName}}</th>
  </ng-container>
</ng-container>

Answer №1

It seems like using nth-child may be the solution to your issue with th:nth-child(x)

Check out this link for more information on :nth-child CSS selector

body{
    display: grid;
    place-content: center;
    position: relative;
}

th{
  margin-right:1rem;
}


.first-table th:nth-child(4){
  color:blue;
}
<table>
      <tr class="first-table">
        <th class="mat-sticky">Savings</th>
        <th class="mat-sticky">Savings</th>
        <th class="mat-sticky">Savings</th>
        <th class="mat-sticky">4th child</th>
        <th class="mat-sticky">Savings</th>
        <th class="mat-sticky">Savings</th>
        <th class="mat-sticky">Savings</th>
      </tr>

      <tr class="second-table">
        <th class="mat-sticky">Savings</th>
        <th class="mat-sticky">Savings</th>
        <th class="mat-sticky">Savings</th>
        <th class="mat-sticky">Savings</th>
        <th class="mat-sticky">Savings</th>
        <th class="mat-sticky">Savings</th>
        <th class="mat-sticky">Savings</th>
      </tr>
</table>

Answer №2

In my solution, I implemented a function in each cell that examines the adjacent column and assigns a sticky-edge class if it is at the edge:

<ng-container *ngFor="let column of columns; let i = index">
  <ng-container matColumnDef={{column.displayName}} [sticky]="column.sticky"> <!-- column.sticky determines whether a given column is sticky -->
    <th mat-header-cell [class]="getStickyEdgeClass(i)">{{column.displayName}}</th>
  </ng-container>
</ng-container>
getStickyEdgeClass(index: number){
  if (columns.length > index + 1 && columns[index].sticky && !columns[i + 1].sticky)
    return "sticky-edge";
  else
    return "";
}
.sticky-edge {
  border-right-color: black;
}

I had initially hoped to achieve this purely with CSS but sometimes solutions require more than just styling. Perhaps there's a better way out there that I haven't discovered yet.

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

Creating responsive images in Bootstrap columns

I've found a solution to my issue with large images, but I am still struggling with smaller images not fitting well into larger spaces. Essentially, I created a CMS for a user who required templates with various bootstrap columns as content areas. Cu ...

How can I place a DOM element without relying on style properties?

Before diving in, let me provide some context. I am currently developing a visual element that operates similar to a spreadsheet. It features scrollable content areas in both directions, with axis indicators on the left and top that remain visible at all t ...

Utilize CSS to break through a backdrop

I have come across a rather peculiar question, and I'd like to elaborate with a code snippet: .container { width: 200px; height: 200px; background: rgba(200, 200, 200, .87); } .pin { position: absolute; left: 50px; top: 20px; } .overl ...

Tips for enhancing the appearance of the dropdown scrollbar in PrimeNG

Just started exploring Angular and I've been struggling to customize the scrollbar on a PrimeNG dropdown. Does anyone have any tips or tricks to achieve this? Here is the HTML code: <p-autoComplete placeholder="- Select -" (onSelect)="onSelect(dh ...

Maintaining nth-child(odd) following filtering using jQuery

I am using a jQuery script to filter a table with multiple entries on this site. The filtering works well, but the issue arises when it comes to maintaining the correct tr:nth-child(odd) color that I have specified in my CSS. After filtering, the original ...

Interacting with elements in Selenium by clicking

I am currently working on a project where I need to extract user data from an HTML table. Although I have successfully gathered information from the initial page, I'm facing difficulty in using Selenium with Python to click on the button that navigate ...

How to Style a Diamond Shape with Content Inside Using CSS

Can someone help me figure out how to create a diamond-shaped background in HTML? I want the diamond shape to contain text and images, similar to what's shown in this screenshot. The example image uses a background image of a diamond, but I'm loo ...

The alignment of the HTML and CSS speech bubble is experiencing issues

I need help with CSS as I am new to it and trying to create a speech bubble. However, when the text inside the bubble is long, it overlaps the timestamp. I am currently using `display: inline-block;` in my CSS. Below are snippets of my HTML and CSS code: ...

Yii2 Gridview - Horizontal Scroll Bar at the Top of the Page

I found a helpful post on Stack Overflow about creating a Yii2 Gridview with a fixed first column (Yii2 : Gridview with fixed first column), and now I'm looking to add a horizontal scrollbar at the top of the grid. My users really appreciate being ab ...

Organizing Pictures in a Gridded Design

My dilemma lies in displaying a set of thumbnail images within a div. The width of the div can vary depending on the screen size. Each image is fixed at 150px * 150px with a padding of 5px. I aim to arrange these thumbnails in a grid layout while ensuring ...

Setting the height of a parent element in AngularJS when using ng-repeat

I'm dealing with a ng-repeat that has varying heights. When I set a fixed height, everything looks fine. However, I need the parent panel's height to adjust automatically based on the ng-repeat child elements to avoid overflow issues. Is there a ...

Tips for making inline elements match the line height

Consider this block element : <div style="background-color: gray; line-height: 30px;"> Some text and a <a style="background-color: yellow;">link<\a> <\div> Why is it that the yellow color does not extend the full hei ...

What is the best way to adjust the Material Drawer width in Reactjs to match the width of its children?

Currently, I am utilizing the Material-ui Drawer component to toggle the display of content on the right side of the screen. The functionality I am aiming for is that when the Drawer opens, it will shrink the existing content on the right without any overl ...

Spacing Issue with Material UI Gap Placement

Currently, I am implementing Material UI 5 within a React project using the following code snippet: <Stack> <Stack flexDirection="row" justifyContent="center" padding={4} spacing={{ xs: 4 }} ...

Is it feasible to generate a fixed lighting effect overlay with HTML and CSS?

Is it possible to incorporate a static lighting effect overlay using HTML/CSS? My project consists of an HTML5/JS app with a top navigation bar and a series of cards that are transitioned through using swipe gestures. These cards are displayed in gray ove ...

Extract value from child div using JQuery and update text in another section of the page

Here is a fiddle showcasing the concept I am working on: https://jsfiddle.net/wvz9p3e7/1/ The code is written in PHP and involves a loop for cycling through 7 or 8 different garments. My goal is to have the window on the right side of the fiddle display t ...

Getting rid of the arrow icons in a Material UI TextField

I am facing a challenge with removing the up and down arrow icons from a Material UI TextField that I adjusted using the Material UI documentation (https://material-ui.com/components/autocomplete/#autocomplete) Highlights section. After trying various sol ...

Achieving left text alignment in CSS for an excerpt using the Ultimate Posts Widget on Wordpress

I've been trying to align the text in the excerpt of a widget called "To-Do List" on the left sidebar. I attempted to set the text-align property to left for the excerpt text, targeting the p tag within the .widget_ultimate_posts, .uw posts using CSS ...

Tips for eliminating unnecessary white space using CSS

https://jsfiddle.net/38h8acaq/ Is there a way to eliminate the white space on the left of the first tab and between the tabs and the content? Despite adding several margin:0px; lines to the CSS, none seem to be effective in removing the unwanted whitespac ...

Obtaining text using selenium based on element ID

When attempting to extract text from an HTML using Selenium in Python, I encountered an issue. The text I am trying to retrieve is located under the id tag, but my current method results in an error. date=browser.find_element_by_id('ctl00_ContentPl ...