When a nested CSS Grid is inserted into an HTML table element, it causes horizontal overflow

    

Generating an invoice dynamically with a CSS grid poses some challenges. To maintain a specific format, I need to define the number of rows and columns in advance along with column widths. Using template rows and columns for this purpose restricts the use of 'auto' values.

For instance, consider a grid with 7 columns and 2 rows - the first row has all 7 columns while the second row contains a single column spanning across all 7. Furthermore, this singular column includes a nested grid consisting of 1 row and 3 columns. The HTML setup below illustrates this structure:

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

Edit Note: While addressing an issue highlighted in the comments, I encountered additional problems with this version as well.

The following code snippet demonstrates the initial setup:

<html>

<head>
    <style type="text/css">
        body {
            font-family: Calibri, monospace;
            font-size: 0.8rem;
            margin: 1rem;
            border: 1px solid;
        }

        table {
            width: 100%;
            height: 100%;
            border-collapse: collapse;
            page-break-inside: auto;
        }

        td section div {
            page-break-inside: avoid;
        }

        table tbody td {
            height: 100%;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }

        main {
            display: flex;
            flex-direction: column;
            height: 100%;
        }

        #invoiceitems {
            display: block;
            height: 100%;
            text-transform: uppercase;
        }

        /* Remaining CSS styles... */

    </style>
</head>

<body>
    <!-- Main content with grid structure -->
</body>

</html>

However, issues arise when incorporating this layout inside a table structure, resulting in horizontal overflow as seen in the image here:

https://i.sstatic.net/7C78f2eK.png

I opted for a table to facilitate header continuity across multiple pages, but the underlying structure is causing unwanted scroll and overflow behavior. Can anyone provide insights into what differentiates the table implementation?

Here's the code snippet within a table context:

<html>

<head>
    <style type="text/css">
        body {
            font-family: Calibri, monospace;
            font-size: 0.8rem;
            margin: 1rem;
            border: 1px solid;
        }

        table {
            width: 100%;
            height: 100%;
            border-collapse: collapse;
            page-break-inside: auto;
        }
        
        /* Additional table-specific styles... */

    </style>
</head>

<body>
    <table>
        <tbody>
            <tr>
                <td>
                    <!-- Content wrapped within a table structure -->
                </td>
            </tr>
        </tbody>
    </table>
</body>

</html>

Your guidance on addressing these concerns would be greatly appreciated!

Answer №1

It has nothing to do with being enclosed in a table. Even when it's not within a table, the content still overflows.

        body {
            font-family: Calibri, monospace;
            font-size: 0.8rem;
            margin: 1rem;
            border: 1px solid;
        }

        table {
            width: 100%;
            height: 100%;
            border-collapse: collapse;
            page-break-inside: auto;
        }

        td section div {
            page-break-inside: avoid;
        }

        table tbody td {
            height: 100%;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }

        main {
            display: flex;
            flex-direction: column;
            height: 100%;
        }

        #invoiceitems {
            display: block;
            height: 100%;
            text-transform: uppercase;
        }

        #itemscontent {
            height: 100%;
            display: grid;
            grid-auto-rows: minmax(1rem, max-content);
            grid-column-gap: 1px;
            align-items: center;
        }

        #itemscontent>div {
            display: flex;
            align-items: center;
            border: 1px solid;
            height: 100%;
            border-top: 0;
            border-right: 0;
            padding: 0 0.25rem;
        }

        .border-left-0 {
            border-left: 0 !important;
        }

        .span-7 {
            grid-column-start: span 7;
        }

        #invoiceinformation {
            text-transform: none;
            border-left: 0 !important;
            display: grid !important;
            grid: "note" "bankdetails" "signature";
            grid-template-columns: minmax(min-content, 40%) minmax(min-content, 35%) minmax(min-content, 25%);
            padding: 0 0.5rem;
        }

        #invoiceinformation div {
            align-self: stretch;
            align-items: center;
            padding: 0 0.5rem;
        }

        #bankdetails {
            border: 1px solid;
            border-top: 0;
            border-bottom: 0;
        }

        #signature {
            display: flex;
            border-left: 0 !important;
            padding-right: 0.5rem !important;
        }
    <main style="padding-bottom: 0">
        <section id="invoiceitems">
            <section id="itemscontent"
                style="grid-template-columns: minmax(min-content, max-content) minmax(min-content, auto) max-content max-content  max-content max-content max-content; grid-template-rows: repeat(2, minmax(1rem, max-content));">

                <div class="border-left-0">S.No</div>

                <div>Description</div>
                <div><b></b></div>
                <div>Days</div>
                <div>Unit</div>

                <div>Price</div>

                <div>Unit Price</div>
                <div id="invoiceinformation" class="span-7">
                    <div id="note">
                        <small>NOTE</small>
                        <br>
                        <text>Certified that the particulars
                            given above are true and correct</text>
                    </div>
                    <div id="bankdetails">
                        <small>COMPANY BANK DETAILS</small>
                        <br>
                        <text>BANK:
                            ABC BANK, A/C. NO: 1111111111111111, IFSC: IABC0000000, Branch: DEF
                            Road, GHI</text>
                    </div>
                    <div id="signature">
                        <text><b>for ABC</b>
                            <br>
                            <br>
                            <br>Authorized Signatory</text>
                    </div>
                </div>
            </section>
        </section>
    </main>

