Tips for eliminating the gap above the initial HTML table row while utilizing border-collapse: separate

Currently, I am utilizing the border-collapse: separate property to introduce spacing between table rows in my design. However, I am facing a challenge when trying to eliminate the space above the initial table row. What would be the most effective approach to address this issue?

The following is a visual representation of how my table appears at present.

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

Here is the code snippet that I am working with:

<!DOCTYPE html>
<html>
<head>
    <title>Table</title>
    <style>
        .table-container {
            width: 400px;
            background-color: grey;
            padding: 0px 10px 10px 10px;
        }

        table { 
            border-collapse: separate;
            border-spacing: 0px 7px; 
            width: 100%;
        } 
                
        tr { 
            background-color: #dc2626; 
            color: white; 
        } 

        td {
            padding: 5px;
        }

        tbody tr td:first-child {
            border-top-left-radius: 6px;
            border-bottom-left-radius: 6px;
        }

        tbody tr td:last-child {
            border-top-right-radius: 6px; 
            border-bottom-right-radius: 6px;
        }
    </style>
</head>
<body>
    <div class="table-container">
        <table>
            <tbody>   
                <tr><td>value 1</td><td>value2</td></tr> 
                <tr><td>value3</td><td>value4</td></tr> 
            </table></tbody>
        </table>
    </div>
    <script>
    </script>
</body>
</html>

I made an attempt by using the border-collapse: collapse property and setting border-bottom. Although it successfully removed the spacing above the first row, it caused issues with achieving rounded corners on all table rows. Below is a quick demonstration of the modified code:

<!DOCTYPE html>
<html>
<head>
    <title>Table</title>
    <style>
        .table-container {
            width: 400px;
            background-color: grey;
            padding: 0px 10px 10px 10px;
        }

        table { 
            border-collapse: collapse;
            width: 100%;
        } 
                
        tr { 
            background-color: #dc2626; 
            color: white; 
            border-bottom: 7px solid grey;
        } 

        td {
            padding: 5px;
        }

        tbody tr td:first-child {
            border-top-left-radius: 6px;
            border-bottom-left-radius: 6px;
        }

        tbody tr td:last-child {
            border-top-right-radius: 6px; 
            border-bottom-right-radius: 6px;
        }
    </style>
</head>
<body>
    <div class="table-container">
        <table>
            <tbody>   
                <tr><td>value 1</td><td>value2</td></tr> 
                <tr><td>value3</td><td>value4</td></tr> 
            </table></tbody>
        </table>
    </div>
    <script>
    </script>
</body>
</html>

Is there a method that allows for both the elimination of spacing above the initial row and the implementation of rounded corners on all row elements within the table?

Answer №1

It appears that utilizing a negative margin is the only way to counteract border spacing.

By the way, there seems to be a mismatch in your code with the table closing tag </table>.

.table-container {
    /* negative margin to compensate for border-spacing  */
    margin-top: calc(var(--border-space-vertical) * -1);
    width: 400px;
    background-color: grey;
    padding: 0px 10px 10px 10px;
  }

<!DOCTYPE html>
<html>
  <head>
    <title>Table</title>
    <style>
      body {
        margin: 0;
      }
      :root {
        --border-space-vertical: 10px;
      }
      .table-container {
        /* negative margin to compensate for border-spacing  */
        margin-top: calc(var(--border-space-vertical) * -1);
        width: 400px;
        background-color: grey;
        padding: 0px 10px 10px 10px;
      }
      table {
        width: 100%;
        border-collapse: separate;
        border-spacing: 10px var(--border-space-vertical); /* Adjust spacing values as needed */
      }
      tr {
        background-color: #dc2626;
        color: white;
      }
      td {
        padding: 5px;
      }
      tbody tr td:first-child {
        border-top-left-radius: 6px;
        border-bottom-left-radius: 6px;
      }
      tbody tr td:last-child {
        border-top-right-radius: 6px;
        border-bottom-right-radius: 6px;
      }

      tr:first-child {
        border-top: none;
      }
    </style>
  </head>
  <body>
    <div class="table-container">
      <table>
        <tbody>
          <tr>
            <td>value 1</td>
            <td>value2</td>
          </tr>
          <tr>
            <td>value3</td>
            <td>value4</td>
          </tr>
        </tbody>
      </table>
    </div>
    <script></script>
  </body>
</html>

Answer №2

The CSS property border-spacing: 0px 7px; controls the spacing between rows and above the first row in a table.

In order to eliminate the space above the first row, you can try the following methods:

  1. Remove the border-spacing: 0px 7px; style altogether
  2. Add margin-top: -7px to either the table or its container

Refer to the example below for an illustration of the first solution:

<!DOCTYPE html>
<html>
<head>
    <title>Table</title>
    <style>
        .table-container {
            width: 400px;
            background-color: grey;
            padding: 0px 10px 10px 10px;
        }

        table { 
            border-collapse: separate;
            width: 100%;
        } 
                
        tr { 
            background-color: #dc2626; 
            color: white; 
        } 

        td {
            padding: 5px;
        }

        tbody tr td:first-child {
            border-top-left-radius: 6px;
            border-bottom-left-radius: 6px;
        }

        tbody tr td:last-child {
            border-top-right-radius: 6px; 
            border-bottom-right-radius: 6px;
        }
    </style>
</head>
<body>
    <div class="table-container">
        <table>
            <tbody>   
                <tr><td>value 1</td><td>value2</td></tr> 
                <tr><td>value3</td><td>value4</td></tr> 
            </table></tbody>
        </table>
    </div>
    <script>
    </script>
