When row highlights overshadow column outlines in an HTML table with CSS styling

I implemented a modified version of ThinkingStiff's solution to a question related to columns, colgroups, and CSS :hover psuedoclass. The result is a table that clearly outlines columns and highlights rows when hovered over. (Note: Vertical column headers are purposely left unoutlined).

Take a look at the code snippet below:

table {
  overflow: hidden;
}
caption {
  background-color: #fff;
  padding: 0px
}
table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
  text-align: center;
  width: 100%
}
th, td {
    width: 100px;  
    height: 50px;
    position: relative;
}
tr:hover {
  background-color: #FBFEFD;
}

td:hover::after {
  content: "";
  position: absolute;
  outline: solid black 2px !important;
  left: 0;
  top: -5000px;
  height: 10000px;
  width: 100%;
  z-index: -1;
}
<table class="ComparisonTable">
<caption>Comparison Table</caption>
  <tbody>
    // table rows with respective data cells
  </tbody>
</table>

Can someone explain why the column outline disappears for the selected row? I thought using the !important tag would retain the outline, but it seems not to be the case. Are there any other solutions to keep that outline intact?

As an additional query, why am I experiencing the loss of the rightmost border in the table (although this is less concerning)?

Many thanks in advance for your help!

Answer №1

After some experimentation, I believe I have found a viable solution for your problem.

table {
  overflow: hidden;
  overflow-x: visible;
  display: block;
}

tbody {
  overflow-y: hidden;
  display: block;
  margin-top: 19px;
  position: relative;
}

caption {
  background-color: #fff;
  padding: 0px;
  width: 617px;
  z-index: 20;
  position: absolute;
  border: 1px solid black;
  border-bottom: 0;
}

table,
th,
td {
  border-collapse: collapse;
  text-align: center;
  width: 100%
}

th,
td {
  width: 100px;
  height: 50px;
  position: relative;
  border: 1px solid black;
}

tr:hover {
  background-color: #FBFEFD;
}

td:hover::after {
  content: "";
  position: absolute;
  outline: solid black 2px !important;
  left: 0;
  top: -5000px;
  height: 10000px;
  width: 100%;
  /*z-index: -1;*/
}

Check out this JSFiddle example for reference: https://jsfiddle.net/jennifergoncalves/rntyt13u/2/

If further adjustments are required, feel free to reach out for additional assistance.

Answer №2

Although a pure CSS solution may not be available, I managed to solve it quickly with a little bit of JavaScript.

To prevent the text from shifting (like a border would) or highlighting entire cells (like an outline would), I utilized box-shadow.

$('td').hover(function() {
    var t = parseInt($(this).index()) + 1;
    $('td:nth-child(' + t + ')').addClass('ct-col-highlighted');
}, 
function() {
    var t = parseInt($(this).index()) + 1;
    $('td:nth-child(' + t + ')').removeClass('ct-col-highlighted');
});
.comparison-table {
  border: 1px solid black;
  border-collapse: collapse;
  width: 100%;
  text-align: center;
}

.comparison-table caption {
  padding: 15px
}

.comparison-table th, td {
  border: 1px solid black;
  width: auto; 
  height: 50px;
}

.comparison-table tr:hover {
  background-color: #FBFEFD;
}

