Using CSS grid to completely fill a 100vh area without impacting the height of the rows

I am working on a grid layout with 3 columns and a variable number of rows. The background of the grid is colored, but when there are not enough rows to fill the vertical space, it remains uncolored:

.grid {
  align-self: flex-start;
  display: grid;
  grid-template-columns: min-content 2fr 1fr;
  grid-gap: 0px;
}
 
.index, .title {
  background: red;
  padding: 0 2vw 0 1vw;
}
 
 .ticker {
  background: yellow;
 }
  
<div class="grid">
  <div class="index">1</div>
  <div class="title">Pigs are cute</div>
  <div class="ticker">blah blah</div>
  
  <div class="index">2</div>
  <div class="title">Horses rise at night</div>
  <div class="ticker">bleh bleh</div>
  
  <div class="index">3</div>
  <div class="title">Cats run in packs</div>
  <div class="ticker">blih blih</div>
</div>

My goal is to have this remaining vertical space also colored like the rest of the grid. I tried setting the height of the grid to 100vh, but that stretches each row's height which is not what I want since I need the row height to adapt to content.

.grid {
  align-self: flex-start;
  display: grid;
  grid-template-columns: min-content 2fr 1fr;
  grid-gap: 0px;
  height: 100vh;
}
 
.index, .title {
  background: red;
  padding: 0 2vw 0 1vw;
}
 
 .ticker {
  background: yellow;
 }
<div class="grid">
  <div class="index">1</div>
  <div class="title">Pigs are cute</div>
  <div class="ticker">blah blah</div>
  
  <div class="index">2</div>
  <div class="title">Horses rise at night</div>
  <div class="ticker">bleh bleh</div>
  
  <div class="index">3</div>
  <div class="title">Cats run in packs</div>
  <div class="ticker">blih blih</div>
</div>

I am looking for a CSS grid solution that expands the grid vertically while keeping row height dynamic. Alternatively, a JavaScript solution adding blank rows until reaching window height would work too.

A flex approach doesn't suit my needs as I require a grid-like layout where columns and rows stay aligned.

Thanks!

Answer №1

Due to the varying colors of different columns in the grid, simply extending the grid is not a viable solution as the grid itself cannot have multiple colors. Therefore, the only practical options are to expand the rows or add new rows.

One of the requirements for the question is to keep the row height unchanged. As indicated by this query, using CSS grid to add an extra row and expand it to fill the remaining space is not possible (only achievable with flexbox). The most effective approach appears to involve adding rows dynamically through JavaScript until the total grid height is equal to or greater than the window's height:

var rowHeight = window.getComputedStyle(document.querySelector('.grid').firstElementChild).height;
var row = "<div class='index' style='height:"+ rowHeight +"'></div><div class='title' style='height:"+ rowHeight +"'></div><div class='ticker' style='height:"+ rowHeight +"'></div>";
while (parseFloat(window.getComputedStyle(document.querySelector('.grid')).height) < window.innerHeight) {
  document.querySelector('.grid').innerHTML += row;
}
html, body {
margin: 0;
padding: 0;
}

.grid {
  align-self: flex-start;
  display: grid;
  grid-template-columns: min-content 2fr 1fr;
  grid-gap: 0px;
}
 
.index, .title {
  background: red;
  padding: 0 2vw 0 1vw;
}
 
.ticker {
  background: yellow;
 }
 
 
<div class="grid">
  <div class="index">1</div>
  <div class="title">Pigs are cute</div>
  <div class="ticker">blah blah</div>
  
  <div class="index">2</div>
  <div class="title">Horses rise at night</div>
  <div class="ticker">bleh bleh</div>
  
  <div class="index">3</div>
  <div class="title">Cats run in packs</div>
  <div class="ticker">blih blih</div>
</div>

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

`Look up values from specified key names`

Consider the following JSON data: const information = { "item1":1, "item2":20, "item3":123, "item4":[{"a":"apple","b":"ball"}], "item5":1211 } In ...

transform two series of data into a single object - JavaScript

I'm struggling to merge two arrays into a single array object. Here is the first array, referred to as "keys". Each item in this array should become an object key: ["name", "age", "gender", "status"] The second array contains values and is named "h ...

Toggle the checkbox to update the count

I am trying to implement a feature where the user can check a maximum of 4 checkboxes. I have managed to achieve this functionality in the code below. When the user unchecks a box, the count should decrease by one and they should be able to check another c ...

Implement a horizontal scrollbar for your table in HTML

