The demarcation line dividing two adjoining table cells using HTML/CSS

Currently, I am in the process of enhancing an existing servlet to improve its appearance. One of the challenges I faced was trying to remove the border between two cells in a complex table to make it look more visually appealing. Despite my attempts to define a CSS rule for this purpose, I have been unsuccessful. I then experimented with changing the cell border color, but encountered another setback. It seems like there may be some priority issues with the CSS rules that I'm finding difficult to resolve.

Here is a visual representation of what I am aiming to achieve:

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

To showcase the issue clearly, below is a minimal reproducible example:

<html>
<head><style>
    table { border-collapse: collapse;}
    table, th, td { border: 1px solid black;}
    th, td {
        padding: 5px;
        text-align: left; vertical-align: top;
    }
    tr.all { background-color: palegreen; }
    tr.other    { background-color: beige; }
    td.chain { border: 1px solid red; }
    td.target { border-left: none; }
</style></head>
<body>
    <table class='rule'>
    <tr class="all"><td>XX</td></tr>
    <tr class="other">
        <td>YY</td>
        <td class='target'>ZZ</td></tr>
    <tr class="other">
        <td>AA</td>
        <td class='chain'>BB</td>
    </tr>
    </table>
</body>
</html>

Answer №1

When you set a border on a td, it applies the border all around the td. Using border-collapse: collapse; will overlap two borders instead of removing them, so you'll need to remove the border from both columns.

.other td:first-child{
  border-right:0;
}
.other td:last-child{
  border-left:0;
}

Layout 1


table {
  border-collapse: collapse;
}

table,
th,
td {
  border: 1px solid black;
}

th,
td {
  padding: 5px;
  text-align: left;
  vertical-align: top;
}

tr.all {
  background-color: palegreen;
}

tr.other {
  background-color: beige;
}
.other td:first-child{
  border-right:0;
}
.other td:last-child{
  border:1px solid red;
}
<table class='rule'>
  <tr class="all">
    <td>XX</td>
  </tr>
  <tr class="other">
    <td>YY</td>
    <td class='target'>ZZ</td>
  </tr>
  <tr class="other">
    <td>AA</td>
    <td class='chain'>BB</td>
  </tr>
</table>

Layout 2


table {
  border-collapse: collapse;
}

table,
th,
td {
  border: 1px solid black;
}

th,
td {
  padding: 5px;
  text-align: left;
  vertical-align: top;
}

tr.all {
  background-color: palegreen;
}

tr.other {
  background-color: beige;
}
.other td:first-child{
  border-right:0;
}
.other td:last-child{
  border-left:0;
}
<table class='rule'>
  <tr class="all">
    <td>XX</td>
  </tr>
  <tr class="other">
    <td>YY</td>
    <td class='target'>ZZ</td>
  </tr>
  <tr class="other">
    <td>AA</td>
    <td class='chain'>BB</td>
  </tr>
</table>

The CSS property border-collapse determines whether a table's borders are separated or collapsed. In the separated model, adjacent cells each have their own distinct borders. In the collapsed model, adjacent table cells share borders.

MDN

Answer №2

Give it a shot!

table { border-collapse: collapse;}
table, th, td { border-left: 1px solid black; border-bottom: 1px solid black;}
th, td {
    padding: 5px;
    text-align: left; vertical-align: top;
}
tr.all { background-color: palegreen; }
tr.all td { border: 1px solid black; }
tr.other    { background-color: beige; }
td.chain { border: 1px solid red; }
td.target { border: 1px solid red; }

Answer №3

Style Variation #1: Check it out here!

<style>
  table { border-collapse: collapse;}
  table, th, td { border: 1px solid black;}
  th, td {
    padding: 5px;
    text-align: left; vertical-align: top;
  }
  tr.all { background-color: palegreen; }
  tr.other    { background-color: beige; }
  td.chain { border-left: 0px; }
  td.target { border-left: 0px; }
</style>

