How to style the background color of the first column in an HTML table that includes rowspan

Here is a simple example of a table. I want the header row to be grey, alternate rows below it to be yellow, and the first column to be orange. I'm having trouble achieving this with CSS. How can I do this without individually styling each cell?

View the code on JSFiddle

table,
th,
td {
  border: 1px solid black;
  border-collapse: collapse;
  text-align: center;
  padding: 5px;
}
table {
  width: 100%;
}
th {
  background-color: grey;
  color: white;
}
tr:nth-child(odd) {
  background-color: yellow;
}
<table>
  <col style="background-color: orange;" />

  <tr>
    <th>&nbsp;</th>
    <th>Bank</th>
    <th>Amount</th>
  </tr>
  <tr>
    <td>John</td>
    <td>ABC</td>
    <td>12345</td>
  </tr>
  <tr>
    <td rowspan="2">David</td>
    <td>DEF</td>
    <td>456789</td>
  </tr>
  <tr>
    <td>GHI</td>
    <td>147258</td>
  </tr>
  <tr>
    <td>Kevin</td>
    <td>JKL</td>
    <td>258369</td>
  </tr>
  <tr>
    <td>Mike</td>
    <td>MNO</td>
    <td>369258</td>
  </tr>
</table>

Answer №1

This might not be the most ideal fix, but it does the job: Check out this link for the solution

To address the issue, I included the following code:

tr td:first-child
{
    background-color: orange;
}
tr:nth-child(even) td:nth-last-child(2)
{
    background-color: white;
}
tr:nth-child(odd) td:nth-last-child(2)
{
    background-color: yellow;
}

If you plan on changing your rowspan to 3 or more, make sure to include the last selector mentioned above.

In the long run, it's recommended to use classes for the first column. This approach will simplify things and provide better flexibility when working with multiple columns in the future.

Answer №2

Check out this demo - http://jsfiddle.net/victor_007/sthmw0dk/3/

I made a small tweak by applying styles to td instead of tr using the property background-color.

If you use td:not(:first-child), the first child of tr will not receive any styling.

table,
th,
td {
  border: 1px solid black;
  border-collapse: collapse;
  text-align: center;
  padding: 5px;
}
table {
  width: 100%;
}
th {
  background-color: grey;
  color: white;
}
.first-col {
  background-color: orange;
}
tr:nth-child(odd) td:not(:first-child) {
  background-color: yellow;
}
<table>
  <colgroup>
    <col class="first-col" />
  </colgroup>
  <tr>
    <th>&nbsp;</th>
    <th>Bank</th>
    <th>Amount</th>
  </tr>
  <tr>
    <td>John</td>
    <td>ABC</td>
    <td>12345</td>
  </tr>
  <tr>
    <td rowspan="2">David</td>
    <td>DEF</td>
    <td>456789</td>
  </tr>
  <tr>
    <td>GHI</td>
    <td>147258</td>
  </tr>
  <tr>
    <td>Kevin</td>
    <td>JKL</td>
    <td>258369</td>
  </tr>
  <tr>
    <td>Mike</td>
    <td>MNO</td>
    <td>369258</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

Experiencing an issue with the left side menu bar in Bootstrap, looking to incorporate additional sub menu items

<li class="drop-menu-tab"> <a class="dropmenu" ><i class="icon-folder-close-alt"></i><span> Employee</span></a> <ul> <li><a class="submenu&q ...

The Datalist HTML does not function properly with line breaks

