The bold horizontal line in HTML tables is a bit unusual and is caused by the border-collapse property

Edit 1: The strange issue appears to be related to the border-collapse property in CSS, as it can be resolved by using border-spacing: 0px. However, the question remains, why does border-collapse result in this behavior? It seems to be connected to scaling, where zooming in or out of the window causes these abnormal bold lines to appear. In my case, I have set both the browser and system (Windows 11) to default zoom levels, specifically at the recommended 125% scaling. I am puzzled as to what could be causing this issue.

Lately, I have noticed an odd horizontal line appearing after certain rows when attempting to create an HTML table. I have included a screenshot for reference. The code consists of a basic HTML table structure with simple CSS to include borders without any complexities. Here is the code snippet:

table.html

<table>
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Phone</th>
        <th>Email</th>
    </tr>
    <?php while($user= mysqli_fetch_array($query)){ ?>
    <tr>
        <td><?php echo $user['id']; ?></td>
        <td><?php echo $user['name']; ?></td>
        <td><?php echo $user['phone']; ?></td>
        <td><?php echo $user['email']; ?></td>
    </tr>
    <?php } ?>
</table>

style.css

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

Answer №1

The issue stems from the way browsers handle scaling improperly.

For example, if you have a 1px border and set DPI Scaling to 150%, technically the border would be 1.5px. However, due to the lack of sub-pixel rendering in browsers, it may alternate between 1px and 2px.

There are various approaches to address this problem.


1. Adjust the border-width to 0.01px

A temporary solution that forces the browser to default to at least 1px


2. Utilize resolution media queries

Modify the border-width based on the appropriate decimal for each scaling level.

@media (min-resolution: 120dpi) {
  table, th, td {
    border-width: 0.75px;
  }
}

@media (min-resolution: 144dpi) {
  table, th, td {
    border-width: 0.666px;
  }
}

@media (min-resolution: 192dpi) {
  table, th, td {
    border-width: 0.5px;
  }
}

Here is an overview of the different DPI Scaling Levels and their corresponding values

DPI Scaling Level

  • Smaller 100% (default) = 96 dpi
  • Medium 125% = 120 dpi
  • Larger 150% = 144 dpi
  • Extra Large 200% = 192 dpi

3. Incorporate JavaScript

Adjust the border-width dynamically using JavaScript and the window.devicePixelRatio variable to match the scaling level.

Answer №2

By utilizing a solution from DvdRom, I was able to resolve the issue using Javascript and CSS variables to achieve a border-spacing of 1px. This approach has been tested and confirmed to work effectively on various browsers including Firefox, Chrome, Edge, Opera, and Brave.

let cssRoot = document.querySelector(':root');
cssRoot.style.setProperty
(
     '--table-border-spacing',
     `${1 / window.devicePixelRatio}px`
);
:root {
    --table-border-spacing: 1px;
}

table {
    border-collapse: separate;
    border-spacing: var(--table-border-spacing);
}

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

Looking for a way to make CSS text color for a:hover have higher priority than a:visited?

I'm having an issue with my CSS code that changes the background color when hovering over a link. The text color is white on a blue background during hover, but if there is no hover, it's blue with a white background. Additionally, once the link ...

How can I add information into a nested div element?

I encountered an issue when attempting to insert data into my block. The div structure is as follows: <div class="1"> <div class="2"> <div class="3"> <div class="4"></div> </div> ...

Adjust size of element in relation to Background Cover using jQuery

Can you help me solve a challenge I'm facing? I have a website with a fullsize background image, and I need to attach a div to a specific position on the image while ensuring that it scales in the same way as the background image with the "background ...

Is it possible to move a directive within a limited parent element?

Is there a way to limit the movement of an angular.js drag directive within the confines of its parent element? You can see the problem I'm facing in this jsbin: http://jsbin.com/maxife/3/edit?html,css,js,output ...