The problem lies not in the table itself but in your use of max-content, which signifies "avoid wrapping at all costs: I would rather overflow than wrap!"

When creating an invoice layout, it would be more appropriate to utilize percentages for column widths, such as:

grid-template-columns: 10% 25% 25% 10% 10% 10% 10%;

body {
            font-family: Calibri, monospace;
            font-size: 0.8rem;
            margin: 1rem;
            border: 1px solid;
        }

        table {
            width: 100%;
            height: 100%;
            border-collapse: collapse;
            page-break-inside: auto;
        }

        td section div {
            page-break-inside: avoid;
        }

        table tbody td {
            height: 100%;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }

        main {
            display: flex;
            flex-direction: column;
            height: 100%;
        }

        #invoiceitems {
            display: block;
            height: 100%;
            text-transform: uppercase;
        }

        #itemscontent {
            height: 100%;
            display: grid;
            align-items: center;
        }

        #itemscontent>div {
            display: flex;
            align-items: center;
            border: 1px solid;
            height: 100%;
            border-top: 0;
            border-right: 0;
            padding: 0 0.25rem;
        }

        .border-left-0 {
            border-left: 0 !important;
        }

        .span-7 {
            grid-column-start: span 7;
        }

        #invoiceinformation {
            text-transform: none;
            border-left: 0 !important;
            display: grid !important;
            grid: "note" "bankdetails" "signature";
            grid-template-columns: minmax(min-content, 40%) minmax(min-content, 35%) minmax(min-content, 25%);
            padding: 0 0.5rem;
        }

        #invoiceinformation div {
            align-self: stretch;
            align-items: center;
            padding: 0 0.5rem;
        }

        #bankdetails {
            border: 1px solid;
            border-top: 0;
            border-bottom: 0;
        }

        #signature {
            display: flex;
            border-left: 0 !important;
            padding-right: 0.5rem !important;
        }
<table>
  <tr>
    <td>
      <main style="padding-bottom: 0">
        <section id="invoiceitems">
          <section id="itemscontent"
              style="grid-template-columns: 10% 25% 25% 10% 10% 10% 10%;">

              <div class="border-left-0">S.No</div>

              <div>Description</div>
              <div><b></b></div>
              <div>Days</div>
              <div>Unit</div>

              <div>Price</div>

              <div>Unit Price</div>
              <div id="invoiceinformation" class="span-7">
                  <div id="note">
                      <small>NOTE</small>
                      <br>
                      <text>Certified that the particulars
                          given above are true and correct</text>
                  </div>
                  <div id="bankdetails">
                      <small>COMPANY BANK DETAILS</small>
                      <br>
                      <text>BANK:
                          ABC BANK, A/C. NO: 1111111111111111, IFSC: IABC0000000, Branch: DEF
                          Road, GHI</text>
                  </div>
                  <div id="signature">
                      <text><b>for ABC</b>
                          <br>
                          <br>
                          <br>Authorized Signatory</text>
                  </div>
              </div>
          </section>
        </section>
      </main>
    </td>
  </tr>
</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

Is there a way to modify a CSS class in Chrome that is only visible when I perform a drag action?

For the website project I am working on, I have an element that only changes its style when I drag something over it. This is achieved by adding a CSS class to it. However, editing this style has proven to be challenging because I cannot live edit it in C ...

Tips for creating a script that compiles all SCSS files into CSS within a Vue 3 project

Within my project, there exists a file named index.scss located in the src/assets/styles directory. Adjacent to this file are multiple folders housing SCSS files that are ultimately imported into index.scss. My objective is to devise a script within the pa ...

How to extract hover CSS property from a Selenium WebElement using XPath in Python

