Is there a way to choose columns 2 to 6 of the row that is currently selected

I am looking to create a hover effect for columns 1 through 7 of a row where, when the user hovers over any column within that range, certain styles are applied to all columns in the row except for columns 1 and 7. This means that the background color should change on the current row's columns 2 through 6, while excluding columns 1 and 7:

tbody tr:hover > td:not(:first-child):not(:last-child) {
    cursor: pointer; background-color: rgb(221, 221, 221);
}

In addition, I want to have a similar hover style on columns 1 and 7 as well. To clarify, the desired behavior is as follows:

  1. When the user hovers over row x, column 1, only that single cell should get the hover effect.
  2. Similar to the above point, when the user hovers over row x, column 7, only that single cell should get the hover effect.
  3. If the user hovers over row x, any column from 2-6, then all cells in row x with columns 2 through 6 should receive the hover effect.

Answer №1

Here's a straightforward solution as suggested by "Pete": css:

tbody tr:not(:first-child):not(:last-child){
    cursor: pointer;
    background-color: rgb(221, 221, 221);
}

This applies to td elements in every row.

tbody tr td:not(:first-child):not(:last-child){
    cursor: pointer;
    background-color: rgb(221, 221, 221);
}

To achieve this for cells 2 to 6 using a specific class for tds, and regular css for the first child and last child, we can use some jQuery.

$("table.mytable").find('td.a')
  .on( "mouseenter", function() {
  var allaclass = $(this).parent().find('td.a');
    allaclass.css("background-color", "red");
  })
  .on( "mouseleave", function() {
    var allaclass = $(this).parent().find('td.a');
    allaclass.css("background-color", "white");
  });