<input type="text" list="req" name="req" style="width:350px; height:70px;"><datalist id="req"> <option value=""><option> <?php while($getreq = $requirements->fetch_array()){ ?> <option><?=$req = preg_r ...

Can someone please explain the process of adding a hover effect to buttons?

Could someone please assist me with creating a button effect for the image buttons I have made? Is there a CSS solution available or do I need to create different buttons altogether? Your help is greatly appreciated as I am in desperate need of this effe ...

Twitter bootstrap does not support the display of Glyphicons

Having trouble getting glyphicons to display properly. Currently, a small square is appearing instead of the glyphicon symbol. I have attempted to reposition folders and troubleshoot without success. I did not customize bootstrap - I simply downloaded the ...

Fixing JavaScript Image Swap Bugs

Seeking assistance with a JavaScript Image Swap creation. Can anyone provide guidance on identifying coding errors in my JavaScript? Any help or pointers on correct usage of getElementById would be greatly appreciated! <title>Photo Gallery</tit ...

The behavior of event propagation across different browsers when the event target is removed from the DOM

We've encountered a cross-browser problem with the behavior of Bootstrap-enhanced anchors that include a span element. After examining the Bootstrap JavaScript, I've condensed the issue into the following code snippet (which does not rely on Boot ...

Guide on positioning an SVG button within a form

I am attempting to design a form with two input fields and a button. Rather than using a plain text button, I prefer to utilize an SVG graphic for the button; however, it is not aligning correctly. Consider the following HTML and CSS: body { backg ...

What is the best way to insert a hint into an asp:textbox?

Is there a method to insert a hint or placeholder text within an asp:TextBox element? I am looking for a way to have text appear in the textbox that disappears when the user clicks on it. Can this be done using html / css? ...

No specified margin at the top of the website's header section

My task was to design the header section of a webpage to resemble this desired webpage look. I started by creating a "header" tag as a container and added a navbar within it. To display the items, I used an unordered list with CSS adjustments for a horizon ...

Locate elements with Jquery after displaying HTML content

I am encountering an issue with the code snippet below: tblBody = $('#tbody'); tblBody.html(rowsStr.join('')); var lines = tblBody.find("tr"); The array rowsStr contains strings that create tr and td tags. Sometimes, when I use tblB ...

Guide to ensuring only one Accordion opens at a time in Elementor

I created a custom accordion in elementor with CSS and JavaScript, but I'm facing an issue where the tabs stay open when others are opened. How can I modify the code to make sure only one tab is open at a time? Below is the CSS code snippet: body ...

Interact with a shared array across multiple HTML pages using JavaScript

Currently, I am developing a web application that involves pushing values into an array through a JavaScript function on one HTML page and then accessing the same array from another JavaScript function on a different HTML page. Here is the code snippet I ...

Tips for optimizing Markdown to HTML conversion for improved SEO in Pagedown editor

Through the pagedown editor, I am transforming markdown into HTML using the method below: <div id="question_div" class="post_text"> </div> <script type="text/javascript"> $(document).ready(function() { var q_converter = new Markdo ...

Angular2 faces a challenge with the selection issue

I have created a Plunker code for you to review. If you click the "toggle show" button twice, the selected item should be displayed. import {Component} from '@angular/core'; @Component({ selector: 'my-app', template: `<div *ngI ...

Execute a script upon page load

Is there a way to trigger a script tag <script> immediately upon the website loading? I've experimented with various codes, but haven't found one that meets my needs. ...

"Utilize NodeJs and Multer to easily upload a variety of file types in a single form to Cloudinary

Currently, I am attempting to upload two different files within a single form in NodeJs utilizing multer and cloudinary with the .fields(fields) method. Here is an example: This is the form: <form action="/requestsList" method="POST" enctype="multipar ...

Center column with header and footer flanking each side

Trying to replicate the layout of the Facebook Android app on my page. The app features a 3 column design, with the central column exclusively displaying the header (no footer included, although I require one for my version). This visual representation is ...

What is the method to send an email with an embedded image using C# without the presence of ATT00001.bin file

Currently, I can successfully send emails using SMTP in C# and even include images as LinkedResource. However, the issue arises when recipients receive emails with ATT0000X.bin files attached. How can I prevent these bin files from being included in the em ...

How to Preserve Scroll Position when Toggling a Div using jQuery

I've been struggling to find a solution for maintaining the scroll position of my page after a JQUERY toggle event. I've searched extensively but haven't found a fix yet. <script src="Scripts/_hideShowDiv/jquery-1.3.2.min.js" type="text/ ...

Error: The property 'length' cannot be read from an undefined parent causing Uncaught TypeError

Hey there, take a look at this cool stuff http://jsfiddle.net/J9Tza/ <form class="validation"> <div> <input type="email" class="form-control" id="inputEmail" name="email" placeholder="Email" pattern=".{3,200}" title="3 to 200 characters" r ...