How can I apply specific styling to certain cells within a table?

Is there a way to apply styles specifically to certain table headers in an HTML table without altering the HTML or introducing JavaScript? I need to target only 4 out of the total 8 table headers.

To see the code and example for this question, visit: https://jsfiddle.net/gk0bwtvs/38/

table tr td {
  padding: 5px;
}
table {
  border: pink 5px;
  border-style: double;
}
table tr:nth-child(even) {
  background-color: #aaa;
}
<table>
  <tr>
    <td>&nbsp;</td>
    <th>January</th>
    <th>March</th>
    <th>June</th>
    <th>September</th>
  </tr>
  <tr>
    <th>Temperature</th>
    <td>Cold</td>
    <td>Maple syrup season!</td>
    <td>Warmer</td>
    <td>Cooling down</td>
  </tr>
  <tr>
    <th>Precipitation</th>
    <td>Snow</td>
    <td>Mud</td>
    <td>Rain</td>
    <td>Rain, but not for long</td>
  </tr>
  <tr>
    <th>Amount of Daylight</th>
    <td>Very little</td>
    <td>Even daylight and night</td>
    <td>Lots of daylight</td>
    <td>Even daylight and night</td>
  </tr>
  <tr>
    <th>Recommended Outerwear</th>
    <td>Heavy jacket</td>
    <td>It depends!</td>
    <td>Usually no jacket</td>
    <td>Light jacket sometimes</td>
  </tr>
</table>

I'm considering using:

table tr th+td:not(th+th) {
  color: green;
}

or perhaps:

table tr th+td {
  color: green;
}

However, neither of these approaches seem to be effective.

The goal is to style the headers "Temperature," "Precipitation," "Amount of Daylight," and "Recommended Outerwear" while excluding the month headers. It's also important to avoid using nth-child or nth-of-type selectors for this task.

UPDATE: Answer has been selected, thank you!

Answer №1

After reviewing the jsfiddle you provided, I found the perfect selector:

table tr + tr > th {
  background-color: red;
  color: #FFF;
}

Explanation: This selector skips the first tr and targets the tr immediately following another tr, selecting only the direct child th.

No nth-child or nth-of-type functions are utilized in this case. I hope this clarifies things for you.

Answer №2

One helpful solution is to utilize suedo selectors for styling.

table tr th:first-child{
      background-color: green;
    }

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

Is it possible for web bots to artificially inflate the number of downloads?

I am managing a PHP program that is linked to a MYSQL database on a website. The program functions by retrieving an integer field from the database, incrementing it, and then updating the number to keep track of the downloads. While the program itself work ...

The issue of AngularJS failing to bind object properties to the template or HTML element

Just dipping my toes into angularJS, following Todd Motto's tutorials, and I'm having trouble displaying object properties on the page. function AddCurrentJobs($scope){ $scope.jobinfo = [{ title: 'Building Shed', description: ...

CSS: Using absolute and relative positioning within a parent element can result in elements overlapping

I want to create a box in html/css where the read more hyperlink text is positioned over the background image. The goal is to have additional text added to the description without overlapping the image. Here's an example of the current issue. Curren ...

Navigate the conversation within the dialog without affecting the content below

Is it possible to have a dialog with scrollable content on top of a page that is also scrollable, but when trying to scroll inside the dialog using the mouse wheel, only the dialog body scrolls and not the page below it? What approach can be used to accom ...

Tips for customizing bootstrap's fixed navbar-default to ensure that the list items align downwards:

Is it possible to customize the bootstrap fixed navbar-default so that the li elements align downward instead of at the top? If you have any corrections or custom solutions, I'd love to see them! Feel free to share your code on platforms like CodePen, ...

Utilizing the empty space to the right of an image in HTML

What could be causing the empty space to the right of my image? This is how the HTML file appears: ... <body> <div class="image"> <img src="image.jpg" alt="Picture here" id="image" align="middle"></img> </div> ...

Animating numerous elements simultaneously during Vue component rendering

Take a look at this simple case in the following jsfiddle: https://jsfiddle.net/hsak2rdu/ I attempted to swap and animate two elements, but it didn't work as expected. When you click the toggle button in the playground, the second element quickly mo ...

Elevate the Appearance of Material UI Elements with custom CSS Sty

I am currently facing an issue while trying to customize the styling of a Material UI component using CSS. Here is the code snippet: <IconButton className="my-class"> <Close /> </IconButton> CSS: .my-class { float: right ...

Changing the appearance of a radio button dynamically upon clicking

I am currently working on a dynamic pickup date form that utilizes radio buttons. My goal is to change the style of the selected value when a user clicks on it. Below is the code I have tried, but it has not been successful: foreach ($period as $day){ ech ...

Loop through a list of form names using ng-repeat and send each form name to a JavaScript function when clicked

I have multiple forms within an ng-repeat loop, each with a different name. I need to pass the form data when a button inside the form is clicked. However, when I try to call it like this: checkSaveDisable(updateProductSale_{{productIndex}}) it thro ...

Extract the entire div including all its elements and then transmit it using the PHP mail function

Currently, I am developing a feature that allows users to send an email. The email should include only one div from the page which contains elements added using the jQuery drag and clone function. I am unsure how to copy the entire div along with its child ...

Tips for expanding the width of an image when employing position:relative

Below is the code snippet: <table border="1" cellpadding="2" cellspacing="2" height="250"> <tbody> <tr> <td> <div style="background: url('path to image'); width: 300px; height: 250px; position: ...

The color of the progress bar in JS is not displaying properly

My work involves using jQuery to manipulate progress bars. The issue I am facing is defining standard colors that should be displayed on the progress bar based on the value received. Here is my code: var defaultSegmentColors = ['#FF6363', &ap ...

Reset the child JSP page to its original appearance

Displayed below is a JSP page: <div id="tabs-7" style="width: 100%;"> <form:form id="deviceForm" name="" modelAttribute="" enctype="multipart/form-data"> <div class="inputWidgetContainer"> <div class="inputWidget"> <table> ...

the combination of class and type functions as a jquery selector

<button class="actionButton default" type="submit"> Save</button> Can you select this button by both its class and type using a jQuery selector? I attempted the following, but it did not work: var saveBttn = $('.actionButton.default [ty ...

Activate an event on a separate webpage using jQuery or JavaScript

Currently, I am in the process of working on a project with 2 distinct pages: Index Settings On the Settings page, I have implemented a button to close an element and hide it. However, I am encountering an issue where I want the elements on the Index pa ...

The background color property of the CSS class "ui-layout-north

Currently working with primefaces 4.3 and incorporating it into my main UI. My goal is to modify the background color of the north unit to red, as currently only the top 80% of the unit is affected. .ui-layout-north .ui-layout-unit-content { backgro ...

Shapes and their corresponding encoded representations compiled in a comprehensive catalog

Can anyone provide me with a comprehensive list of HTML escaped codes for displaying various shapes on web pages? I need codes that are universally compatible with all browsers. For instance, I would like to display a square using an escaped code in one ...

What is the best way to create JavaScript code specifically for devices with a maximum width of 520px?

Is there a way to apply this JavaScript code specifically to devices with a maximum width of 520px? I could use some guidance on how to achieve this. // Apply code for max-width = 520px const myBtn = document.getElementById("darktheme"); const ...

Execute JavaScript code via URL injection

One interesting aspect of the HTML is that it has a feature where it opens a webpage. The specific webpage it opens is determined by the URL, for example: https://mywebsite.com/index.html&audio=disabled In addition to this, there is a useful JavaScri ...