Inconsistencies in table row border behavior

I created a table that allows users to set a marker on a specific row by adding a 5px border to the left side of the row.

Interestingly, when the border is added to the first row, the table gets padded on the right by a few pixels. However, this padding does not occur when the border is added to any other row that's not the first one.

<table>
<tbody>
  <tr class='marked'><td>A</td><td>A</td><td>A</td></tr>
  <tr><td>B</td><td>B</td><td>B</td></tr>
  <tr><td>C</td><td>C</td><td>C</td></tr>
</tbody>
</table>

<table>
<tbody>
  <tr><td>A</td><td>A</td><td>A</td></tr>
  <tr class='marked'><td>B</td><td>B</td><td>B</td></tr>
  <tr><td>C</td><td>C</td><td>C</td></tr>
</tbody>
</table>

https://i.sstatic.net/3N3Aa.png

Has anyone else experienced this behavior? If so, how can it be fixed? I've tested this on Firefox and Chrome with consistent results.

Fiddle: https://jsfiddle.net/2hwuq8ed/

Answer №1

After extensive experience creating html newsletter templates, I have observed that the first row in an html table can impact the positioning of subsequent rows in certain unique situations. This behavior is inherent to tabular formats and should be anticipated.

To mitigate any issues caused by applying borders to the first row or cell, it is advisable to compensate for the deficit by defining a transparent border of equal width for all cells that are not marked, like so:

tr:not(.marked) {
    border-left: 5px solid transparent;
}

View updated JSFiddle

table {
  border-collapse: collapse;
}

tr {
  background-color: blue;
}
tr:nth-child(odd) {
  background-color: grey;
}
tr.marked {
  border-left: 5px solid green;
}
tr:not(.marked) {
    border-left: 5px solid transparent;
}

td:first-child {
  width: 100px;
}
td {
  width: 50px;
}
<table>
<tbody>
  <tr class='marked'><td>A</td><td>A</td><td>A</td></tr>
  <tr><td>B</td><td>B</td><td>B</td></tr>
  <tr><td>C</td><td>C</td><td>C</td></tr>
</tbody>
</table>
<br>
<table>
<tbody>
  <tr><td>A</td><td>A</td><td>A</td></tr>
  <tr class='marked'><td>B</td><td>B</td><td>B</td></tr>
  <tr><td>C</td><td>C</td><td>C</td></tr>
</tbody>
</table>

If you want to eliminate preceding whitespace in tables entirely, you may consider using a pseudo-element on the first nested table cell as a visual marker instead, as demonstrated below:

tr.marked td:first-child:before {
    content: "";
    position: absolute;
    left: -3px;
    top: 0;
    bottom: 0;
    background: green;
    width: 5px;
}

View updated JSFiddle

table {
  border-collapse: collapse;
}

tr {
  background-color: blue;
}
tr:nth-child(odd) {
  background-color: grey;
}

tr.marked td:first-child:before { /* additional */
    content: "";
    position: absolute;
    left: -3px;
    top: 0;
    bottom: 0;
    background: green;
    width: 5px;
}

td:first-child {
  width: 100px;
  position: relative; /* required for absolutely positioned pseudo-elements */
}
td {
  width: 50px;
}
<table>
<tbody>
  <tr class='marked'><td>A</td><td>A</td><td>A</td></tr>
  <tr><td>B</td><td>B</td><td>B</td></tr>
  <tr><td>C</td><td>C</td><td>C</td></tr>
</tbody>
</table>
<br>
<table>
<tbody>
  <tr><td>A</td><td>A</td><td>A</td></tr>
  <tr class='marked'><td>B</td><td>B</td><td>B</td></tr>
  <tr><td>C</td><td>C</td><td>C</td></tr>
</tbody>
</table>

Answer №2

Make the following adjustments: Remove border-collapse and add cellspacing="0" cellpadding="0"

 td{
      border:none
    }
    tr {
      background-color: blue;
    }
    tr:nth-child(odd) {
      background-color: grey;
    }
    tr.marked {
      border-left: 5px solid green;
    }

    td:first-child {
      width: 100px;
    }
    td {
      width: 50px;
    }


    <table cellspacing="0" cellpadding="0">
    <tbody>
      <tr class='marked'><td>A</td><td>A</td><td>A</td></tr>
      <tr><td>B</td><td>B</td><td>B</td></tr>
      <tr><td>C</td><td>C</td><td>C</td></tr>
    </tbody>
    </table>

    <table cellspacing="0" cellpadding="0">
    <tbody>
      <tr><td>A</td><td>A</td><td>A</td></tr>
      <tr class='marked'><td>B</td><td>B</td><td>B</td></tr>
      <tr><td>C</td><td>C</td><td>C</td></tr>
    </tbody>
    </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

"The functionality of the Bootstrap4 tab panel is not working properly for switching