</body>
</html>

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

Checkboxes and the adjacent selector

Looking for a way to change the background color of a label when the corresponding checkbox is checked? Take a look at the code snippet below to see how it can be done: .viewbutton { height: 48px; width: 48px; background-color: #FFFFFF; border-r ...

Troubleshooting Cross-Origin Resource Sharing (CORS) problem in Jquery

When using AJAX post from jQuery to call two REST services on different domains for business logic, a CORS issue arises. By setting crossDomain: true in my AJAX call following Google references, it works fine without specifying headers. However, if I inc ...

Issue with React: Caret icon in dropdown menu shifts position when page is resized to iPad or smaller dimensions

I'm facing an issue with a styled react component that has a custom dropdown menu featuring a caret icon. Whenever I test the responsiveness of the dropdown, the caret icon ends up outside the intended area. To resolve this, I've wrapped the drop ...

Modifying an HTML list item to become 'active' in a navigation bar

One way I've been implementing my navbar on each page is by using the following code at the bottom of the page within script tags: $("#navbar-partial").load("navbar.html). The code for the navbar list looks like this: <ul id="main-nav" class="nav ...

How can you use plain javascript to drag and drop the cross sign within the box in the grid?

Creating a grid in HTML where clicking on a box will draw an x sign and remove it if clicked again. View the DEMO here. Challenge Description:- I am now looking to implement dragging the cross (x) sign to another grid within the box, but cancelling the ...

Tips for displaying text within an input field labeled "password" and then reverting back to a password display after entering text

I am working with a login form that includes a password field: <input type="password" name="password" id="password" value="Password" maxlength="40" required style="width: 130px; font-size: 10px;" /> Currently, the password field displays "******** ...

The <a> tag with href attribute is mistakenly identified as an external link when used in conjunction with the <base> tag

I am currently working with jQuery UI tabs using jquery 1.11.1 and jQuery ui 1.11.1. Within the <base> tag in the head>, I have the following: <base href="http://mytestdomain.com/" /> Upon loading the page, jQuery UI creates two tabs from ...

Incorrect rendering of the <li> tag

I've been working on creating a simple web "to do list" but I've encountered an issue. When I manually add todos and click on them, the 'text-decoration: line-through;' property is ignored, and I can't see the strikethrough effect ...

Refresh the page when the dropdown selection is changed and send the new value

I'm currently working on a page that includes a dropdown menu. My goal is to have the page reload after selecting an option from the dropdown so that it returns to the same page with the selected value passed through. Here's the PHP code for th ...

How to style date strings in JavaScript, jQuery, or CSS

Below are dates that need formatting: 7-sep, 14-sep, 21-sep, 28-sep, 5-oct,12-oct, 19-oct,26-oct, 2-nov, 9-nov, 16-nov, 23-nov,30-nov, 7-dec,14-dec, 21-dec,28-dec-2013. Is there a way to tidy them up using JavaScript, jQuery, or CSS? Ideally, I would lik ...

The jQuery gallery is experiencing some functionality issues

Having an issue with the gallery on my website (currently on my computer, not yet uploaded to a server). Here is the script for the gallery (loaded from the server using PHP): $(document).ready(function() { $('.gallery').hide(); $(' ...

The background remains hidden from view as a result of its positioning

There seems to be an issue with the background color of my list not displaying correctly. The problem arose after floating elements left and right. Any suggestions on how to resolve this? If you'd like to take a look, here's the link to the fidd ...

Is it possible to swap a <div> element with the content of another HTML page using the .innerHTML method?

I am currently working on a project that involves loading different webpages into a <div> on my page once specific links are clicked. I came across a thread about using jQuery for this purpose, but I'm not familiar with it. Is there a way to ach ...

Enable the Angular button only when the radio button has been selected

Just starting out with Angular and I have a query. This is a scenario for my project at work. https://i.stack.imgur.com/R3SxA.png In this screenshot, I want to enable the "ajouter" button when at least one radio button is selected in the secure chest (s ...

Header Logo centered at the top

I am using a Bootstrap 4 navbar where the nav-brand is centered on a computer window, but shifts to a dropdown when expanded on mobile. Is there a way to keep the brand fixed at the top and center? Here is my navbar code in HTML: <nav class="navba ...

Organize group data by month in a visually appealing table using HTML/PHP with

I have successfully implemented a code that prints HTML table rows from a database table and sorts them by date. The challenge lies in the fact that the date is stored as VARCHAR (due to a pre-existing project with an active database used in a PHP web app ...

Get Python CSV file with Callback feature

When trying to download a CSV file from Morningstar by clicking on a button link, the presence of a callback in the URL seems to be hindering the download process. Even when pasting the URL (http://financials.morningstar.com/finan/ajax/exportKR2CSV.html?&a ...

The CSS3 selector matching a value starting with "[attribute^=value]" does not match a value that has spaces after the quotation marks

When using the div[class^="test"] selector, keep in mind that it will not select an element with a class attribute like class=" test-something" (This selector was generated on the backend.) Using the div[class^=" test"] is not a valid choice. ...

Using JSTL within JavaScript

Can JavaScript be used with JSTL? I am attempting to set <c:set var="abc" value="yes"/> and then later retrieve this value in HTML and execute some code. The issue I'm facing is that the c:set always executes even when the JavaScript condition ...

Integrating a footer into the enhanced search tab slider

I'm struggling to create a sticky footer like the one on w3schools. Even though I used the same code in my material UI demo, it's not functioning properly. I tried debugging by changing the position from fixed to absolute, but it still isn&apos ...