A grid with a set number of lines and rows that may overflow

My grid has 3 columns and 6 rows, with dynamic elements that can vary in size. I want the grid to adjust its height based on the screen when all elements are within the defined columns and rows. If an element does not fit within the grid, I would like it to be positioned beneath the other elements without affecting their layout. Can this be achieved using CSS grid?

In this example, item4 and item5 should appear under item2.

.container {
  display: grid;
  grid-template-rows: repeat(6, minmax(0, 1fr));
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.container > div {
  border: 1px solid black;
}

.item1, .item2 {
  grid-row: span 3 / span 3;
  grid-column: span 2 / span 2;
}

.item3, .item4, .item5 {
  grid-row: span 2 / span 2;
}
<div class="container">
  <div class="item1">1</div>
  <div class="item2">2</div>
  <div class="item3">3</div>
  <div class="item4">4</div>
  <div class="item5">5</div>
</div>

Answer №1

I couldn't quite grasp the intention behind your current CSS layout as it didn't seem to fully utilize the web page space effectively. Here are some suggestions you might want to consider:

SOLUTION

  1. Ensure that both your <html> and <body> tags occupy the entire height of the viewport using CSS to allow your grid to fill the entire page (refer to the HTML snippet below for guidance).
  2. Make sure that elements within the body, such as your grid <div>, also use CSS to take up the full height of the viewport. It's recommended to set the <body> tag as the parent for the grid, ensuring complete page width and height.
  3. When working with grids, decide whether rows or columns should dictate how the page is filled. Specify this logic in your code to avoid any ambiguity for the browser. In the provided example, I used rows to ensure vertical filling by setting auto-fit, which removes excess space while maintaining full window coverage.
  4. If you mentioned using 6 rows but the design doesn't reflect that, consider allowing for dynamic row creation based on the number of child cells added. This approach ensures all cells share the vertical space evenly, adapting to different amounts of content.
  5. The demonstrated layout accommodates an unlimited number of cells, dynamically adjusting to fit the entire browser viewport space.

This design will fluidly adapt to varying viewport sizes, either stacking vertically by rows like in the initial example or horizontally by columns, depending on your requirements.

All examples showcase a responsive design that fills the entire browser viewport proportionally.

<!doctype html>
<html style="width:100%;height:100%;">
<head>
</head>
<body style="margin:0;padding:0;height:100%;">

<style type="text/css">

.container {
    min-width:100%;
    min-height:100%;
    padding: 0;
    margin: 0;
    display: grid;
    grid-template-rows: auto-fit;
    grid-template-columns: repeat(3,1fr);
}

.container div[class^=item] {
    grid-column: 1 / 4;
    border: 1px solid black;
    background:green;
}

</style>
 
 <div class="container">
  <div class="item1">1</div>
  <div class="item2">2</div>
  <div class="item3">3</div>
  <div class="item4">4</div>
  <div class="item5">5</div>
</div>
<!-- I added this cell to show how items not part of the grid CSS stack under the full viewport grid. -->
<div>6</div>

To align the cells horizontally within the available columns while permitting rows to expand as new cells are added to the viewport, remove the "grid-column" property from the CSS block as outlined below. This adjustment allows the blocks to stack horizontally within the space, potentially achieving the desired outcome more effectively.

.container div[class^=item] {
    border: 1px solid black;
    background:green;
}

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

Why is the medium-offset-2 value not being implemented?

Currently utilizing Zurb's Foundation for the construction of my website, where I have integrated two action buttons within a callout section. My objective is to create space between the two buttons up to tablet-sized screens by applying the medium-o ...

Sequence of background colors not altering as intended

After clicking a button, I included the following jQuery code in a code snippet on my WordPress site: jQuery("#cf_course_planner tr").css("background-color", "#f66"); jQuery("#cf_course_planner tr:eq(0)").css(& ...

Tips for assigning a class to a div based on the route in Angular

In my angular template, I have a basic ng-repeat with some div elements. Now, I am looking to add a class to a specific div if the $routeParams.userId matches the id of the object in the loop. You can refer to the second line of code below for clarificatio ...

Angular mobile navbar experiencing collapse issue

I am working on an Angular project that does not include Jquery. I am trying to implement a navbar using mdbootstrap, but I am encountering issues with the collapse feature not working properly. Below is the HTML content I am using: <header> < ...

Tips for adding line breaks in TypeScript

I'm brand new to angular and I've been working on a random quote generator. After following a tutorial, everything seemed to be going smoothly until I wanted to add a line break between the quote and the author. Here's what I currently have ...

Extensive content within the v-autocomplete field

I am trying to use the v-autocomplete component to display the entire text of the selected item. On initial selection, everything displays correctly: https://i.sstatic.net/LFZ2c.png However, once it is chosen, only a portion of the text is shown, leavin ...

Resetting CSS attributes in Kendo UI Mobile when transitioning between views after removing them with jQuery

Dealing with the flicker of unstyled content in my kendo mobile web app has been a challenge, but I found a solution by using a specific CSS rule within my stylesheet: [data-role="content"] { visibility: hidden; } This effectively hides all ...

Error encountered in Selenium Webdriver: Element is not currently in view

I encountered an issue when trying to click on a button using the same type of code that worked fine for selecting a drop down. The error message I received was "Element is not currently visible and so may not be interacted with". Command duration or timeo ...

Can a div be relocated within another div based on a random roll of the dice?

Hey there, I'm currently working on creating a simple Monopoly-style game. As someone who is pretty new to JavaScript, I'm looking to figure out how to move the game piece around the board based on dice rolls. Any guidance or assistance would be ...

Creating an Efficient Lazy Loading HTML Page with jQuery

I've been looking to create a page that loads content only as the user scrolls to that specific section, similar to the way Pinterest website functions. Does anyone have suggestions on how to design and implement this feature, or know of any plugins ...

Attempting to align two div blocks side by side to occupy the entire page

http://jsfiddle.net/UeLMA/1/ Here's the HTML code snippet: <div id="left" style="background: red; display: inline-block; float: left"> aaaa<br /> aaaaa </div> <div id="right" style="background: yellow; float: left">&l ...

The outcome in my EJS file is not as expected when employing a for loop

I am currently developing a task management app, following along with my instructor's guidance. While my instructor's code is functioning properly, I am encountering an issue with my for loop. The only difference seems to be the variable names us ...

What is the best way to clearRect in a JavaScript canvas?

Feeling a bit like Homer Simpson here, I'm attempting to clear the canvas with each iteration to display a rotating square, but for some reason clearRect isn't doing its job. Any insights into what might be going wrong? var canvas = document. ...

Make sure to inherit the width from the parent element

I am facing an issue with my main menu #navi, where the items have different widths but the submenus should match the width of their parent items. To illustrate this problem, I have created a JsBin: http://jsbin.com/yusunohage/1 The text A very very very ...

Prevent HTML element from moving when the window is minimized

I am a beginner in HTML and I need help fixing the positioning of my elements on the window. Whenever I try to minimize the window, everything gets messed up. I am working on creating a website for the first time, so please excuse any mistakes. Here is the ...

Issue with floating the navbar to the right in Bootstrap 4

I am trying to reposition the navbar (without logo) to the right side using code. To achieve this, I included the class "float-right" like so <div class="collapse navbar-collapse float-right" id="navbarSupportedContent">. However, this modification ...

Issue with jQuery: Changing colors does not work on Chrome and Safarirowsers

I'm puzzled as to why the code snippet below only works in Firefox, but not in Chrome and Safari. Can you shed some light on this issue? if ($(this).css("color") == "Fuchsia"){ $(this).css("color","#000000"); } Here is the link in question: If you ...

Menu Items at the Center

I am currently working on a Wordpress website and struggling to center the menu items that are currently aligned to the left. I have created a child theme from the existing theme's code, but still can't figure out how to achieve this simple task. ...

Changes made to the CSS are not being reflected on the live production server hosted on our Bitbucket repository

My WordPress website is hosted on a Linux server and connected to a Bitbucket repository, but I am experiencing an issue where CSS changes are not reflecting on the live site. I have made changes to the HTML code and style sheet, and while the HTML code u ...

Certain Bootstrap 5 icons are failing to appear on the screen

I'm encountering an issue with Bootstrap 5 where certain icons are not displaying correctly. The bi-search icon shows up perfectly, but the bi-send icon is not being shown at all. I would usually include this in a code snippet, but it seems that featu ...