apply CSS to a specific column in a table

I have a table created in the specific layout provided below:

<thead>
  <th></th>
  <th></th>
  <th></th>
</thead>

<tbody>
  <tr>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
  </tr>
</tbody>

My goal is to highlight an entire column when its header is hovered over. For example, if I hover over the second <th> from, let's say making it blue, then the corresponding second <td> in every <tr> will be highlighted in yellow as a column. I've attempted various methods but so far have only been successful in highlighting the header and not the entire column. I would like to maintain the structure within a table format. Can anyone provide assistance with this?

Answer №1

Here is an example of how you can achieve this:

<table>
<thead>
  <th>Header 1</th>
  <th>Header 2</th>
  <th>Header 3</th>
</thead>

<tbody>
  <tr>
    <td>Row 1 Cell 1</td>
    <td>Row 1 Cell 2</td>
    <td>Row 1 Cell 3</td>
  </tr>
  <tr>
    <td>Row 2 Cell 1</td>
    <td>Row 2 Cell 2</td>
    <td>Row 2 Cell 3</td>
  </tr>
  <tr>
    <td>Row 3 Cell 1</td>
    <td>Row 3 Cell 2</td>
    <td>Row 3 Cell 3</td>
  </tr>
</tbody>
  </table>

Additionally, include the following CSS:

table {
  overflow: hidden;
}


td, th {
  position: relative;
}
th:hover {background-color:blue;}

th:hover::after {
  content: "";
  position: absolute;
  background-color: grey;
  left: 0;
  top: -5000px;
  height: 10000px;
  width: 100%;
  z-index: -1;
}

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

Customizing the tab content background color in MaterializeCSS

I've been struggling to customize the background color of my MaterializeCSS tabs content by referring to their official documentation: If you visit the website, you'll notice that the default tabs have a white background, while the 'Test 1& ...

ng-html-bind for a label with a checkbox

Currently, I have a view that uses the standard bootstrap layout for checkboxes. In this layout, the input is located within the label tag. <div class="checkbox"> <label ng-bind-html="vm.trustedHtml"> <input type="checkbox" ng- ...

Transferring information from ng-Dialog HTML to a controller

Having some issues with ng-Dialog. When I include the ngDialog controller option, it works. I can retrieve the value of $scope.descriptionText from <p>Description:</p> <textarea ng-model="descriptionText"></textarea> Now, whe ...

Navigating the waters of adding a CSS property to a jQuery variable pseudo selector

Is there a way to adjust the top position for $myClass both before and after? I attempted to do so with the following code: var $myClass = $(".myclass"); $myClass.filter(":before,:after").css("top",-23); ...

Having trouble connecting the HTML file with the JavaScript file

This is my unique HTML file <!DOCTYPE html> <html> <head> <script src="ll.js"></script> </head> <body> <link rel="stylesheet" href="home.css"> ...

When I apply properties in JavaScript, I find that I am unable to utilize the CSS "hover" feature

For a personal project I am currently working on, I decided to experiment with setting background colors using Javascript. However, I have encountered an issue where my CSS code that changes the hover property does not seem to work after implementing the J ...

What sets XHTML 1.1 apart from other document types?

Visit this page for more information: XHTML 1.1 makes it simple to create various display formats, including printing pages, wireless device and PDA pages, and browser pages for television by creating a new CSS (cascading style sheet) for the specific ...

Prevent the repositioning of a tooltip due to overflow in Bootstrap 5.2 and Popper JS Tooltips

Can someone please provide an example of how to create a tooltip using Bootstrap 5.x and Popper JS, where the tooltip remains fixed to the associated element even if the page overflows and scroll bars appear? I've attempted to work with boundaries an ...

What is the best way to implement CSS properties on Material UI components?

I've recently started exploring Material UI, but I'm having trouble understanding how the spacing properties function. I'm trying to utilize the "spacing" feature for various elements, but it appears that it only works for "Box" components a ...

Align elements to the left and right using Flexbox

I am having trouble arranging a list of buttons horizontally. My goal is to have each row display two buttons - one on the left side and one on the right side. Occasionally, the left button needs to be hidden using display: none, but I still want the right ...

Sketch lines within the accordion

In my design, I have nested accordions and I am trying to create a graphical representation with vertical and horizontal lines connecting the elements. Here is the basic structure: <div id="accordion3" class="collapseblock"> <div class="cardsm ...

The binding between the model and the AngularJs ng-options on an HTML select element is not functioning

As a beginner in Angularjs, I've been struggling to make my first Angularjs screen work for the past few days. The screen is quite simple - just a jsp page with a couple of html select dropdowns. I have also set up three javascript modules: applicatio ...

What CSS style should be used to fill both the thumbs up and thumbs down icons of a glyphicon?

Using glyphicon icons instead of other icons (fa icons), I am able to change the color of the icon, but it does not completely fill the icon with color like Facebook's like button. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1 ...

Activate the Submission Button Once Validation is Complete

My form consists of multiple input fields with validations. Initially, the submit button is disabled. Each time I enter a field, the validation function should run immediately. After all fields are filled with valid input, the submit button should be enabl ...

What is causing the issue with using classes in Javascript to clear the form?

<!DOCTYPE html> <html> <head> <title>Onreset</title> </head> <body> <form> Username: <input type="text" class="abc"><br><br> Password: <input type ...

"Create a stunning backdrop with animated wave effects using JavaScript

I am currently using three.js to create a waves canvas animation, but I am unsure how to eliminate the background-color (only the background-color) of the canvas. if (WEBGL.isWebGLAvailable() === false) { document.body.appendChild(WEBGL.getWebGLErrorMes ...

Is the :root selector truly devoid of value?

While I understand that :root can be used to select the html tag in an HTML file and the svg tag in an SVG file, I personally find it strange and not practical to apply the same styles to both HTML and SVG using :root. It seems like an unrealistic scenario ...

The issue of a non-functional grid with scroll in a flexbox

I've encountered a problem while working with the grid layout using divs and flexbox. The header, which I want to be fixed, is overlapping with the first row and I'm struggling to get the scrolling behavior right. How can I address this issue? I ...

How can CSS incorporate the color of a different class?

Is it feasible to reference a color from one CSS class to another? Or generate a universal color definition that can be applied to multiple classes, eliminating the need to update in various places when changing a color? For instance, if I have a CSS clas ...

Switching up icons using React Spring - a step-by-step guide!

Is it possible to change the icon when I click on it using react spring? For example, if I click on " ...