How can I make the columns in my table stand out with different background colors?

I'm currently working on implementing alternating background colors for columns in a table. I have successfully applied CSS to a simple table, but I am facing issues when dealing with more complex tables that involve rowspan and colspan. Can anyone offer guidance on how to approach fixing this issue?

The tables I am working with are all generated by someone else, so I do not have control over the HTML structure. The challenge is to create CSS that will work universally across all these tables.

You can view my CSS code in action at the following links:

or

http://codepen.io/anon/pen/ORPLgz

table.specialtable {
  border-top: 2px solid #00ABEF;
    border-bottom: 2px solid #00ABEF;
    background-colo: #FFF;
    font-size: 11px;
}

table.specialtable th {
    background-color: #B8E5FA;
    border-right: 1px solid #FFF;
    border-left: 1px solid #FFF;
    padding: 4px 2px;
}

table.specialtable td {
    padding: 4px 2px;
    text-align: center;
}

table.specialtable td:first-child {
    text-align: left;
}

table.specialtable td:nth-child(even){ 
 background-color: #E2F4FE;
}

table.specialtable td:nth-child(odd){ 
 background-color: #FFF;
}

Answer №1

Wow, the markup here is quite unconventional but it gets the job done. I've simplified some of the unnecessary specificity while also incorporating SCSS - hope that works for you!

I've made some changes to your code and forked your pen at this link: http://codepen.io/memoblue/pen/yayBpA

Check out the key parts of the code below:

table {
  border-top: 2px solid #00ABEF;
    border-bottom: 2px solid #00ABEF;
    background-colo: #FFF;
    font-size: 11px;
}

table th {
    background-color: #B8E5FA;
    border-right: 1px solid #FFF;
    border-left: 1px solid #FFF;
    padding: 4px 2px;
}

table td {
    padding: 4px 2px;
    text-align: center;
}

table td:first-child {
    text-align: left;
}

table td:nth-child(even){ 
 background-color: #E2F4FE;
}

table td:nth-child(odd){ 
 background-color: #FFF;
}

.specialtable ~ table {
  tr:nth-child(2n) {
    td:first-child {
      background-color: #E2F4FE;
    }
    td:nth-child(2n) {
      background-color: #fff;
    }
    &:nth-child(8) {
      td:first-child {
        background-color: #fff;
      }
    }
  }
}

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

Encountering a 504-loadbalancer error (Gateway Time-out) on PythonAnywhere after waiting for 5 minutes for a response, despite setting SQLALCHEMY_POOL_RECYCLE to 600

Currently, I am utilizing the SPOTIPY API to insert a user's song, album, artist, and user_info into a database. Approximately 4,000 records are being inserted in total. The database updates successfully, but after 5 minutes (response_time = 300 secon ...

Managing images in JavaScript while conserving memory

Welcome I am embarking on a project to construct a webpage using HTML, CSS, JavaScript (jQuery), and nearly 1000 images. The length of the HTML page is substantial, around 5000px. My vision involves creating a captivating visual experience for users as t ...

Insert metadata tag into the head element of the parent iframe

I am looking to insert a meta tag element into the head element of an Iframe parent. I attempted window.parent.$('head').append('sometext'); This method worked when the source file and iframe file were in the same folder, however it d ...

-=:Heading with a decorative vertical line=:-

Currently, I am working on a CSS project that requires page titles (headings) to be centered with horizontal lines positioned vertically on both sides. Additionally, there is a background image on the page which means the title's background must be tr ...

There is a gap found in the span element in Angular, but this gap is not seen in

Comparing two component templates, one in AngularJS and the other in modern Angular: <span ng-repeat="status in statusInformation.statusList | filter : whereFilter" class="show-on-hover"> <span class="app-icon ap ...

The issue with two different link styles displaying correctly in Internet Explorer but not in other browsers is that when a link is clicked, all links appear

Looking for a testing ground? Check out my website: While everything works smoothly on IE, I've noticed a glitch in other browsers. Clicking on one link causes all links on the page to appear as visited links. Take a look at the CSS code below: @ch ...

Generate a Vue component that automatically wraps text dynamically

Challenge I am looking for a way to dynamically wrap selected plain text in a Vue component using the mouseUp event. For instance: <div> Hello World! </div> => <div> <Greeting/> World! </div> Potential Solution Approach ...

Prevent pinch zoom in webkit (or electron)

Is there a way to prevent pinch zoom in an electron app? I've tried different methods, such as using event.preventDefault() on touchmove/mousemove events in JavaScript, adding meta viewport tags in HTML, adjusting -webkit-text-size-adjust in CSS, and ...

"Challenges Arising in Deciphering a Basic JSON Array

After countless attempts, I am still struggling to solve this issue. My PHP code is functioning properly, as it returns the expected data when "Grove Bow" is selected from the dropdown menu: [{"wtype":"Grove Bow","was":"1.55","wcc":"5","wbdmin":"12","wbdm ...

Activating/Deactivating a button with just a press

In my SQL data display, each row has an "Add To Zip" button that is initially enabled. I want to disable the button in the current row when it's clicked and later re-enable them using a different PHP file. What's the best way to achieve this with ...

Customizable styling using SQL and PHP

I've reached a dead end with this issue. Here's the current situation: When User 1 clicks "not available," the background of a div turns red and updates on all other users' screens. CSS: #User1 { background-color: green; <--This is ...

I am unable to tap on the input field

Having a major issue here... I've encountered a problem with this website: sk.maze-it.dk When attempting to click on "BOOK MØDE" in the navigation bar and select an available date (e.g. 30) followed by clicking on "Book møde", two fields appear - o ...

Trouble with importing scss file in sass and bootstrap collaboration

I am using Sass with Bootstrap 4, and now I'm facing an issue in my Sass folder where I have an app.scss file that includes: @import _customVariables.scss @import node_modules/bootstrap/scss/functions @import node_modules/bootstrap/scss/mixins Howev ...

Positioning elements within a Bootstrap column: Fixed versus Absolute positioning

Striving to maintain the absolute positioning of the right column as users scroll through the left column has proven to be a challenging task. Despite exploring various solutions from stackoverflow and google, none seem to effectively address this issue. ...

Exploring the ins and outs of utilizing pseudo selectors with material-ui

I've been attempting to accomplish a simple task. I wanted to toggle the visibility of my <TreeMenu/> component in material UI v1 using pseudo selectors, but for some reason it's not working. Here is the code: CSS: root: { ba ...

Is this approach to implementing content security policy acceptable?

I am in the process of implementing CSP on my web application. I have included the following meta tag in my index page: <meta http-equiv="Content-Security-Policy" content="img-src 'self' data:;default-src *;style-src 'self' http://* ...

Adding HTML content to a statically served file using Flask

Seeking assistance from those more experienced in Flask. I am trying to serve a static file and add some HTML content to it, or display the file beneath some text. Is there a method to achieve this, or am I approaching it incorrectly? @app.route('/com ...

Can you please tell me the name of this specific graph? And could you explain how I can recreate it on a

I am fully aware that this information is just a Google search away. However, I am unsure of the specific name of the chart I am seeking. Therefore, I am unable to conduct an effective search for suitable plugins or APIs. Your understanding is greatly appr ...

output a string from the write_html function in plotly

I am currently attempting to utilize plotly express to generate a string representation of a div object in HTML for integration with other packages. Although I have been following the documentation (Plotly v5.17.0), it seems that my attempts have not been ...