CSS Code Failing to Adjust Inner Components Width in Variable Table

I'm facing an issue while trying to create an excel-style table using HTML and CSS. The problem arises when I attempt to have a varying number of columns in different rows, as the table exceeds the border limit and expands on its own. My goal is to replicate the layout shown in the image.https://i.stack.imgur.com/4VhR6.png

table
   {
    table-layout: fixed;
    border-collapse: collapse;
    width: 100%;
    border: 3px solid #151515;
   }
   th, td
   {
    border: 1px solid #151515;
    padding: 12px;
    text-align: center;
   }
   th
   {
    font-weight: bold;
   }
   tfoot tr:nth-child(2)
   {
    font-weight: normal;
    height: 100px;
   }
   .colored td
   {
    background-color: #aaaaaa;
   }
<table>
  <tbody>
    <tr>
      <th colspan="4" class="title">PURCHASE DETAILS</th>
    </tr>
    <tr class="colored">
      <th rowspan="2">BUDGET CODE:</th>
      <td>Account Code</td>
      <td>Cost Center</td>
      <td>Project Code</td>
      <td>DEA</td>
    </tr>
    <tr>
      <td>5458</td>
      <td>22222</td>
      <td>3658954</td>
      <td>95874</td>
    </tr>
    <tr>
      <th>PO Number:</th>
      <td>PO/SC/2010/33</td>
      <th>Supplier:</th>
      <td>STORM TECH</td>
    </tr>
    <tr>
      <th>SOF:</th>
      <td>00254584</td>
      <th>Value (Purchase Price USD):</th>
      <td><b>1,084.89</b></td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <th colspan="2">Plan After Award Ends:</th>
      <td colspan="2">Carry Over</td>
    </tr>
  </tfoot>
</table>

If anyone could provide assistance, it would be greatly appreciated.

Answer №1

Using a table layout may not be the ideal choice in this situation, but it is possible to make it work. You can achieve the desired effect by creating more cells per row and spanning the cells over 2 or 3 "cells"

table
   {
    table-layout: fixed;
    border-collapse: collapse;
    width: 100%;
    border: 3px solid #151515;
   }
   th, td
   {
    border: 1px solid #151515;
    padding: 12px;
    text-align: center;
   }
   th
   {
    font-weight: bold;
   }
   tfoot tr:nth-child(2)
   {
    font-weight: normal;
    height: 100px;
   }
   .colored td
   {
    background-color: #aaaaaa;
   }
<table>
  <tbody>
    <tr>
      <th colspan="8" class="title">PURCHASE DETAILS</th>
    </tr>
    <tr class="colored">
      <th rowspan="2">BUDGET CODE:</th>
      <td colspan="2">Account Code</td>
      <td colspan="2">Cost Center</td>
      <td colspan="2">Project Code</td>
      <td>DEA</td>
    </tr>
    <tr>
      <td colspan="2">5458</td>
      <td colspan="2">22222</td>
      <td colspan="2">3658954</td>
      <td>95874</td>
    </tr>
    <tr>
      <th>PO Number:</th>
      <td colspan="3">PO/SC/2010/33</td>
      <th colspan="2">Supplier:</th>
      <td colspan="2">STORM TECH</td>
    </tr>
    <tr>
      <th>SOF:</th>
      <td colspan="3">00254584</td>
      <th colspan="2">Value (Purchase Price USD):</th>
      <td colspan="2"><b>1,084.89</b></td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <th colspan="2">Plan After Award Ends:</th>
      <td colspan="6">Carry Over</td>
    </tr>
  </tfoot>
</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

A program designed to access and retrieve a universal variable

It seems like a simple task, but I can't seem to figure it out. I'm trying to assign the currentUserId within the getCurrentUser function so that I can use it in other functions. Currently, the code below is returning undefined. What am I overl ...

Is there a way to collapse child elements in a hover sidebar using Vuetify?

My project includes a sidebar that opens when I hover over it with the mouse. Everything works fine, but within the sidebar, there is a collapse dropdown menu. When I open this menu and then move the mouse away from the sidebar and back again, the collapse ...

Is it possible to implement localStorage for auto-filling multiple forms simultaneously on a single webpage

I have been exploring a code snippet by Ke Yang that uses localStorage to synchronize text in form fields across different fields. I am interested in implementing something similar. On a page where I have a library of downloadable items, there are approxi ...

"Browser compatibility issues: 404 error on post request in Firefox, while request is