<table class="rule">
  <tbody><tr class="all"><td>XX</td></tr>
   <tr class="other">
      <td style="border-right: 0px;">YY</td>
      <td class="target">ZZ</td></tr>
   <tr class="other">
      <td style="border-right: 0px;">AA</td>
      <td class="chain">BB</td>
   </tr>
  </tbody>
</table>

Style Variation #2: Explore this version here!

<style>
  table { border-collapse: collapse;}
  table, th, td { border: 1px solid black;}
  th, td {
    padding: 5px;
    text-align: left; vertical-align: top;
  }
  tr.all { background-color: palegreen; }
  tr.other    { background-color: beige; }
  td.chain { border-color: red; }
  td.target { border-color: red;  }
</style>

<table class="rule">
  <tbody><tr class="all"><td>XX</td></tr>
   <tr class="other">
      <td style="border-right: 0px;">YY</td>
      <td class="target">ZZ</td></tr>
   <tr class="other">
      <td style="border-right: 0px;">AA</td>
      <td class="chain">BB</td>
   </tr>
</tbody>
</table>

Answer №4

Desired output 1

table {
  border-collapse: collapse;
}

table,
th,
td {
  border: 1px solid black;
}

th,
td {
  padding: 5px;
  text-align: left;
  vertical-align: top;
}

tr.all {
  background-color: palegreen;
}

tr.other {
  background-color: beige;
}

td.chain {
  border: 1px solid red;
}

td.target {
}
.other>td:first-child{
  border-right: 1px solid red;
}
.other>td:last-child{
  border: 1px solid red;
}
<table class='rule'>
  <tr class="all">
    <td>XX</td>
  </tr>
  <tr class="other">
    <td>YY</td>
    <td class='target'>ZZ</td>
  </tr>
  <tr class="other">
    <td>AA</td>
    <td class='chain'>BB</td>
  </tr>
</table>

Desired output 2

<table class='rule'>
  <tr class="all">
    <td>XX</td>
  </tr>
  <tr class="other">
    <td>YY</td>
    <td class='target'>ZZ</td>
  </tr>
  <tr class="other">
    <td>AA</td>
    <td class='chain'>BB</td>
  </tr>
</table>
table {
  border-collapse: collapse;
}

table,
th,
td {
  border: 1px solid black;
}

th,
td {
  padding: 5px;
  text-align: left;
  vertical-align: top;
}

tr.all {
  background-color: palegreen;
}

tr.other {
  background-color: beige;
}

.other>td:not(.target) {
  border: none;
}
.target{
  border-left:none;
  border-bottom:none;
  }

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 showcase images in a random order within a PHP for loop

