The grid is experiencing improper sizing in the row orientation

Challenges with Grid Layout

In my current project, I am facing a challenge in creating a responsive layout with a dynamic grid within a page structure that includes a header and footer. The main issue arises when the grid items, designed to resemble books with specific dimensions, are appended to the grid container. Initially, the goal is for the <main> section to expand to fill the available space on the page to prevent the footer from floating halfway up the screen. However, as the number of grid items increases, the page should vertically grow and allow scrolling.

Defining "Book Shape"

When referring to a "book shaped" grid item, it means that each column should occupy two-thirds of the total height allocated for the specific grid item.

Troubleshooting Issues

The primary problem lies in achieving consistent sizing for both columns and rows within the grid. While the columns display correctly, the subsequent rows fail to maintain the desired equal-height appearance. This results in the grid items losing their intended book-like shape.

Attempted Solutions

To address this issue, various strategies were tested, including adding flex: 1 to the <main> element to enable flexible growth. Additionally, using

grid-template-rows: repeat(auto-fill, minmax(300px, 1fr));
aimed at automatically filling the grid with uniform row sizes.

An alternative approach involved setting fixed dimensions for both columns and rows:

grid-template-columns: repeat(auto-fill, 200px);
grid-template-rows: repeat(auto-fill, 300px);

Despite these efforts and additional research on CSS functions like repeat() and auto-fill, the issue persisted. Various suggestions from sources like Stack Overflow were explored, such as adjusting flex-grow and flex-basis, but no definitive solution was found.

Closest Resolution

A breakthrough came when defining rows explicitly rather than relying on auto-fill, leading to successful implementation of evenly sized rows.

grid-template-rows: repeat(12, minmax(250px, 1fr));

However, this workaround entails modifying the .grid style via JavaScript during child element additions. Yet, this method presents challenges if there's a change in page width affecting column count, necessitating constant adjustment of row calculations via JavaScript. Hence, an ideal solution involving solely CSS is sought after.

body {
    display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 100vh;
    margin: 0;
    padding: 0;
}

header, footer {
  height: 40px;
}

main {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    background-color: grey;
    width: 100%;
}

.grid {
    display: grid;
    width: 60vw;
    margin: 5px var(--margin-size) 5px var(--margin-size);
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    grid-template-rows: repeat(auto-fill, minmax(300px, 1fr));
    gap: 10px;
}

.grid-item {
  background-color: red;
}
<body>
  <header>
    Header
  </header>
  <main>
    <div class="grid">
      <div class="grid-item">
        Grid Item
      </div>
      <div class="grid-item">
        Grid Item
      </div>
      <div class="grid-item">
        Grid Item
      </div>
      <div class="grid-item">
        Grid Item
      </div>
      <div class="grid-item">
        Grid Item
      </div>
      <div class="grid-item">
        Grid Item
      </div>
      <div class="grid-item">
        Grid Item
      </div>
      <div class="grid-item">
        Grid Item
      </div>
      <div class="grid-item">
        Grid Item
      </div>
      <div class="grid-item">
        Grid Item
      </div>
      <div class="grid-item">
        Grid Item
      </div>
      <div class="grid-item">
        Grid Item
      </div>
    </div>
  </main>
  <footer>
    Footer
  </footer>
<body>

The main inquiry revolves around finding a CSS-based solution to enable the grid to surpass the screen boundaries while preserving the unique book-like appearance of its child elements.

Answer №1

After some troubleshooting, I managed to achieve the desired outcome using the following CSS:

grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
grid-auto-rows: minmax(300px, 1fr);

Below is a snippet demonstrating the working solution:

body {
    display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 100vh;
    margin: 0;
    padding: 0;
}

header, footer {
  height: 40px;
}

main {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    background-color: grey;
    width: 100%;
}

.grid {
    display: grid;
    width: 60vw;
    margin: 5px var(--margin-size) 5px var(--margin-size);
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    grid-auto-rows: minmax(300px, 1fr);
    gap: 10px;
}

.grid-item {
  background-color: red;
}
<body>
  <header>
    Header
  </header>
  <main>
    <div class="grid">
      <div class="grid-item">
        Grid Item
      </div>
   <!-- More grid items -->
    </div>
  </main>
  <footer>
    Footer
  </footer>
<body>

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

Unable to adjust font weight as intended

I'm encountering an issue with getting the lighter font weights to display correctly. Despite having all the necessary font weights installed on my computer, it seems that many fonts do not support the lighter options. What could be causing this incon ...

Excessive Blank Space Issue on Mobile Devices with MDBootstrap

