Styling Table Rows with CSS Cross-Banding

I am striving to achieve a particular visual effect:

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

The HTML code I am working with involves a basic table structure:

<table id="a">
  <thead>
    <tr>
      <td>normal</td>
      <td>grey</td>
      <td>normal</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>normal</td>
      <td>grey</td>
      <td>normal</td>
    </tr>
    <tr>
      <td>normal</td>
      <td>grey</td>
      <td>normal</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>normal</td>
      <td>grey</td>
      <td>normal</td>
    </tr>
  </tfoot>
</table>

To achieve rounded borders, I need to change the td elements to display as inline-block.

My challenge arises with the grey bands between rows. There are white spaces which I do not want. Unfortunately, I cannot use a separate background element because the number of rows may vary. edit: I require spacing between borders but wish for the white gaps in the grey column to be grey as well...

You can view my attempts here: https://jsfiddle.net/h5Lh1eaw/24/

Is there a solution using CSS that I am unaware of?

Answer №1

Absolutely! In order to achieve the desired effect, you must apply border-collapse: collapse to the <table>. Here is how your CSS should look:

CSS

table#a, table#b {
  border-collapse: collapse;
}

Preview:

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

Snippet

* {
  box-sizing: border-box;
}

table#a {
  border-collapse: collapse;
}

table#b {
  border-collapse: collapse;
}

td:nth-child(2) {
  background-color: #c0c0c0;
}

tbody tr {
  margin: 5px;
}

td {
  display: inline-block;
  padding: 3px;
  width: 200px;
}

tbody td {
  border-top: 1px solid black;
  border-bottom: 1px solid black;
}

tbody td:first-of-type {
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  border-left: 1px solid black;
}

tbody td:last-of-type {
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
  border-right: 1px solid black;
}
<p>
  A:
</p>
<table id="a">
  <thead>
    <tr>
      <td>normal</td>
      <td>grey</td>
      <td>normal</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>normal</td>
      <td>grey</td>
      <td>normal</td>
    </tr>
    <tr>
      <td>normal</td>
      <td>grey</td>
      <td>normal</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>normal</td>
      <td>grey</td>
      <td>normal</td>
    </tr>
  </tfoot>
</table>
<p>
  B:
</p>
<table id="b">
  <thead>
    <tr>
      <td>normal</td>
      <td>grey</td>
      <td>normal</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>normal</td>
      <td>grey</td>
      <td>normal</td>
    </tr>
    <tr>
      <td>normal</td>
      <td>grey</td>
      <td>normal</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>normal</td>
      <td>grey</td>
      <td>normal</td>
    </tr>
  </tfoot>
</table>

JSFiddle: https://jsfiddle.net/gfqrL8n0/

Answer №2

To achieve the desired appearance of the gaps, one option is to enclose the cell content within a span element.

* {
  box-sizing: border-box;
}
td:nth-child(2),
td:nth-child(3) {
  background-color: #c0c0c0;
}

table {
  border-spacing:6px 0;  /* gap between columns */
}

td {
  padding: 3px 0;       /* gap between rows */
  width: 200px;
}
thead td,
tfoot td {
  padding:3px;         /* match padding of span */
}

tbody td > span {
  padding:3px;        
  position:relative;
  display:block;
  border-top: 1px solid black;
  border-bottom: 1px solid black;
}

tbody td > span:after {           
  content:'';
  display:block; 
  position:absolute; 
  left:100%;
  top:-1px;
  bottom:-1px;
  width:6px;                     
  border-top: 1px solid black;
  border-bottom: 1px solid black;
}

tbody td:first-child > span {
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  border-left: 1px solid black;
}

tbody td:last-child > span {
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
  border-right: 1px solid black;
}