I'm working on a basic webpage where I'm trying to display multiple images using the following code: <?php for($l=1;$l<=45;$l++){?> <div class="thumb" style="background-image: url(l<?=$l?>.jpg);">< ...

Challenges with positioning arise due to divs overlapping each other, preventing the accurate placement of the footer

My footer div is not sticking to the bottom of the page as I intended. Instead of remaining fixed at the bottom, it seems to be floating on top of the main content. Additionally, my divs are not lining up properly and appear to have padding on the top and ...

Solving the "ExpressionChangedAfterItHasBeenCheckedError" in Ionic: A Guide

//html <span style="margin-left:43%;background-color:rgb(229,229,229);border- radius:10%">&nbsp;&nbsp;{{formatEpoch(epoch)}}&nbsp;&nbsp;</span> //ts lastdate:any; formatEpoch(epoch): string { ...

Learn how to dynamically alter the background of an ExtJS button when it is hovered over or pressed

Greetings, I am looking to dynamically change the background-color for different states (normal, hover, pressed) of a button. Here is what I have come up with so far: http://jsfiddle.net/suamikim/c3eHh/ Ext.onReady(function() { function updateBackgr ...

Guidelines for retrieving a class name using jQuery when hovering over a div

I need assistance in fetching the class name by hovering over a div. Both divs have the same id but slightly different class names. Here is an example: <div id="1-someid" class="1-example-class border cz"> ...more elements go here.... </div> ...

Concern with Isotope's masonry feature

I'm at my wit's end! The container div I'm dealing with is 1170px wide. My goal is to fit in 3 divs within this width. The first div is 275px wide, the second is 580px wide, and the third is also 275px wide. Altogether, these divs should ad ...

Exploring methods to modify the style attribute of a div tag in .NET

Using .NET, we need to access the CSS style of a div and input certain values in the code behind on page load. Since the div is located on the master page and this is another separate page, we are unable to add RUNAT="SERVER" to the div. ...

Filtering posts in Angular with the use of pipes

I've been attempting to use pipes in Angular to filter posts from a table, but I keep running into this error on the console: core.js:12689 Can't bind to 'ngForFilter' since it isn't a known property of 'a'. I believe th ...

Is it achievable to apply a shadow to a div using only CSS, or would images be required?

I am looking for a simple solution where the user can press something and see a shadow effect on a new div (div centered window) that appears on top of the entire page, maybe 1/4th in size. Can this be achieved using pure web-kit css? Or would a combinat ...

Any idea why the HTML Select dropdown isn't functioning properly in Angular 2?

I am facing an issue with an Angular 2 Form. I am trying to include an html select but it is not working. I have checked the Angular 2 Documentation and even the live examples provided, like the HERO FORM, are not working. You can view the Hero Form Live E ...

Styling Hyperlinks with CSS

Recently, I've been playing around with links and using CSS to style them. I have successfully achieved the normal look and implemented a cool hover effect using a:hover. However, I'm facing some challenges when it comes to customizing the appear ...

A straight line segment separating two divisions horizontally

Looking to enhance the appearance of my colorful box created using bootstrap, I attempted to add a horizontal line spanning from left to right as shown in the second image. However, my approach of adding a new div element with a border did not result in fi ...

Change the background color of the accordion header

Is there a way to dynamically change the color of an accordion based on the status of the current item in the list? I'm trying to implement something like ng-class="{status: item.status}" (where I have set testClass: true) The issue I'm facing ...

Utilizing jQuery and html5 for manipulating the DOM with append()/appendTo() functions while taking

Instructions on Replicating: Begin by creating an html5 page. Ensure that you have included the script from remysharp.com/2009/01/07/html5-enabling-script/ so that Internet Explorer can recognize the tags. Add a hardcoded <section id='anyt ...

Using Django, Ajax, and JavaScript to toggle the like and unlike icons when a button is clicked

1 I recently came across a solution on how to implement a Django Like and Unlike button without page reload. The method I found only works with text toggles (Like and Unlike) but I'm interested in using icon toggles (Like and Unlike icons). I am stil ...

What is the best method for applying comment style to text segments that may contain overlapping elements?

If I have a contenteditable div with text inside, my goal is to add comments (outside of the container div) to specific sections of the text. When the user clicks on a comment, the corresponding text segments should be styled, such as changing their color ...

Generating a customizable HTML table using a two-dimensional array

My goal is to develop a flashcard website that allows users to add, edit, and delete flashcards. Each flashcard consists of two sides - front and back. Currently, users can add words but are unable to edit or remove them. To illustrate, I have created an e ...

Tips for utilizing CSS to ensure that the images within a hyperlink are consistent with the desired proportions

Seeking assistance with adjusting image proportions in a href link. Despite using background-position, background-size, and object-fit properties, the pictures inside the link are not matching the proportions on different devices. Any help would be greatly ...

css background images are fully covered

Hey there! I recently created a CSS image that covers the entire background of my website. However, when I added a picture in the HTML, it doesn't show up because it's being overlapped by the CSS background. Check out my CSS code: { margin: 0 ...

The wonders of CSS flexbox and the power of justify-content

I am working on a layout where I need three flex items (rows) in a flex container, and my requirement is to justify them as space-between... The first row will consist of cloud tags, the second will have a price, and the third will contain a Read more link ...