Is there a way to detect if a button changes its background color on hover? I know how to get the cursor property: print webElement.value_of_css_property('cursor') But I am unsure how to capture the hover property. ...

The HR element seems to be fixed in place and is not moving

I'm having trouble figuring out what's causing the issue, so I wasn't sure how to title it. Any help in looking at the code would be greatly appreciated. Here is the problem: The HR tag should be positioned below the last image and not cut ...

What is the reason the text does not have a border around it?

The border is supposed to be around the text, but it isn't showing up. Where did I go wrong? Could it be that I need to set a position? html { width: 100%; height: 100%; margin: 0; padding: 0; } body { width: 100%; height: 100%; marg ...

Effect on Label in WordPress Contact Form 7 When Input Field is Populated

Currently, I am attempting to achieve the floating label effect on Contact Form 7 and have encountered an issue. Although I have successfully implemented the label effect on input:focus, I am struggling to make it persist once text has been entered and foc ...

Troubleshooting Issues with jQuery Accordion Buttons

I have a nearly complete accordion that just needs some adjustments. HTML CODE: <footer> <div> <h1>Hide</h1> <h1>Hide</h1> <h1>Hide</h1> <h1>Hide</h1> ...

Receiving a notification when attempting to log in with incorrect credentials

I am currently working on an Angular login page implementation using a username and password setup. When the user enters incorrect credentials, I want to display an alert message indicating the same. Here is the HTML code snippet for the form: <form [f ...

Guide to turning off the Facebook iframe page plugin

I am facing a challenge with embedding a facebook iframe on my website. I want to disable it once the screen width reaches a specific point, but I am struggling to make the iframe disappear properly. Here is how I have attempted to implement this: window.o ...

Having difficulties including the Stack Overflow website within an iframe

I've been attempting to embed the Stack OverFlow website into an HTML page using an iFrame, but I'm running into some issues. Oddly, when I tried embedding my other two websites, they displayed correctly, but Stack OverFlow is not showing up on m ...

Activate the timepicker in Bootstrap when the user clicks on the input field

Struggling to implement a timepicker feature? Look no further than this resource. After adding the necessary js and css to your project, you may encounter issues with the popup not appearing when clicked. The code provided offers a TextBox control with s ...

JavaScript - Attempting to insert a value into a text field by clicking a button

I am struggling to get my function to properly add the value of the button to the text field when clicked by the user. HTML <form name="testing" action="test.php" method="get">Chords: <textarea name="field"& ...

Is there a way to use Jquery to determine if a parent div contains a scroll

I'm trying to find a jQuery example that checks if any parent div has a scroll bar, but I can't seem to locate a helpful one. Here is the code snippet I am working with: <div> <div class="heading"> <div class="visit ...

What is the method to display Post values to the user without necessitating a page reload?

Currently, I am attempting to utilize Ajax to send the user's inputted data from a JSP page. Through Ajax, I have successfully transmitted the variable "vars" to the Jsp page. The output upon registration includes: Registration is Successful. Welcome ...

"Enhance User Experience: Use CSS to highlight text selection based on

As a beginner in the world of css and html, I am eager to create a webpage that showcases a list of names with the ability to select one or multiple names. Instead of simply checking a box on the side, I would love for the background of the selected text t ...

Undefined will be returned if the data in AngularJS is not set within the $scope

I have developed a factory that contains a list of functions which are called when the state changes. On page load, I am calling dtoResource.rc1Step1DTO(). Although the function executes, when I check the result in the console, it returns undefined. Below ...

Guide to centering Embed horizontally with Bootstrap 5

My goal is to create a centralized input with an image positioned above it. I have been following the guidelines provided in the position documentation, but the image still does not align horizontally: https://i.sstatic.net/awWxL.png Here's the code ...

Send the completed form to a designated web address

Here's the form I'm working with: @using (Html.BeginForm("Search", "Home", FormMethod.Get, new { enctype = "multipart/form-data", @class = "navbar-form navbar-left form-inline", @role = "search" })) { var numberOfVi ...

Is there a way to increase the spacing between the titles of these progress bars?

Ensure your responsiveness by enabling the Toggle Device Toolbar on your browser I'm attempting to create a progress bar, but I'm facing some challenges. Firstly, I want to increase the height of the semi-transparent black background, but I can& ...

Elements separated by empty space on a Complex Grid, appearing distant despite the available space

Struggling with creating a complex layout using minimal relative and absolute positioning. Here's the issue I'm facing: All my elements are aligned side by side with only one problem: the border image needs to bleed into the row below. How can t ...