tbody td:last-child > span:after {
  display:none;
}
<table id="a">
  <thead>
    <tr>
      <td>normal</td>
      <td>grey</td>
      <td>grey</td>
      <td>normal</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><span>normal</span></td>
      <td><span>grey</span></td>
      <td><span>grey</span></td>
      <td><span>normal</span></td>
    </tr>
    <tr>
      <td><span>normal</span></td>
      <td><span>grey</span></td>
      <td><span>grey</span></td>
      <td><span>normal</span></td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>normal</td>
      <td>grey</td>
      <td>grey</td>
      <td>normal</td>
    </tr>
  </tfoot>
</table>

Another approach is to use pseudo elements for borders instead of spans.

* {
  box-sizing: border-box;
}
td:nth-child(2),
td:nth-child(3) {
  background-color: #c0c0c0;
}

table {
  border-spacing:6px 0;  
}

td {
  width: 200px;
}
thead td,
tfoot td {
  padding:3px;         
}

tbody td {
  padding: 6px 3px;       
  position: relative;
}

tbody td:after {          
  content:'';     
  display:block;
  position:absolute; 
  left:0;
  right: -6px;              
  top:3px;                 
  bottom:3px;             
  border-top: 1px solid black;
  border-bottom: 1px solid black;
}

tbody td:first-child:after {
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  border-left: 1px solid black;
}

tbody td:last-child:after {
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
  border-right: 1px solid black;
  right: -3px;                    
}
<table id="a">
  <thead>
    <tr>
      <td>normal</td>
      <td>grey</td>
      <td>grey</td>
      <td>normal</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>normal</td>
      <td>grey</td>
      <td>grey</td>
      <td>normal</td>
    </tr>
    <tr>
      <td>normal</td>
      <td>grey</td>
      <td>grey</td>
      <td>normal</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>normal</td>
      <td>grey</td>
      <td>grey</td>
      <td>normal</td>
    </tr>
  </tfoot>
</table>

Answer №3

Review this CSS code snippet:

* {
        box-sizing: border-box;
    }

    table#a {
        border-spacing: 5px, 0;
    }

    table#b {
        border-collapse: collapse;
    }

    td {
        background-color: #c0c0c0;
        border-right: 5px solid #FFFFFF;
    }

    td:nth-child(1) {
        background-color: #ffffff;
    }

    tbody tr {
        margin: 5px;
    }

    td {
        display: inline-block;
        padding: 3px;
        width: 200px;
    }

    tbody td {
        border-top: 1px solid black;
        border-bottom: 1px solid black;
    }

    tbody td:first-of-type {
        border-top-left-radius: 4px;
        border-bottom-left-radius: 4px;
        border-left: 1px solid black;
    }

    tbody td:last-of-type {
        border-top-right-radius: 4px;
        border-bottom-right-radius: 4px;
        border-right: 1px solid black;
    }

Take a look at the image snippet provided below

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

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

Ways to increase the count in Vue Class Bindings?

Trying to utilize Vue class bindings, but uncertain about the syntax for bindings. I aim to append a numeric value to the class attribute, for instance class = "allstar50", then employ class bindings with the v-for directive to display a list of text items ...

What could be causing the removeChild() method to fail when trying to delete a list item in JavaScript DOM manipulation?

Check out this interactive shopping list. You can add items, delete them, and mark them as completed. Adding and deleting items works fine, but there's an issue when toggling them. Issue (occurs when toggling multiple list items) If you add 3 items ...

Update array when checkbox is selected/deselected

Using angular-google-maps, I am creating a map with various types of markers. Below the map, there are checkboxes in a simple form: <div class="mapOptions"> <form action="" class="form-inline" style="text-align:center"> <label for=""& ...

Attempting to dynamically add an image to a template snippet

Struggling with inserting images dynamically from database paths in templates. How can I display them when a user clicks the Show More button on an expense page? New to templates and unsure about syntax. Show More Button displayed in the HTML template sho ...

displaying html with embedded php script

Just purchased a PHP script but all the pages are in .html format. After uploading it to my Ubuntu server and accessing the site, the PHP codes are being displayed alongside the HTML code. Any idea why this is happening? How can I resolve this issue? ...

the dropdown menu toggle is not working as expected

