Fixing border prioritization in a table using the CSS style "border-collapse: collapse"

I have encountered a problem where certain borders take precedence over others when using the border-collapse: collapse styling in a table. Specifically, when setting the border-right on a cell, it appears above the border-left. Is there a way to adjust the z-index of a border or achieve a similar effect?

Below is a code snippet to better demonstrate the issue:

table{
  border-collapse: collapse;
  width: 200px;
  height: 100px;
  text-align: center;
}

td{
  border: 3px solid black;
}

#specialCell{
  border-right: 3px solid blue;
}

#otherSpecialCell{
  border-left: 4px solid green;
}

#redBorder{
  border-left: 4px solid red;
}
<table>
<tbody>
  <tr>
    <td></td>
    <td></td>
    <td id="otherSpecialCell"> 1 </td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td id="specialCell"> 2 </td>
    <td></td>
    <td></td>
  </tr>
</tbody>
</table>

<p>
The border on the "otherSpecialCell" needs to be 4px in order to display, while the "specialCell" only requires 3px for its border to show. This demonstrates that border-right has priority over border-left.
</p>

<table>
<tbody>
  <tr>
    <td></td>
    <td></td>
    <td id="redBorder"> 3 </td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
</tbody>
</table>

<p> The red border appears above both the top and middle borders of the table.</p>

<p> Can this behavior be controlled without adjusting border widths?</p>

Furthermore, although removing the border-collapse: collapse may provide a similar outcome as individually setting left and right borders, the sharp edges can result in an unattractive appearance.

Answer №1

To avoid seeing black lines, consider using border-spacing: 0px; instead of border-collapse: collapse.

table{
  border-spacing: 0px;
  width: 200px;
  height: 100px;
  text-align: center;
}

td{
  border: 3px solid black;
}

#specialCell{
  border-right: 3px solid blue;
}

#otherSpecialCell{
  border-left: 3px solid green;
}

#redBorder{
  border-left: 3px solid red;
}
<table>
<tbody>
  <tr>
    <td></td>
    <td></td>
    <td id="otherSpecialCell"> 1 </td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td id="specialCell"> 2 </td>
    <td></td>
    <td></td>
  </tr>
</tbody>
</table>

<p>
The "otherSpecialCell" requires a 4px border size to be visible while the "specialCell" only needs 3px for its border to show up (demonstrating that border-right takes precedence over border-left).
</p>

<table>
<tbody>
  <tr>
    <td></td>
    <td></td>
    <td id="redBorder"> 3 </td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
</tbody>
</table>

<p> The red border is displayed above both the top and middle borders of the table.</p>

<p>Is it possible to control this behavior without adjusting the border widths?</p>

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 there a way to adjust the label size for a material ui TextField component?

My TextField element is defined like this: <TextField id="standard-with-placeholder" label="First Name" className={classes.textField} margin="normal" /> It currently appears as shown in the image below: https://i.sstatic.net/aYi1a.png How ...

Establishing visual properties for inactive Mat-Expansion Panels

Is there a way to customize the appearance of a disabled Mat-Expansion-Panel? I have buttons in the header that toggle the panel, so I'm considering disabling the panel itself instead. However, when I disable the panel, all the items inside are greyed ...

The values are failing to update within the update panel

I have exhausted all the suggested solutions provided here in relation to my issue with no success. I am working on creating some graphs using the Dygraphs library. The JavaScript code snippet below represents just a fraction of the complete function, as I ...

Adding a CSS shadow to an element positioned beneath another

I'm attempting to recreate a shadow effect in HTML similar to one I created in Photoshop: https://i.sstatic.net/fBcBl.png I believe a basic HTML structure like the one below should suffice, but I'm unsure: <div id="anotherMask"> <di ...

Need inspiration for testing web content with Protractor?

Exploring new possibilities for testing web content with Protractor. Any fresh ideas on what else can be tested? So far, I've checked links to ensure they redirect correctly. I've also verified text color and decoration changes when hovering ove ...