I have been working on creating a replica of Watch2Gether, and it looks great on medium and large screens. However, when viewed on a phone, everything seems perfect until you scroll to the left and notice a large white space on the right side. I've ex ...

Transitioning Menus with Submenu Animation

After following a CSS menu tutorial and customizing it, two issues have arisen. When hovering over the "About" menu with additional list links, the transition works but then the content shifts to the left and fades out. The second issue is related to tryin ...

Creating a dynamic background color that pulses with animation

I recently created a script to animate the menu li element when hovering over the corresponding a element. Everything is functioning as expected, but now I'm looking to enhance the effect by having it continue as long as the mouse remains over the a e ...

Adjusting the font size in Bootstrap grid rows to include an offset

I am currently working on improving site navigation using the Bootstrap Grid system. My goal is to create a layout with 3 columns and two rows structured as follows: |Title | |Login| |Subtitle|Menu buttons| | Everything seems to be functi ...

Use CSS to activate a sibling div when hovering over a button within the same div that contains the button

Is it possible to change the color of div #panel when hovering over the button #button inside div #left? #left #button:hover~#panel { background-color: blue; } #panel { position: absolute; float: r ...

Padding problem arises when you wrap an image with an anchor tag

I'm having an issue with a div containing an anchor-wrapped image, as there seems to be extra padding at the bottom of the div. How can I remove this? HTML: <div id="lineup"> <div class="line-up-click"> ...

In the production mode, Nuxt styles may not be properly applied

After working on this project for about four months, everything seems fine in the development version. However, once I build the project and upload it to the server, some of my styles are not applied. For more information, I have imported styles both in c ...

Preventing multiple clicks by toggling the HTML tag on and off

Here is the jQuery structure that I am currently working with: $(document).on('click', '.view-details', function () { $(this).prop("disabled", true); // API call1 // API call2 // API call3 $(this).prop("disabled" ...

When you reach a scrolling distance of over 300 vertical heights,

Is it possible to show and hide a class based on viewport height? I am familiar with displaying and hiding a class after a specified pixel height, but I'm wondering if it's achievable using viewport height instead? Specifically 3 times the viewp ...

Addressing Image Issues in CSS Grid

I'm struggling to position two images in different sections of a grid layout without overlapping them. I've attempted referencing the images by both their class and id, but they continue to occupy the same grid space. Referencing the images by ...

Material UI Card shadow effect getting cropped

Currently experiencing an uncommon issue while using Material UI's Card component - the box-shadow is cut off on the top and bottom sides. Any suggestions on how to resolve this problem? Check out my code below: import React, { Component } from & ...

Unable to match the height of the inner card image in bootstrap with the height of the outer card image

I'm facing an issue with two bootstrap cards, one nested inside the other, both containing images. Check out the StackBlitz here The inner image doesn't flex to the same height as the outer one, resulting in a small space between the two cards ...

Convert the HTML content into a PDF while retaining the CSS styles using either JavaScript or Django

Looking to create a PDF of an HTML element with background color and images. Seeking a solution, either client-side or server-side using Django/Python. Tried jsPDF on the client side, but it does not support CSS. Considering ReportLab for Django, but uns ...

What is the source of the width for this expansive dropdown menu?

I can't seem to crack this puzzle. When you navigate to the following link: click here, and select 'Accordian' from the topbar menu, where exactly does the dropdown menu width originate from? Upon inspecting it, I only see a computed value w ...

Modifying the label focus does not alter the CSS style

I'm struggling with a simple form where I want to highlight the focused labels by changing their background colors. However, the jquery code doesn't seem to be working as expected. Despite no errors showing up in the console, the functionality is ...

Conceal the initial modal beneath the overlay when the second modal is activated

I have a situation where I am using a modal that contains a button to trigger a second modal. The issue is that the overlay of the second modal does not hide the first modal, it simply darkens the background. How can I make the overlay of the second modal ...

Incorporating a hamburger symbol into an established menu for easy navigation

I have come across a tutorial on creating a navigation menu with a logo positioned to the left. However, I now wish to incorporate a hamburger icon for mobile devices and I am unsure about the process. Despite my efforts to find a suitable tutorial onlin ...

The radio button's checked attribute fails to function

I am currently working on adding a hover effect to radio button labels, and while it is functioning correctly, I would like to achieve the following: When a radio button is selected, I want the corresponding label to have a background color. Despite tryi ...

Add a flexible element to a container without disrupting the alignment of the other

I'm facing a challenge in adding an action button to the end of a grid layout without affecting the centering of the items. The button should seamlessly fit into the grid, just slightly offset. If you check out my demo, you'll see what I mean. ...