I am currently working on creating 4 tab panels using Bootstrap 4. My goal is for the tab contents to fade in when I click on them. However, I am facing an issue where nothing happens when I click on the other panels. Do you have any insights on why this ...

How to position slides in the center using react-slick

I'm having difficulty achieving vertical centering for an image in a react-slick carousel implementation. The issue can be seen here. https://codesandbox.io/s/react-slick-playground-o7dhn Here are the problems identified: Images are not centered: ...

A pair of buttons and text with varying alignment

Here is the code from my popup.htlm file: HTML <body> <select style="width: 100%;" multiple id="HLSlist"> </select> <span style="display:inline"> <button type="button" id="Deletebut">Delete</button> ...

Why won't my Nivo Slider images print? How can I make my Nivo Slider images appear on printouts?

I am having trouble printing the nivo slider (default-theme): Despite trying for a few days, I still can't figure out why it's not working. I have attempted solutions like #slider { visibility: visible; width: 950px; height: 35 ...

Python AssertionError: The argument passed to the write() function in GAE must be a

Currently, I am utilizing Sublime Text 2 as my primary editor to develop a brand new Google App Engine project. UPDATE: While testing this code via localhost, I encountered an issue when accessing the app on appspot: Status: 500 Internal Server Error ...

Using a width of 100% with the display inline-block property does not function as intended

Is there a way to have both a checkbox and text input in one line, with the text input being responsive? I attempted to use display: inline-block, which worked for keeping them in the same line, but I'm having trouble making the text input fill the re ...

Colorful background visuals without any text

Can you achieve a background color effect using Stylus without any text? span { width: 5px; height: 5px; } span.red { background-color: #f00; } <span class="red"></span> ...

Shifting the entire page from left to right and back again

I am in search for a template that moves the page from left to right. If anyone can guide me on how to create this effect or provide a JavaScript example, I would greatly appreciate it. ...

Utilizing FontAwesome icons instead of png images as a visual source

Is there anyone who can assist me? I currently have SiteSearch360 set up on my website with a full screen search bar that is activated by clicking on an image of a magnifying glass. My goal is to replace the default image (src="https://cdn.sitesearch360. ...

The NG8002 error has occurred, as it is not possible to connect to 'matDatepicker' because it is not a recognized attribute of 'input'

I've come across an issue while working on my Angular 15 application with Angular Material. I'm trying to incorporate a date picker, but after adding the code snippet below, I encountered an error. <mat-form-field appearance="outline" ...

Is it possible to retrieve specific information from a website using the rvest package?

I've been attempting to extract and display the review rating of a song through web scraping using rvest in R from Pitchfork. The rating for this particular song is 8.5, but unfortunately, I am encountering an issue as shown below: https://i.sstatic.n ...

In certain instances, a modal popup may intermittently fail to hide after the page has finished loading, an issue observed

Upon page load, an ajax modal pop-up is displayed and then hidden once the loading process is complete. This functionality is defined within the master page. The issue arises when certain button clicks fail to trigger the window.onload javascript event, as ...

How come my directive is being updated when there are changes in a different instance of the same directive?

For the purpose of enabling Angular binding to work, I developed a straightforward directive wrapper around the HTML file input. Below is the code for my directive: angular.module('myApp').directive('inputFile', InputFileDirective); f ...

Ways to conceal overflow at the base of a container while enabling the image to break free from the top of the container

Trying to create a visual effect where an image of a person appears to be partially hidden inside a container, with the bottom part concealed by overflow: hidden. However, I want the top part of the image to extend out of the container. Is there a way to a ...

What is the method for including a TabIndex property in a JSON file?

https://i.sstatic.net/ISi72.png I discussed the integration of HTML fields within a JSON file and highlighted how to utilize the TabIndex property effectively in JSON files. ...

Issues with Jquery datatable causing column header to overlap the table header

I am facing an issue with my datatable as the table is not displaying correctly. Could someone advise me on how to troubleshoot and fix this problem? Below is the code snippet in question: $(tbName).dataTable({ "sScrollX": "100%", "sScro ...

Ways to emphasize search outcomes in Flask/HTML?

I am currently working on creating a search box using HTML and the Flask framework in Python. Below is the layout I am working with: Layout My goal is to input text into the search box and have it highlighted within the text area on the same HTML page. F ...

Clickable functionality disabled for form elements

I am encountering an issue with my system development task. The form elements appear to be unclickable, preventing any data entry in the fields. I have attempted moving the form tag above the first div in the code structure below as a troubleshooting step, ...

Using AJAX to load multiple pages asynchronously

Recently, I put together a website using the following script: <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script> function loadpage(page) { document.getElementById( ...

Change the height of a div after submitting a form using Django (Python)

I have a registration form with one submit button. Upon clicking submit, if there is an error, the height of the div increases by setting it to height:auto. However, when I click submit, it changes the height of the div (.continer). Strangely, after one ...