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

Rotate the front view image to the left view with a three-dimensional twist

I've been attempting to rotate the image using the code below, but I can only manage to go from a front view to a bottom view. My goal is to rotate the image from the front view to the left view. Is there a way for me to achieve this? body { heig ...

What attribute should I utilize to ensure the image always takes priority over text when using @media queries?

resolution My goal is to have the image appear on top of the text when resizing the window, but maintain the appearance shown in the attached image when using a larger resolution. Below is the CSS code I am currently using: .section4 { text-align: cen ...

Implementing the @media rule using Javascript

I'm trying to use JavaScript to add an image dynamically, but I want to remove it when the viewport is 600px or wider. This is my approach so far: var img = document.createElement('img'); // (imagine here all the other fields being defined ...

Setting the Height of a Fixed Element to Extend to the Bottom of the Browser Window

I currently have a webpage layout featuring a header at the top, a fixed sidebar on the left side, and main content displayed on the right. A demo version of this layout can be viewed at http://jsbin.com/iqibew/3. The challenge I'm facing is ensuring ...

css code for styling html elements

HTML: <link rel="stylesheet" type="text/css" href="styles.css"><table border="0" cellpadding="0" cellspacing="0" class="month"> <tr><th colspan="7" class="month">January 2017</th></tr> <tr><th class="mon">Mo ...

What is causing my jQuery to only impact the initial item in my iron-list?

How can I create a toggle effect for the left border of an iron-list entry in Polymer when clicking on it? I found some jQuery code that adds the border to the first entry clicked, but I need help extending this functionality to apply to all list entries ...

Adjust padding of elements based on scrolling movements

Currently, I am attempting to adjust the padding of a specific element based on how far down the page the user scrolls. Ideally, as the user scrolls further down the page, the padding will increase, and as they scroll back up, the padding will decrease. H ...

The error at line 72 of proxyConsole.js is: TypeError - Unable to access the 'default' property of an undefined object

I am new to using react redux loading bar and redux forms After clicking a button, I want the loading bar to display. The issue arises when I try to load the loading bar, resulting in the following error message: proxyConsole.js:72 TypeError: Cannot read p ...

What causes the parent and grandparent elements to shift when adjusting the margin of a header tag?

I'm facing an issue with the h1 tag where changing its margin-top property also affects the parent and grandparent elements. Can someone shed light on why this is happening and provide a solution? If you observe, setting the h1 margin to 0 auto creat ...

Ensuring the bottom border of an element extends to the edge of the screen when utilizing a 960 grid in HTML/CSS

I am currently working on creating a website layout using the 960 grid system developed by Nathansmith. I am facing an issue in extending the bottom border of an element to reach till the end of the screen while keeping the element inside a container div. ...

Tips for updating the content of a div with fresh data using a slideshow effect in jQuery

Imagine you have a div called A and an array filled with text data. When t=0, div A will display $data[0]. Then, after every n seconds, I want the div to show the next piece of data in the array. I am interested in creating a transition effect similar to ...

Hovering over the designated area will reveal the appearance of

I've encountered an issue with a list group in a card. Specifically, when hovering over the topmost item in the list group, a border appears underneath it (which should only appear when hovering). However, when I try to override this behavior using :h ...

The appearance of HTML varies depending on the type of mobile device being used

My email template is having display variations on different mobile devices, with the textbox sometimes centered instead of left aligned as intended. Any suggestions on what might be causing this issue and how to resolve it? Desired Display on All Devices ...

Determine the overall price of the items in the shopping cart and automatically show it at the bottom without the need for manual entry

Creating a checkout form that displays the total cost of products, subtotal, GST cost, delivery cost, and overall price. The goal is to calculate the total by adding together the costs of all products with the "price" class (may need ids) at a 15% increase ...

ShadowBox replacement for IE8

After much experimentation, I am on the verge of achieving an inset box shadow for IE8 without relying on JavaScript. Take a look at the screenshot below: Since Internet Explorer versions 5.5 through 8 only support Microsoft's "dropshadows" and "sha ...

Struggled to Find a Solution for Code Alignment

Is there a tool or software that can align different types of codes with just one click? I've tried using the Code alignment plugin in Notepad++, but it hasn't been effective. For example, when aligning HTML code using tags, I couldn't find ...

Determining height of an element in AngularJS directive prior to rendering the view

As I dive into the world of Angular, any assistance is greatly welcomed. My goal is to vertically align a div based on screen size (excluding the header and footer) when the page loads, the window resizes, and during a custom event triggered by the user ex ...

What is the best way to align an html table to the top of the page?

Can anyone help me with positioning an HTML table at the top of the screen in IE7 without the automatic top border? I am using HTML and CSS for this. I'm currently working on development in VS2008. ...

Updating the style of a CSS element using Python Selenium

Is it possible to change the css element style using Python Selenium during automation to increase the height of the page? The structure of the element in the HTML is as shown below: <div style="overflow-y:scroll;overflow-x:hidden;height:150px;&quo ...

Exploring the `zoom` CSS attribute and adjusting font sizes on Safari

While my website is somewhat responsive on desktop, the display on iPad or tablet-sized devices leaves much to be desired. Despite having an acceptable layout, everything appears too large, making navigation difficult. It's worth noting that my websit ...