Grouping and styling table data cells within a row: best practices

I am attempting to "group" a collection of cells/<td> elements within a row/<tr> in a <table> in order to apply styles to them. The code below accomplishes what I desire to some extent, but it does have its limitations:

/* Issues with styling */
.row td:not(:first-child) {
  background-color: lightgray;
  /* possibly a box-shadow */
}

.row td:nth-child(2) {
  border-top-left-radius: 10px;
  border-bottom-left-radius: 10px;
}

.row td:last-child {
  border-top-right-radius: 10px;
  border-bottom-right-radius: 10px;
}


/* Fundamental table styles for a decent appearance */
table {
  margin: 0 auto;
  font-size: 20px;
  border-collapse: separate;
  border-spacing: 0 1em; 
}
table td {
  padding: 10px;
}
<table>
  <thead>
    <th>A</th>
    <th>B</th>
    <th>C</th>
    <th>D</th>
  </thead>
  <tbody>
    <tr class="row">
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
    </tr>
    <tr class="row">
      <td>5</td>
      <td>6</td>
      <td>7</td>
      <td>8</td>
    </tr>
  </tbody>
</table>

The above method achieves the visual effect I desire (except for a box-shadow surrounding the grey box for each group), where the last 3 cells of each row are grouped together. However, there are a few drawbacks:

  1. Styling the last three cells individually requires changing the style of each <td>, making styling laborious (e.g., specifying border radius for each corner rather than one simple border-radius style for the entire group).

  2. Styling the table this way makes it difficult to add cell spacing between each cell or create a box-shadow that surrounds the entire grey box grouping the last three cells. Applying box-shadow to each <td> separately results in each cell appearing distinct, unlike the desired look of a single shadow around the group.

Is there a way to collectively group <td>s and apply styles holistically to the group instead of individual cells? Ideally, I would like to:

<tr class="row">
  <td>1</td>
  <div class="row-group">
    <td>2</td>
    <td>3</td>
    <td>4</td>
  </div>
</tr>

Then, I could apply border-radius: 10px, background-color: lightgray, and box-shadow styles to the .row-group class. Unfortunately, the HTML specification does not allow <div> elements within <tr>s, rendering this approach ineffective. Exploring <colgroup> and <col> tags also proves unhelpful as they primarily affect column-wise styling rather than grouping cells per row. Another consideration is using a sub-table, although I wonder if there exists a simpler solution to achieve grouping and styling these <td>s collectively, potentially utilizing <col>s?

Answer №1

Here is my response posted as an answer per your instruction.

To achieve the desired look of a rounded grey box, you can utilize the :after pseudo-element with specific styling properties. By positioning it absolutely under the TD's, it will span the full row width while excluding the width of the first:child.

/* Resolve style concerns */
tbody tr {position:relative;}
tbody tr:after {
  content:'';
  display:block;
  position:absolute;
  top:0;
  right:0;
  bottom:0;
  left:30px; /*Width of first child*/
  margin:auto;
  background-color: lightgray;
  border-radius:10px;
}
td {position:relative; z-index:1}
/* Foundational table styles for visual appeal */
table {
  margin: 0 auto;
  font-size: 20px;
  border-collapse: separate;
  border-spacing: 0 1em; 
}
table td {
  padding: 10px;
}
<table>
  <thead>
    <th>A</th>
    <th>B</th>
    <th>C</th>
    <th>D</th>
  </thead>
  <tbody>
    <tr class="row">
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
    </tr>
    <tr class="row">
      <td>5</td>
      <td>6</td>
      <td>7</td>
      <td>8</td>
    </tr>
  </tbody>
</table>

Update: Same snippet but now with additional jquery script to cater for dynamic width of the first-child:

