CSS is effective when styling table elements such as 'tr' and 'td', but encounters issues when trying to include 'th'

I have a variety of tables on my website, most of them without borders. However, I want to add borders to some of them. In my CSS, I am using a specific class for those tables:

table {
  padding: 2px;
  border-collapse: collapse;
  table-layout: auto;
  text-align: center;
  display: block;
}

table.tableAllBorder tr td{
  border-right: solid 1px; 
  border-left: solid 1px;
  border-top: solid 1px; 
  border-bottom: solid 1px;
}

This approach works smoothly. But when I try to include 'th' within the definition, the CSS does not work correctly:

table.tableAllBorder tr td th{
  border-right: solid 1px; 
  border-left: solid 1px;
  border-top: solid 1px; 
  border-bottom: solid 1px;
}

What I ideally want is to simply use the attribute border="1" when defining my table. However, when I do this, I end up with a double border that extends beyond my table (see attached screenshot). Can you help?

<table border="1">
</table>

https://i.sstatic.net/WL1lU.png

Edit: Here is my CSS:

html, body {
  background-color: rgb(var(--color5));
  font-family: sans-serif;
  -webkit-transition: height 0.3s;
  -moz-transition: height 0.3s;
  transition: height 0.3s;
}

.div1
{
  margin-left: 40px;
  overflow-x:auto;
  width:auto;
}

table {
  padding: 2px;
  border-collapse: collapse;
  table-layout: auto;
  text-align: center;
  display: block;
}

table tr:nth-child(odd) {
  background-color: rgb(var(--color4));
}

table tr:nth-child(even) {
  background-color: rgb(var(--color5));
}

table.tableAllBorder tr td th{
  border-right: solid 1px rgb(var(--color1)); 
  border-left: solid 1px rgb(var(--color1));
  border-top: solid 1px rgb(var(--color1)); 
  border-bottom: solid 1px rgb(var(--color1));
}

and here is my HTML code:

<div class="div1">
<table border="1"  class="center">
<tr>
 <th> ColA </th>
 <th> ColB </th>
 <th> ColC </th>
 <th> ColD  </th>
 <th> ColE </th>
 <th> ColF </th>
 <th> ColG </th>
 <th> ColH </th>
 </tr>
</table>
</div>

Answer №1

Avoid using border=1. This type of markup is outdated and does not adhere to semantic HTML principles. Instead, utilize a class with border: 1px solid black.

If you are styling the th element in your CSS, remember that you are targeting only the th element itself. You should create a separate class for this purpose.

table.tableAllBorder tr td {
   /* include your original styles here */
}

table.tableAllBorder tr th { /* add a new selector here */ }

Without seeing your specific markup, we assume you have the following correct HTML structure:

<table class="tableAllBorder">
  <tr>
    <th>Header</th>
    ...
  </tr>
  <tr>
    <td></td>
    ...
  </tr>
  <tr>
    <td></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

Can you help me figure out how to make a header that sticks to the top of the page as you scroll horizontally, and a sidebar that stays in place as you scroll vertically with

I am currently working on developing a layout that includes a sticky header and sidebar for my content list. My goal is to have the sidebar scroll down as I move through the page content, while also ensuring that the header scrolls horizontally along with ...

Getting the value of a CSS variable under <script> in Vue.js

I am working on implementing a Doughnut chart using chartJs in my application. However, I want to set the color of the chart within the <script> tag and retrieve the color from theme/variables.css. In the current code snippet below, there is a hardc ...

Tips for inserting a Vue.js variable into a window.open event

Within this snippet of code: <tr v-for="person, index in People" :key='index'> <td> <button type="button" onclick="window.open('/details/'+person.id,'_blank')"> details</butto ...

I am having issues with my search form - it doesn't appear to be

I am trying to create a search form that will display users (username, firstname, or lastname) from my database as the user types, similar to how Facebook and Twitter do it. However, when I click on the search button, nothing happens. Below are two parts o ...

Issue with Angular 6: Jquery unable to detect element when using *ngIf directive