Transfer a document to a php server using AJAX and jQuery in place of a traditional form

Is there a way to use AJAX for uploading files in PHP without reloading the page or performing additional functions? I want to keep it simple. Any suggestions? <form action="upload.php" method="post" enctype="multipart/form-data"> <label st ...

Can anyone provide guidance on how to create this particular shadow effect using CSS?

Is there a method to use CSS to create a green shadow in that particular shape? ...

Assign a background image to a master page utilized in subdirectories

In my website's root directory, I have a master page. Inside this root directory, there is a folder named Images which contains the background image for my master page. When I apply this master page to web pages located in the root directory, everythi ...

Using a div in React to create a vertical gap between two React elements

I am working with two React Components in my project - the Right Column and the Left Column. I am trying to create space between them using a div tag. Currently, my setup in the App.js file looks like this: import React from 'react'; import LeftC ...

Tips for avoiding flickering in a background image when it is being changed

Utilizing JavaScript, I am setting a repeated background image from a canvas to a div in the following way: var img_canvas = document.createElement('canvas'); img_canvas.width = 16; img_canvas.height = 16; img_canvas.getContext('2d' ...

Tips for including numerous hyperlinks in a single row for a website's navigation menu

I need assistance in creating a navigation bar for my website that includes multiple headers with links. I am not sure if the issue lies within my CSS, HTML, or both. Can someone please help me troubleshoot? index.html <!DOCTYPE html> <html> ...

Customizing the input field to have a width of 100%

Could someone provide a solution, like a link or working example, for creating input fields with rounded corners that have a width of 100% and a customizable right border? Here is the current structure I am using: <span class="rounded left-corner" ...

Illustrating a fresh DOM element with intro.js

I am currently utilizing intro.js to create a virtual 'tour' for my website. As I aim to provide a highly interactive experience for users, I have enabled the ability for them to engage with highlighted DOM elements at different points in the tou ...

Connect to dynamically generated div on separate page

I am facing an issue with my navigation bar drop down menu that displays event titles fetched from the database. I want users to be able to click on a specific event and have the page scroll to the dynamically generated content for that event. However, it ...

The linking process in AngularJS is encountering difficulties when trying to interact with

I've already looked at similar questions, and my code seems correct based on the answers provided. It's a very simple beginner code. <html ng-app=""> <head> <title>Assignment</title> <script src=" ...

An alternative to PHP's exec function in Node.js

I am interested in developing a piece of software using C (such as prime number factorization) and hosting it on my web server with Node.js. Following that, I would like to create an HTML document containing a form and a button. Upon clicking the button, ...

What are some ways to create a one-sided design without being limited by container constraints

How can I create a block on my site where there are no container restrictions on the right, but they are limited on the left? The goal is to have an image pressed against the right edge of the page while keeping the container limits on the left. Here is my ...

Bootstrap - the input group button is designed to align to the right side

Currently, I am in the process of constructing a navigation bar for a website that includes a search bar and a grouped button. Everything functions properly when viewed on a desktop, but as soon as I switch to mobile view and the collapsible menu is activa ...

Enter the variable into the parameter

I'm working with some Javascript code: document.getElementById("Grid").style.gridTemplateRows = "repeat(3, 2fr)"; I'm trying to insert a variable as an argument to modify my CSS style. When I attempt the following: "repe ...

Optimal method for showcasing a multitude of columns on an HTML page

Seeking to showcase a vast array of data in HTML (over 10,000 columns with approximately 200-300 rows) to present a schedule. The information will be structured as movable blocks containing text and background colors. Is it feasible to exhibit such a mas ...

Directing BeautifulSoup to a specific <tr> class

I'm currently working on a project where I need BeautifulSoup to extract the numbers located in the "Units Sold" column: from bs4 import BeautifulSoup from urllib import urlopen html = urlopen('http://www.the-numbers.com/home-market/dvd-sales/2 ...