.ct-col-highlighted {
box-shadow: -2px 0px 0px 0px black, 3px 0px 0px 0px black;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class='comparison-table'>
<caption>Caption</caption>
<tr>
<th>H-A</th>
<td>A1 </td>
<td>A2</td>
<td>A3</td>
</tr>
<tr>
<th>H-B</th>
<td>B1</td>
<td>B2</td>
<td>B3</td>
</tr>
<tr>
<th>H-C</th>
<td>C1</td>
<td>C2</td>
<td>C3</td>
</tr>
<tr>
<th>H-D</th>
<td>D1</td>
<td>D2</td>
<td>D3</td>
</tr>
</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

Maintaining fixed panels as you scroll through a centrally aligned layout

I am looking to create a layout with three divs set up as columns. The outer two will be for navigation and need to stay in place while the user scrolls, but the middle section will contain content that should scroll normally along with the page. I know t ...

Incorporate my personal design flair when utilizing third-party React.js component libraries

Incorporating Material UI as a component library in my React project has been beneficial. However, I am facing a challenge where the inline styling provided by Material UI clashes with my existing custom styles that I have developed over time. Is there a ...

Increased space between the sidebar and the top bar

Currently, my code is a bit messy as I am still in the learning stages of bootstrap. I need some assistance on how to resolve the empty space that exists between the top and side bar elements. <!DOCTYPE html> <html lang="en"> <sty ...

Having trouble getting custom select to work with CSS in Firefox?

I want to customize the button (down arrow) located in the right corner of the select box. The HTML code is as follows: <div class="buscaSelect"> <select name="oper"> <option value="value1">Value 1</option> < ...

Express Node + Styling with CSS

I am in need of a single EJS file (configuration includes Express and Request). app.get('/space', function(req, res){ res.render('space.ejs'); }); The file successfully renders, but only two out of three CSS stylesheet links work ...

What causes certain div content to be absent from ZoneTemplate within an ASP.NET web part?

Check out this snippet of code: <asp:WebPartZone ID="Zone1" runat="server" Width="100%" PartChromeType="None" Padding="0" PartStyle-CssClass="NoPadding" PartStyle-BackColor="Transparent" BackColor="Transparent" PartChromeStyle-BackColor ...

Fetching data in VueJs before redirecting to a new page

Within the mounted function, I am creating an action that fetches data from a Rest API and populates my table in a Vue.js component mounted() { UserService.getProjects().then( (response) => { this.isProject = true; this.project ...

Having trouble with Font Awesome icons displaying correctly in Firefox, while they work perfectly fine in Chrome and

Having some issues with the website: - specifically, the cart icon and the down arrows in the navigation menu aren't displaying properly in Firefox. They show up fine in Edge and Chrome though. I attempted to fix the problem by including <link hr ...

Substitute the identifier with an element for a specific webpage

Currently, my navigation consists of two list items: <body id="trips"> <nav> <li><a href="/trips.php" class="trips">Trips</a></li> <li><a href="/trips.php?offer=true" class="offers">Offers< ...

Guide to placing the Drawer component beneath the AppBar in Material-UI

Exploring the capabilities of the Material UI drawer component, I'm looking to position the drawer below the appbar. My goal is to have the hamburger icon toggle the drawer open and closed when clicked: opening the drawer downwards without moving the ...

Conceal a designated H2 identifier through the use of either javascript or CSS

We are currently exploring Zendesk for our customer support website, but we have encountered some limitations in terms of customization. Our goal is to remove specific text from the page by utilizing Zendesk's widgets function, which can be implemente ...

Creating a Basic Slideshow using Javascript

I've been attempting to create a slideshow using only JavaScript, but I've run into an issue. When I set the divs to change on click, it works perfectly fine. However, when I try to set it to change at specific intervals, it doesn't work. ...

Unable to see the toggle button on the navigation bar

My code for a toggle button on the navbar using bootstrap4 is not displaying when the browser screen size is reduced. Can someone please help me identify what's wrong with my code? <nav class="navbar navbar-expand-lg"> <button class="na ...

Maintaining an element's vertical position in relation to the screen with jQuery

I'm very close to achieving what I want, but my goal is a bit unconventional. My setup includes several panels divided into 2 columns, with each panel capable of triggering an expand/compress toggle. My challenge is maintaining the vertical position ...

Is it feasible to exclude certain CSS files in an HTML Master page within the .NET framework?

On my website, I have 6 CSS files linked, but on a specific page, I only need to use 3 of them. Our developers are using a master page in .NET and are hesitant to make changes to it. Therefore, I am wondering if there is a way to exclude certain linked C ...

Adjust the size of the buttons based on the width of the screen

I have successfully implemented a dropdown menu with both text and an icon. However, I am now faced with the challenge of removing the text while retaining the icon when the screen width is reduced to a maximum of 768px. Is it possible to modify the conten ...

How can we briefly make the list item color and size return to default after clicking the Bootstrap navbar-toggler button?

.navbar .collapse .navbar-nav .nav-item .nav-link { font-family: 'yekan'; color: #fff; opacity: .8; font-size: .9rem; padding-left: 15px; padding-right: 15px } <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery. ...

Issue with displaying jQuery class names accurately

I am implementing jquery.scrollablecombo.js for a dropdown menu using jQuery. However, when I include it in my HTML code, the class attribute is displayed as "classname" instead of "class." See the example below: <div classname="searchBar cb_selectMain ...

What is the best way to transform height in pixels to percentage in order to create a responsive design that adjusts smoothly on

Currently, I am in the process of developing a responsive grid system by using a max-width of 980px to convert my fixed PX widths to percentages. However, I am facing some challenges in updating the heights that are also set in PX to %. Could anyone provid ...

Unraveling the Enigma of CSS Grid Whitespace

I'm facing an issue while trying to construct a grid of images using CSS Grid. Interestingly, I am encountering mysterious white space around the first two items in the grid. Upon inspecting the images, I am unable to identify any source of this white ...