When following this tutorial on React and PostgreSQL, the app should display the JSON fetch in the bash terminal around the 37-minute mark. However, there seems to be a problem as there is no feedback showing up on the npm or nodemon servers. After tryin ...

The resume button is failing to activate any functions

I recently encountered an issue with a JS file that is associated with a Wordpress Plugin, specifically a Quiz plugin featuring a timer. I successfully added a Pause and resume button to the quiz, which effectively pauses and resumes the timer. However, I ...

Process called gulp useref eliminates certain files from the pipeline

Is there a way to exclude the gulp.src file from output? I am aiming to bundle only JavaScript and output .js files, not HTML. The following blocks in base.html are utilized for bundling JavaScript with gulp-useref: <!-- build:js app.core.js --> &l ...

Tips for successfully passing an object as a prop in nextjs

Struggling to understand how to pass an object as a prop using useState in Next JS. My javascript functions include a lorem ipsum generator, housed in a component called Paragraphs. This component requires two properties: Number of paragraphs Sentence le ...

Is there a way for me to update the button text and class to make it toggle-like

Is there a way to switch the button text and class when toggling? Currently, the default settings show a blue button with "Show" text, but upon click it should change to "Hide" with a white button background. <button class="btn exam-int-btn">Show< ...

What is the process for incorporating Angular.js with a live messaging platform such as Pusher or PubNub?

Can Pusher or PubNub be implemented as an Angular service? Anyone have any examples of code for this kind of integration? ...

Does the Apps Script parser JSON.parse() incorrectly remove leading zeros from string object keys?

I am facing an issue with my Apps Script Web App where JSON data is being manipulated when I try to parse it. Specifically, keys with leading zeros are being altered (e.g "0123" becomes "123") during the JSON.parse() function call. It seems like the functi ...

Can you identify the reason for the hydration issue in my next.js project?

My ThreadCard.tsx component contains a LikeButton.tsx component, and the liked state of LikeButton.tsx should be unique for each logged-in user. I have successfully implemented the thread liking functionality in my app, but I encountered a hydration error, ...

I experienced an issue with Firestore where updating just one data field in a document caused all the other data to be wiped out and replaced with empty Strings

When updating data in my Firestore document, I find myself inputting each individual piece of data. If I try to edit the tag number, it ends up overwriting the contract number with an empty string, and vice versa. This issue seems to stem from the way th ...

The Php Include function is known to create unnecessary whitespace and disregard any specified styles

On one of my web pages, I am using the include() function to bring in content from another page. This external page contains both HTML and PHP code, with echoes for output. Here's an example: The page Apple.php looks like this: <html> <head ...

Using two different Readable streams to pipe to the same Writable stream multiple times

In my current project, I am facing the challenge of concatenating a string and a Readable stream. The Readable stream is linked to a file that may contain data in multiple chunks, making it quite large. My objective is to combine these two entities into on ...

Stop jQuery from adding duplicate values to a table

When I make an AJAX call using jQuery and PHP to receive JSON response, I am encountering a problem with duplicate values. The code is functioning correctly, but when selecting an option from the drop-down list, duplicate entries appear. The scenario invol ...

Check whether the username variable is blank; if it is, then refrain from navigating to page2.php

Introducing meekochat, a live chat website where users can connect with others. To get started, simply input your name on page1.php and you'll be directed to page2.php for chatting. However, I have implemented a feature in my code to ensure that if th ...

What is the best way to utilize the existing MUI state in order to calculate and show column totals?

I am currently in the process of developing an MUI web application to keep track of some personal data. Within this application, I have incorporated the MUI datagrid pro component to efficiently display the data with its robust filtering capabilities. In ...

What steps should I take to ensure the privacy of this React Layout?

To ensure only authenticated users can access the layout component, we need to implement a check. const router = createBrowserRouter([ { path: "/", element: <Layout />, children: [ { path: "/", ...

What is the most effective way to utilize a loop in order to generate a comma-separated list of values that does not contain an additional comma

Using @for to generate multiple box-shadow arguments: .myclass { $bxsw : ""; @for $i from 1 through 10 { $bxsw : $bxsw + " -" + $i + "px -" + $i + "px " + brown + ","; } box-shadow: #{$bxsw}; } This results in: .myclass { bo ...

Nested Angular click events triggering within each other

In my page layout, I have set up the following configuration. https://i.stack.imgur.com/t7Mx4.png When I select the main box of a division, it becomes highlighted, and the related department and teams are updated in the tabs on the right. However, I also ...