My dropdown icon is not appearing and the menu options are not functioning properly. Despite trying to use a script to display the text when an option is clicked, it doesn't seem to be working. It appears that the toggle functionality is not working ...

Identifying elements using xpath - Streamlined Xpath Techniques

I successfully found the element using the xpath copied directly from the code. Can someone assist me in creating a simpler xpath for the following code snippet, which is fully functional! WebElement oCheckbox = myDriver.findElement(By.xpath(".//*[@id=&ap ...

Is it possible to stop content from spilling out of an HTML element by automatically resizing its children?

Is there a way to adjust the size of the h1 tags in the following example so that they do not overflow? I am looking for a solution other than simply hiding the overflow. We want the 'strictContainer' to have a maximum height of 100px. I have se ...

The function in JavaScript that is supposed to receive a value from a servlet is malfunctioning

I am encountering an issue with my JavaScript method that is supposed to display an alert on the document body's onload event. The method takes a single string parameter provided by the servlet. Although the value is retrieved successfully, it seems ...

Swapping a Cube Mesh for a Car Mesh in ThreeJS

After developing a 3D cube that moves on one axis simulating the accelerometer sensor, I encountered errors when attempting to replace it with a car mesh. The errors are persistent and related to object definitions: https://i.sstatic.net/iTbGI.png Even t ...

What is the best way to transfer information from Swift to JavaScript and showcase it in a web view?

I am currently developing an iOS hybrid app using Swift 2 and HTML/JavaScript. My app has a native shell that retrieves information from the calendar and GPS. I want to display this information in my WKWebView and update it every second. The challenge lies ...

Encountering an error message stating that the "@font-face declaration does not adhere to the fontspring bulletproof syntax" while attempting to integrate a custom font

I've been struggling to incorporate a unique font into my website, but I keep encountering the same error every time. Despite my efforts, I haven't found much guidance on how to rectify this issue. Below is the code I'm using: @font-face ...

What could be the reason why my JavaScript code for adding a class to hide an image is not functioning properly?

My HTML code looks like this: <div class="container-fluid instructions"> <img src="chick2.png"> <img class="img1" src="dice6.png"> <img class="img2" src="dice6.png" ...

Insert a variety of images onto the page at the exact position where the user has

Looking to create a script that selects a random image from an array and displays it at the current mouse position. Additionally, you can click elsewhere to add a new image, allowing you to cover the entire page with random cat images if desired. docum ...

Utilizing Python to dynamically create unique tags from deeply nested dictionaries through recursion

As part of my project, I am developing a unique dynamic template tool that can seamlessly integrate with any templating framework such as pugs and jinja. The goal is to create a demo that resembles the following structure: view! { div { p { ...

Shifting the placement of a button with the help of bootstrap

Having a bit of trouble trying to adjust the position of a button inside an input field using bootstrap. The button appears centered within the form field, but I need it to be slightly shifted to the right for design consistency. https://i.stack.imgur.com ...

After being reloaded multiple times, the time and date mysteriously vanish

Issue with Javascript/jQuery/jQuery Mobile Code After reloading or refreshing the page multiple times, the values for currentDate and currentTime seem to disappear sporadically. Strangely, they start working fine again after a few more reloads. Can anyone ...

Understanding the dissimilarities between the input:disabled and input[disabled] CSS selectors

Is there a distinction between using the :disabled CSS pseudo-class selector and the [disabled] CSS attribute selector when applying styles to a disabled HTML input element, or are they essentially identical? Perhaps in terms of browser support or other ...

Refreshing Your WordPress Post Automatically

Can someone assist me with setting up an autorefresh feature for a specific div that has the ID of "blue"? I want the div to automatically update whenever I make changes to a post with that same ID. var auto_refresh = setInterval( function () { $('#b ...

Leveraging a jQuery variable as the identifier for an element

Looking to dynamically use a jQuery variable: var Classofentry = $('#upload_form option[class]').attr("class"); It currently holds the value "animal_species" and I want to utilize it in an id reference like this: $(Classofentry).append("<op ...