I'm currently developing an app using Angular 6, and I am facing an issue while trying to add a class using the jquery hover() method. In my navigation menu, I have 5 links, and although they all appear the same, 2 of them have an added *ngIf directiv ...

Customizing checkboxes in React with JSS: A step-by-step guide

I'm currently working on customizing CSS using JSS as my styling solution, which is proving to be a bit complex while following the w3schools tutorial. https://www.w3schools.com/howto/howto_css_custom_checkbox.asp HTML: <label class="container"& ...

There is a significant distance between the columns, causing the text to appear misaligned and not left-

I'm encountering an issue with the alignment of a row with two columns. The first column contains an image, and the second column contains text. However, the text is not aligning properly next to the image. <link href="https://cdn.jsdelivr.net/ ...

Learn how to retrieve the value of an associated field at a specific index by utilizing a combo box in JavaScript when receiving a JSON response

Hey there, I'm currently working on a phone-gap app where I need to fetch data from a WCF service that returns JSON responses. Specifically, I want to display the DesignName in a combo box and pass the associated designId. Any thoughts on how I can ac ...

Is it possible to create two header columns for the same column within a Material UI table design?

In my Material UI table, I am looking to create a unique header setup. The last column's header will actually share the same space as the previous one. Picture it like this: there are 4 headers displayed at the top, but only 3 distinct columns undern ...

How can I enhance a JavaScript Calendar with more features?

I recently acquired a JavaScript and jQuery calendar from CodeCanyon, which can be found here. I am now faced with the task of integrating this calendar into my workplace website. The current calendar on our ASPX-based site is limited to read-only functio ...

Updating the PHP form to include functionality for a reset button and adjusting the logic for

Having some difficulties with my PHP form. I created a simple form that consists of the following logic: if (isset($_POST['submit'])) { } elseif (isset($_POST['confirm'])) { } else { } The form includes a submit button, followed by a ...

Explore the hidden route of the input components

HTML: <div id="quiz"> <div id="question"> <p id="quiz-txt">What is your favorite color?</p> <ul id="quiz-opt"> <div id="ans"> <input type="checkbox" id="Red" value="Red" class="options"> ...

What is the best way to update the content of a text area using PyScript?

Currently, I am in the process of developing a program that involves entering text, clicking a button, processing it, and then displaying the output on a textarea. However, I'm faced with an issue where the textarea fails to update as expected. I woul ...

Background image not adhering to the page width as expected within the Bootstrap framework

I feel like I'm losing my mind. Currently, I am in the process of designing a section that consists of four photo boxes - two at the top and two below. In order to achieve this layout, I have turned to utilizing Bootstrap. Unfortunately, this parti ...

What steps can be taken to restrict a user's access to the main page unless they are logged in?

I have created sign up and login pages using JavaScript, HTML, and PHP with a database. After a user successfully logs in on the login page, the following code is executed: sessionStorage.setItem('logged','loggedIn'); The user is then ...

Choose a trio of columns using jQuery

Is it possible to target three specific columns using jQuery or CSS, like this: Take a look at the example code below: <script> //I am attempting to target specific columns with this code snippet $('GvGrid').slice(1, 3).css("backgroundC ...

Struggling to Position Advertisement in CSS

I've been struggling to properly center a div using CSS. Adding text-align: center; doesn't seem to do the trick. The issue can be found in the top advert on this website: . Do you have any suggestions? Here's some sample code: HTML: &l ...

Convert an HTML table into an Excel file using PHP

After successfully generating DATA from PHP into an HTML table format, I utilized the code below to export it to EXCEL: header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="BASIC_Data.xls"&apos ...

Unexpected character appearing in Android 2.3, whether using the li or ul tag

Having an issue with my social icons on the top of my website displaying unwanted symbols in Android 2.3. Can't figure out the cause even after ruling out <a> tags and associated CSS. Any thoughts on what the problem might be? I've remove ...

The addClass, removeClass, and css functions in jQuery are malfunctioning

I stumbled upon this website and I am looking to switch the font color from black to white. I attempted various methods but unfortunately, none of them seemed to work: Implementing jQuery's addClass function (to add a class to the SPAN tag) Uti ...