tbody tr td:first-child:hover,
tbody tr td:last-child:hover{
    cursor: pointer;
    background-color: rgb(221, 221, 221);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<table class="mytable"> 
  <tbody>
    <tr>
      <td>1</td>
      <td class="a">2</td>
      <td class="a">3</td>
      <td class="a">4</td>
      <td class="a">5</td>
      <td class="a">6</td>
      <td>7</td>
    </tr>
    <tr>
      <td>1</td>
      <td class="a">2</td>
      <td class="a">3</td>
      <td class="a">4</td>
      <td class="a">5</td>
      <td class="a">6</td>
      <td>7</td>
    </tr>
  </tbody>
</table>

Answer №2

Utilize the hover effect on the parent element.

tbody:hover tr:not(:nth-child(1)):not(:nth-child(7)) {
    cursor: pointer;
    background-color: rgb(221, 221, 221);
}

tbody:hover tr:not(:nth-child(1)):not(:nth-child(7)) {
    cursor: pointer;
    background-color: rgb(221, 221, 221);
}
<table>
  <tbody>
    <tr>
      <td>1</td>
    </tr>
    <tr>
      <td>2</td>
    </tr>
    <tr>
      <td>3</td>
    </tr>
    <tr>
      <td>4</td>
    </tr>
    <tr>
      <td>5</td>
    </tr>
    <tr>
      <td>6</td>
    </tr>
    <tr>
      <td>7</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

What is the best way to send props to a styled component without needing to convert them to transient props beforehand

Recently, I designed a custom Text component that accepts several props. These props are then forwarded to the styled component where specific styles are applied. However, I am facing an issue where I do not want these props to be passed down to the DOM, b ...

Looking to transform an HTML table into a stylish CSS layout complete with a form submission button?

Recently, I've been delving into the world of CSS and PHP as I work on converting old code entirely into HTML and PHP with a touch of CSS. The visual aspect seems fine, but I've hit a roadblock with the submit form due to an IF statement in PHP. ...

Change the size of a particular div upon hovering over another element

I'm trying to figure out how to scale my div with text when a user hovers over a button, but I've tried various operators like >, ~, +, - with no success. section { background: #000; color:#fff; height: 1000px; padding: 150px 0; font-family: R ...

Place the button inside the form following another block element

Here is the HTML code I am working with: <div class="actions"> <input id="submit-car" name="commit" type="submit" value="Добавить объявление"> </div> and here is a fiddle example: http://jsfiddle.net/348U4/ In ...

I am interested in designing a navigation bar with varying background and hover colors for each individual block

I need help creating a navigation bar with different background colors and hover colors for each block. I can do this separately but not together, so please advise on how to combine both in the li class. Below is the code for the navigation bar: header ...

Position the paper-icon-button so that it is aligned to the top-right corner of the scaffold

Looking for a way to... <core-scaffold id="core_scaffold"> <core-header-panel mode="seamed" id="core_header_panel" navigation flex> <core-toolbar id="core_toolbar"></core-toolbar> <core-menu valueattr="label" ...

Animating Elements with CSS and HTML

Struggling with my website project, specifically creating a login page. The issue arises when resizing the browser, causing elements to look misaligned and chaotic. Before: view image here After: view image here /* Bordered form */ form { margin-l ...

Tips for removing the default hover and click effects from a Material-UI CardActionArea

Check out the card sample with a lizard photo on https://material-ui.com/components/cards. When you hover over the cardActionArea, it will get darker or lighter based on the theme. Clicking on the card will make the picture change its brightness relative ...

The arrows on the Slick Slider appear cropped on ios devices like the iphone 8, however, they display perfectly when tested on Chrome and Firefox

I am currently facing an issue with my slick slider setup at https://www.labtag.com/shop/product/clear-cryogenic-labels-for-frozen-vials-1-75-x-1-aha-224/. It is configured for 4 slides on desktop and 2 slides on mobile devices (768px breakpoint). The pr ...

Placing a pseudoelement amidst the track and thumb of a range input type

I'm trying to position a pseudo element with a specific z index that is lower than the thumb of an input type range but higher than its track. This is what I have so far: HTML: <div class="range"> <input type="range" name="" class="progre ...

Guide on how to organize a list into three columns using a single ul tag

Is there a way to split a ul list into 3 columns using CSS? This is how my HTML structure looks like: <ul> <li>Test</li> <li>Test</li> <li>Test</li> <li>Test</li> <li> ...

Strategies for limiting a table row in JavaScript or jQuery without utilizing the style tag or class attribute for that specific row

I am looking for a way to limit the display of records in a table. Currently, I can restrict the table rows using the style property, but this causes UI issues such as missing mouse-over effects for the entire row. I need to ensure that mouse-over functi ...

What is the process for extracting content from CSS comments or annotations in a stylesheet?

Here's an interesting concept: allowing users to define a set of CSS rules with annotations. For example: /* @name Page style */ body { font: 16px/1.5 Arial; /* @editable */ background-color: #fff; /* @editable */ } /* @name Section header */ ...

Adjust the size of an HTML image for printing using a JavaScript window.print() function

On my website, I have a print option set up where specific elements are hidden using @media. How can I adjust the size of an image within the @media query when my JavaScript print function is triggered? I am able to modify a regular div element easily, but ...

The hover drop-down menu in CSS is not appearing in the correct position

Having trouble with my navigation bar - the dropdown menu isn't showing up below the category when I hover over it. It keeps ending up in the left-hand corner and I can't access it! I've searched everywhere for a solution, but can't fin ...

What causes my absolutely positioned element to disappear when using overflow-x: hidden?

I've been experimenting with the CSS property overflow-x: hidden on a container and noticed that it causes my element with position: absolute to disappear. To see this effect in action, check out this demo: https://codepen.io/Karina1703/pen/LYMzqEj ...

I can't seem to get the button to function properly, and it's really

I am having an issue with my "Let's Get Fit" button. When I hover over it or click it, the cursor doesn't change and it doesn't lead to index.html. I'm not sure why it's not working. .btn { display: inline-block; text-transf ...

What is the best method for flipping the sequence of elements in media queries?

I am facing an issue with two divs, left and right, on my webpage. When the screen size is less than 500px, I want the left div to move to the bottom and the right div to move to the top. Essentially, I need to swap the positions of these divs in the DOM. ...

Crafting CSS for styling input type file elements

My attempts to style the input type file with CSS have been unsuccessful. In my current code, the "choose file" button is displaying above my styled button. How can I use CSS to remove this? I am aiming to only display a blue colored button for uploading ...

What should we name this particular style of navigation tab menu?

What is the name of this tab menu that includes options for next and previous buttons? Additionally, is there a material-ui component available for it? ...