I need to implement a horizontal scroll bar for my table. Here is the HTML code: Even though I have utilized Bootstrap, the horizontal scroll bar is not working for this particular module. It works fine in other modules. The tbody data is added after an ...

Unable to locate the request in Angular's HTTP testing

Facing an issue with my Jasmine test that involves mocking a HTTP request. The error I'm encountering is: Failed: Expected one matching request for criteria "Match URL: https://myurl.net/api/v2/OperationalAreas/1/Equipments?PageNumber=1&PageSize= ...

Display the selected item from the array while concealing the rest using React

I'm creating a Q/A page where questions are initially visible and answers are hidden. When a question is clicked, the corresponding answer should be displayed. I have an array of objects that I map through in JSX to display the questions. However, wh ...

When using JS, the open and close buttons function properly, however, the escape key always attempts to close the most recent modal

My buttons are working fine, but I'm having trouble getting the escape key to work with the correct modal. It keeps closing the last modal no matter how many I have. Even though thisModal seems to point to the right one for the buttons, it doesn&apos ...

Using EffectComposer in conjunction with the alpha channel in three.js

Here's the code I'm working with: renderTargetParametersRGBA = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBAFormat,stencilBuffer: true }; colorTarget = new THREE.WebGLRenderTarget( SCALE * SCREEN_WIDTH, ...

Optimal methods for handling Ajax requests in the present day

Recently, I revisited some websites I co-built with a friend and was working on getting them functional again. It's been a while since I've done any AJAX work, and I'm realizing that there aren't many resources available to help trouble ...

Is there a way I can create a conditional statement to determine the values of my selection?

I need to customize the order status values for 'confirmed', 'on the way' and 'delivered'. How can I map these statuses to specific numerical values based on the options available in a select menu? Confirmed - 10 On the way - ...

Ensure that the search input field is in focus whenever the showSearch state is true

Having difficulty focusing on the input field whenever the showSearch state is true. Utilizing useRef and useEffect for this purpose. When the showSearch flag changes, the useEffect hook is triggered to check if showSearch is true, and if so, it should foc ...

getStaticProps only runs on IIS after the entire page is refreshed

Using the nextjs getStaticProps feature in my project has been smooth sailing so far. However, after uploading the Static files to IIS, the feature seemed to stop working until I configured a urlRewrite module on it. I noticed that when initially visiting ...

Custom Component in React Bootstrap with Overflowing Column

I am working on a custom toggle dropdown feature in my React application: import React from 'react'; import 'react-datepicker/dist/react-datepicker.css'; const DateRange = props => ( <div className="dropdown artesianDropdo ...

What is the most effective method for populating an array with all images from a particular directory?

I recently installed a pdf viewer plugin on my website (check it out here: ), and I have a question regarding its functionality. I am looking to include approximately 60 - 70 pages in this flip book, but I am unsure of how to proceed. I have attempted var ...

Executing the calling statement once all function statements have been completed

I am currently working on a lengthy function that involves retrieving data from multiple levels of a database. getResult() { this.appService .getCountries() .subscribe( (countries: Country[]) => { this.countries = co ...

How come the dark grey in CSS appears to be lighter than the standard grey hue?

JSBIN DEMO Could this be a mistake or is it a bug? Shouldn't the dark gray shade be darker than the default grey one? HTML <div class="lightgrey">Light Grey</div> <div class="grey">Grey</div> <div class="darkgrey">Dar ...

Can Facebox's settings be adjusted during runtime? If so, how can this be done?

Can Facebox settings be accessed and customized? For instance, I am interested in setting the location of the loading image dynamically as shown on line 4: <script type="text/javascript" src="<?php echo base_url(); ?>media/facebox/facebox.js" > ...

CSS: When using the float: right property, the container does not grow to

I'm having trouble with expanding the comment body that is floated to the right, as my container doesn't expand along with it. Any tips on how I can resolve this issue? For a clearer understanding, please refer to this link: http://jsfiddle.net/ ...

Assign a function in one class to be equivalent to a function in a different class

What is causing this issue and how should it be resolved? class A { constructor() { console.log('constructin A') } public someMethod = (x: string) => { console.log(x) } } class B { private myA: A constructor ...

Error encountered with the NextGen (Mirth) Connect and MongoDB Java driver connection

I'm currently in the process of setting up Mirth Connect Server 3.10.1 (Java version: 1.8.0_181) to write FHIR JSON documents to MongoDB. Following the instructions provided in this post, I have included the following drivers in my custom-lib/ directo ...