Spacing between table cells is only applied to the first row and footer

In an attempt to add spacing between the first row, second row, and footer of a table, I have experimented with cell spacing combined with border-collapse. However, the spacing is being applied all over the table instead of in specific areas. I am utilizing MUI styled components with React for this project. I have explored different solutions such as using multiple tables, applying CSS rules like border-spacing and border-collapse, but none of them seem to be coding it correctly.

I aim to create a table layout that resembles the following:

https://i.stack.imgur.com/zQl5b.png

Below is an excerpt from the existing code:

<table class="tableClass">
  <thead>
    <tr>
      <th>header 1</th>
      <th>header 2</th>
      <th>header 3</th>
      <th>header 4</th>
      <th>header 5</th>
    </tr>
  </thead>
  
  <tbody>
    <td>content 1</td>
    <td>content 2</td>
    <td>content 3</td>
    <td>content 4</td>
    <td>content 5</td>
  </tbody>
</table>

.tableClass{ border-collapse: separate; border-spacing: 10px 0px;}

UPDATE (Solution)

To achieve the desired effect, leaving an empty cell and applying the empty-cell: hide rule seems to work effectively. Thank you for the assistance, and I apologize for any confusion in my initial question.

Answer №1

If you only have access to limited code, one option is to utilize css pseudo selectors like :first-of-type or :nth-of-type to apply styles specifically to the first or second columns/rows. For instance, you can increase the padding of the first cells in a column with the following CSS snippet:

.tableClass tr > th:first-of-type, 
.tableClass tr > td:first-of-type { padding-right: 40px; }
<table class="tableClass">
  <thead>
    <tr>
      <th>header 1</th>
      <th>header 2</th>
      <th>header 3</th>
      <th>header 4</th>
    </tr>
  </thead>
  
  <tbody>
    <td>cell 1</td>
    <td>cell 2</td>
    <td>cell 3</td>
    <td>cell 4</td>
  </tbody>
</table>

However, for more complex layouts, it's recommended to consider using multiple tables or CSS grid instead. With multiple tables, you may need to restructure your components to output separate tables rather than just new cells and rely on CSS for positioning adjustments. Another workaround could involve wrapping cell content in divs styled with borders as shown below:

.tableClass{ border-collapse: separate; border-spacing: 0; }
.tableClass tr div { border: 1px solid #000; padding: 2px 4px; min-height: 1.2rem; }
.tableClass tr > th:first-of-type, 
.tableClass tr > td:first-of-type { padding-right: 40px; }
<table class="tableClass">
  <thead>
    <tr>
      <th><div>header 1</div></th>
      <th><div>header 2</div></th>
      <th><div>header 3</div></th>
      <th><div>header 4</div></th>
    </tr>
  </thead>
  
  <tbody>
    <td><div>cell 1</div></td>
    <td><div>cell 2</div></td>
    <td><div>cell 3</div></td>
    <td><div>cell 4</div></td>
  </tbody>
</table>

Keep in mind that setting div heights can be tricky, so for more flexibility in handling complex layouts, CSS grid would be the preferable choice.

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

Tips for applying CSS styles to the active page link

Check out the code below: Is there a way to assign a specific class to the current page link so that the mouse cursor will display as default rather than changing to a hand icon? I'm looking for a solution where I can apply a class to the active lis ...

Resizing cell height in an HTML table is not supported by Angular 5

In my angular application, I've implemented a component that represents a basic HTML table. However, I'm facing an issue with reducing the height of cells within the table. It seems like my CSS styling is not affecting the display as desired. He ...

Why isn't onBlur recognizing the input in Mui components?

Currently, I am utilizing MUI tags. The default behavior allows tags to be accepted when pressing Enter. However, my goal is to achieve the same functionality upon focus-out from the input field. I have implemented the following code for handling onBlu ...

Can someone explain the functionality of the CSS Selector > * when used inside a Vue.js component?

While exploring the Vuestic admin dashboard tool, I noticed a glitch in the UI where the donut chart appeared larger than its container. I tried to troubleshoot and fix it for submission as a PR, but I encountered an unfamiliar CSS selector > * while de ...

Is it possible to create a translucent glass floating box over HTML elements, similar to the frosted glass effect seen in iOS7?

Creating an overlapping div with a frosted glass effect typically requires using an image background (like ). I am interested in having a floating div (position:fixed) that can apply a frosted glass effect over any content below it, whether it be an image ...

Utilizing the Bootstrap grid system allows for input fields to automatically expand to 100% width once they reach

I am encountering some challenges while attempting to develop a responsive form using bootstrap grids. The issue seems to arise when resizing the browser window, as the grid classes behave unexpectedly by expanding and taking up the entire page width at ce ...

The Font Awesome icons do not display offline

I'm having trouble using Font Awesome icons offline on my webpage. I've included the necessary code, but for some reason it's not working. Here is what I have: <html> <head> <title>font-awesome example</title> ...

After deploying my next.js app on cPanel, it suddenly displays a blank screen

I recently deployed my Next.js app on cPanel and encountered a blank screen issue, although it works perfectly fine when tested locally. Here's the detailed process I followed: 1. To begin with, I created a custom server.js file in the root directory ...

Experiencing an excessive amount of re-renders due to state setting? Consider implementing useSW

I am currently utilizing useSWR to fetch data and then calculating a total using the reduce method. Strangely, when I try to update the state with the calculated value, it triggers multiple re-renders causing the 'Too many re-renders' error messa ...

Unique hover effects for tab navigation

I have designed my website with 4 tabs on the homepage: holidays, hospitality, events, and consulting. I am trying to create an effect where hovering over the tabs displays images. Here is the code I have tried: HTML: <div class="menu_links"> & ...

Encountering issues when attempting to post JSON form data to a REST API via AJAX within a React

Recently, I created a basic SignUp page utilizing React and I am looking to transmit the form data to a REST API. In my setup, I have incorporated react.min.js, react-dom.min.js, and jquery.min.js. Here is the code snippet: <div id="container"></ ...

Incorporating Blank Class into HTML Tag with Modernizr

Currently, I am experimenting with Modernizr for the first time and facing some challenges in adding a class to the HTML tag as per the documentation. To check compatibility for the CSS Object Fit property, I used Modernizr's build feature to create ...

"Exploring the Relationship Between Parent and Child Elements in CSS

Is it possible for an id to act as a parent in CSS? For instance: <div id="link"> <a href="www.example.com"></a> </div> The CSS code: #link > a{ /* style would be applied here*/ } Can this actually be achieved? ...

Ensuring Proper Alignment of Headers in CSS

I'm struggling to align my header correctly. Despite getting close in Internet Explorer, it looks terrible in Firefox with the code below. I've scoured forums for solutions but the more I try to fix it, the worse it looks. All I want is for heade ...

Ways to implement the React.FC<props> type with flexibility for children as either a React node or a function

I'm working on a sample component that has specific requirements. import React, { FC, ReactNode, useMemo } from "react"; import PropTypes from "prop-types"; type Props = { children: ((x: number) => ReactNode) | ReactNode; }; const Comp: FC< ...

Creating a modal popup when a button is clicked

After searching online for a way to make my sign up modal pop up when the signup button in my navbar is pressed, I was unable to find any information on how to achieve this. I suspect it involves JavaScript, but my knowledge of JS is limited. I attempted ...

Troubles encountered when attempting to use Axios to call a third-party API in a React JS application

Challenge Description: I set out to create a dropdown menu of post-offices based on the user-entered pincode. To achieve this, I utilized an API and Axios for the backend request. While I successfully populate the dropdown with the necessary data, the is ...

Having trouble deleting the value and deselecting the checkbox item?

Feeling a bit confused about a coding issue I'm facing. The problem lies in the categories listed in my database, which I fetched and used to create a post. Now, I'm attempting to edit that post. The categories are in checkbox format, where check ...

Is there a distinction between em and percentage measurements and do their defaults have specified definitions?

Both em and percentage are defined in relation to other elements or properties. For example, with text-indent, it is related to the width of the paragraph, while with font size, it is related to the browser's default size. The question arises - wh ...

Leveraging the useEffect hook to make multiple API calls in ReactJS

I have a useEffect function in my react component where I am calling the API videoGridState. The issue I am facing is that the API is being called twice; once when the page initially reloads and again when the count changes. I want it to be called only onc ...