$(document).ready(function(){
    var tdWidth = $("td:first-child").outerWidth();
  $( "<style>tbody tr:after { left: " + tdWidth + "px; }</style>" ).appendTo( "head" )
});
/* Resolve style concerns */
tbody tr {position:relative;}
tbody tr:after {
  content:'';
  display:block;
  position:absolute;
  top:0;
  right:0;
  bottom:0;
  margin:auto;
  background-color: lightgray;
  border-radius:10px;
}
td {position:relative; z-index:1}
/* Foundational table styles for visual appeal */
table {
  margin: 0 auto;
  font-size: 20px;
  border-collapse: separate;
  border-spacing: 0 1em; 
}
table td {
  padding: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table>
  <thead>
    <th>A</th>
    <th>B</th>
    <th>C</th>
    <th>D</th>
  </thead>
  <tbody>
    <tr class="row">
      <td>1111111</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
    </tr>
    <tr class="row">
      <td>5</td>
      <td>6</td>
      <td>7</td>
      <td>8</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

Stop the "float:right" div from taking up all available space

I encountered an issue while developing a reddit clone. Just to clarify, I'm using bootstrap v5.0 The problem arises when trying to position the sidebar on the right and the content on the left. The layout seems fine at first, but the content sectio ...

Is it a not-so-great idea to use a combination of Bootstrap CSS and Custom CSS for styling

Is it considered bad practice to apply custom CSS on top of standard Bootstrap CSS for a button? Should I completely remove Bootstrap and use only custom CSS? If so, what is the trade-off in terms of performance? Another web developer mentioned that addit ...

Clicking the Less/More button using jQuery

In my current setup, I have a code snippet that loads and displays two comments initially. There is also a button labeled More comments which, when clicked, fetches and shows the remaining comments using AJAX. Upon clicking the More comments button, it tr ...

Utilizing Bootstrap 5: Breaking a Page into Two Separate Vertical Sections

I am looking to design a unique grid system that divides the page into either 2 columns or 2 rows that are completely separate from each other. This means that the content on one side of the page does not expand to match the height of the content on the ot ...

Bootstrap modal experiencing technical difficulties

I am experiencing issues with my bootstrap modal. It seems to be malfunctioning, almost as if the CSS or JavaScript files are not being recognized. I have tried various solutions but have been unable to resolve the problem. I even attempted using the examp ...

The POST request is coming back as null, even though it includes both the name and

Having issues with a login PHP code where the post method is returning an empty string. Here is my HTML code: <form action="iniSesion.php" method="post"> <div class="form-group"> <input type="email" name="email_" class ...

Menu options are unresponsive to clicks in the dropdown

Need help fixing my dropdown menu issue. Whenever I hover over the dropdown, it disappears before I can click on any items. Here is a snippet of the code causing the problem: #navContainer { margin: 0; padding: 0; padding-top: 17px; width: 220 ...

Looking to organize my divs by data attributes when clicked - how can I achieve this with javascript?

I am looking to implement a dropdown sorting functionality for multiple divs based on different data attributes such as price and popularity. The specific divs are labeled as "element-1" and are contained within the "board-container". var divList = $(". ...

Exploring deep nested elements with Beautiful Soup in Python

My webpage has a specific layout: <div id ="a"> <table> <td> <!-- many tables and divs here --> </td> <td> <table></table> <table></t ...

Learn the method to animate changing the background-color of an element using a click function

Can you create an animation for changing the background color when clicking on an element? Most examples I've found use mouseover/hover functions, but none with a click function. The following line changes the background color without any animation: ...

Button press triggers the fadeIn function successfully, but the keypress event does not have the same effect

I'm currently facing an issue with two div elements on my webpage. I want only one of them to be visible at a time, depending on which key is pressed. If the 1 key is pressed, I want 'div1' to fadeIn (if it's not already visible) and fo ...

How can I combine HTML and CSS in a single request using the sendFile method in Express?

const app = require('express')(); app.get('/', function(req, res) { res.sendFile(__dirname + "/" + "index.html"); }); <link rel="stylesheet" href="style.css"> I utilized the Node.js code above to send an HTML file. In order to ...

Implement varying styles in React components

In my React project, I am attempting to create a unique progress bar with custom styling. Specifically, I have a dynamically calculated percentage that I want to assign as the width of a div element. Initially, I tried achieving this using Tailwind CSS: &l ...

Submitting form data from an HTML page to a specific Skype account is a straightforward process that can be easily accomplished

[Situation] A new feature is being considered for a feedback form that would allow users to input their name, email address, subject, and message. A unique option being proposed is the ability for users to submit their messages via Skype. If this option is ...

Angular ensures that the fixed display element matches the size of its neighboring sibling

I have a unique challenge where I want to fix a div to the bottom of the screen, but its width should always match the content it scrolls past. Visualize the scenario in this image: https://i.sstatic.net/i7eZT.png The issue arises when setting the div&apo ...

How to add a new row to a Kendo grid with a unique custom styling

I am looking for a way to insert new data into a Kendo grid, but I want the added row to have a custom class so that I can style it with a different background color. How can I achieve this? I have searched through all the documentation but couldn't f ...

Incorporate an animated loading GIF image within the ajaxStart function, dynamically appending it within a div tag

Is there a way to dynamically display a loading GIF image on all HTML web pages? var loading = $(' <div id="loading"> <img src="../img/loading%20(1).gif" id="loading-image" alt="Loading..." /> </div>'); $(document).ajaxStart( ...

Arranging Form Elements with Bootstrap styling

Check out the Bootply I created to demonstrate an issue I'm facing: http://www.bootply.com/8fPmr31Wd7 When scrolling down, you'll notice that "People" and "Places" are indented on the sides. It's possible that I'm not using the correc ...

css readonly input textbox not changing background color when set

I've come across an old Classic ASP form that requires additional functionality, and I'm currently using IE11. To address this, I decided to include a doctype like so (even though I'm unsure of its necessity): <!DOCTYPE html> In my C ...

Resetting a component's color to its default state after being styled by a parent selector

The Bootstrap stylesheet contains the following code: pre code { color: inherit; } Even if I include an explicit pre code { color: red; }, the style from Bootstrap's stylesheet still takes priority. Why does this happen? [Edit: can ignore this ...