Create circular shapes using the border-radius property

I've been experimenting with converting some divs using border radius and I've almost got it, but sometimes they end up looking like 'eggs' haha. Here is the CSS code I'm using: #div{ /*central div*/ position:absolute; t ...

Sas: The outcome matches the viewer results exactly

I am reaching out because I am experiencing an issue with my results viewer in Sas 9.4. When the results load, they appear similar to the output format. Instead of displaying a table with cells, it appears as a table drawn with -+|. The layout options do ...

When a specific option is selected in a `select` element with the `multiple` attribute, activate the change event

Is it possible to trigger a change event on individual options within a standard select element using jQuery or another method? I want the change event to be triggered on the option elements themselves rather than on the select element, as multiple options ...

Tips on creating a hierarchical ul list from a one-dimensional array of objects

I have an array filled with various objects: const data = [ {id: "0"},{id: "1"},{id: "2"},{id: "00"},{id: "01"},{id: "02"},{id: "11"},{id: "20"},{id: "23"},{id: & ...

unitary incline division assemblage

I noticed a particular element in the design that is currently displayed with an image (png). However, I am facing limitations due to needing to make it adaptive. Can a gradient be used as a sub-element instead? background-image: linear-gradient(to left, ...

Is there a way to modify the image source upon clicking a button?

I am currently working on implementing an image swap functionality on a webpage. I need the image to change when a user clicks a button, but for some reason, my logic isn't working. I am more interested in understanding why it's not working and w ...

Styling Fonts in Safari with CSS

Because FixedSys doesn't appear correctly in Chrome or Safari, I switch it to Lucida Console. While this solution works for Chrome, I encounter a problem with Safari. Unless Lucida Console stands alone, the font will not display as desired. If there a ...

Avoiding the "urls" format using the onBeforeRequest function

chrome.webRequest.onBeforeRequest.addListener(function(details) { if (localStorage.on == '1') { return {cancel:true}; } }, {urls: ["*://*.domain1.net/*","*://*.domain2.com/*","*://*.domain3.com/*"], types: ["script","xmlhttpreques ...

What is the best way to align this alert in the center of the screen

Hey there, I'm facing an issue with aligning an alert horizontally when a user clicks a button. I've been trying different methods but can't seem to get it right. The goal is to keep the alert within #main. You can check out this fiddle for ...

Is it possible to scroll the grid horizontally?

I have a grid that scrolls vertically, with columns adjusting their width automatically. .grid { display: grid; overflow-y: auto; grid-template-columns: repeat(auto-fit, minmax($min-column-width, 1fr)); } Now, I'm attempting to create a horizon ...

Issues with Javascript code syntax

I have encountered an issue while creating a new literal control from code behind. I am able to add standard HTML into it successfully, however, when I try to insert <%= SomeDiv.ClientID %> into the literal control, it simply prints exactly what is i ...

The duration spent on a website using AJAX technology

We conducted an online survey and need to accurately calculate the time spent by participants. After using JavaScript and PHP, we found that the calculated time is not completely accurate. The original script was sending server requests every 5 seconds to ...

Is it true that JavaScript Date.parse doesn't recognize Alaska Timezones?

When using JavaScript's Date.parse, it handles Pacific Time without any issues: Date.parse('June 20 2015 10:22 PDT') However, it encounters issues with Alaska Time: Date.parse('June 20 2015 10:22 AKDT') Does anyone have a relia ...

What is the best way to assign a value to a property in a Controller or Global Variable using jQuery?

On my ASP MVC 5 site, I have a checkbox within the NavBar element. This checkbox's state dictates whether or not the Grid will display Inactive records alongside active ones on each page. In an attempt to have all controllers access the same property ...

Changing the Style Sheets on the Fly

I have designed a grid of 5x5 boxes. My goal is to be able to click on a link and have that specific link change color after it has been clicked (a:visited) - one at a time. Unfortunately, my current code changes the color of all the links instead of just ...