Tips for creating adaptive spacing within a grid design

I'm attempting to design a grid layout with responsive, colored spaces that adjust their size based on the width of the parent container. However, I encountered an issue where the vertical gaps do not extend all the way to the bottom of the grid.

To achieve this layout, I defined a grid container with percentage-based gap sizes and assigned background colors to these gaps. You can view the code snippet below:

.container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-gap: 10%;
  background-color: black;
}

.pic {
  padding-bottom: 100%;
  background-size: contain;
  background-repeat: no-repeat;
  background-image: url(https://via.placeholder.com/512);
}
<div class="container">
  <div class="pic"></div>
  <div class="pic"></div>
  <div class="pic"></div>
  <div class="pic"></div>
</div>

The challenge arises when using percentage-based grid gaps without specifying a fixed height. This article on Stack Overflow delves into the reasons behind this behavior but does not provide a definitive solution.

Answer №1

This particular issue of grid gap percentage without height was discussed in detail on a post titled Grid gap percentage without height. The explanation provided there led me to discover the solution outlined below:

  1. Instead of defining grid-gap, specify grid-column-gap and grid-row-gap individually. Ensure that grid-row-gap is set to grid-column-gap / (1 - grid-column-gap).
  2. Adjust the padding-bottom of the container to match the value of grid-column-gap.

For instance: if the grid-column-gap is 10% (0.1), then the corresponding grid-row-gap would be 0.1111 (11.1%).

This method accommodates any percentage assigned to grid-column-gap.

.container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-column-gap: 10%;
  grid-row-gap: 11.1%;
  padding-bottom: 10%;
  background-color: black;
}

.pic {
  padding-bottom: 100%;
  background-size: contain;
  background-repeat: no-repeat;
  background-image: url(https://via.placeholder.com/512);
}
<div class="container">
  <div class="pic"></div>
  <div class="pic"></div>
  <div class="pic"></div>
  <div class="pic"></div>
</div>

This strategy can also be applied to grids with multiple columns. In cases where the number of gaps equals 'n' (resulting in n+1 columns), calculate grid-row-gap as grid-column-gap / (1 - n * grid-column-gap) and adjust padding-bottom to n times the grid-column-gap. Consider the following example with n = 2 (3 columns).

.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-column-gap: 10%;
  grid-row-gap: 12.5%;
  padding-bottom: 20%;
  background-color: black;
}

.pic {
  padding-bottom: 100%;
  background-size: contain;
  background-repeat: no-repeat;
  background-image: url(https://via.placeholder.com/512);
}
<div class="container">
  <div class="pic"></div>
  <div class="pic"></div>
  <div class="pic"></div>
  <div class="pic"></div>
  <div class="pic"></div>
  <div class="pic"></div>
  <div class="pic"></div>
  <div class="pic"></div>
  <div class="pic"></div>
</div>

Answer №2

body{
height: 100%;
}
.container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-gap: 10%;
  background-color: black;
  padding-bottom: 9%;
}

.pic {
  padding-bottom: 100%;
  background-size: contain;
  background-repeat: no-repeat;
  background-image: url(https://via.placeholder.com/512);
}
<div class="container">
  <div class="pic"></div>
  <div class="pic"></div>
  <div class="pic"></div>
  <div class="pic"></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

JavaScript can be used to select and click on a specific li element using the :

I'm attempting to click on a specific link using a submit button. Essentially, I have a ul containing a large number of li elements, each with a link of class "episodeLink" inside, as shown on . I've added an input box where you can enter a num ...

.jquery_materia_chip() function malfunctioning

I'm looking to implement a tagging feature using chips on my website. Below is the HTML code I have so far: <div class="chips chips-autocomplete"> <input type="text" autocomplete="true"> </div> Within the <script> tag, I&apos ...

Creating a slick progress bar using a combination of HTML, CSS, and JavaScript

Looking to create a progress bar similar to the one shown in the image below? https://i.sstatic.net/dwrDp.png ...

Utilizing blend modes to invert colors

I'm working on a code where I need to change the color of certain points when they overlap with a segment, and partially change their color when hovered over by a rectangle. I attempted to achieve this using mix-blend-mode but it didn't work as e ...

Importing JSON data from a URL to display in an HTML table

Looking to utilize the Untappd API for a beer menu project, I've encountered an issue. The goal is to showcase the JSON data received from the server and organize it into an HTML table. Despite successfully importing data from a local list.json file ...

Show information and organize tabs based on a localStorage variable

Recently, I came across a theme that intrigued me: . My goal is to utilize local storage in order to maintain the active tab even when the page is refreshed. However, I've encountered an issue where the site continues to display previous content and t ...

Dynamic Entry: The Art of JQuery Animation

I want to achieve a sequential flying-in effect when my page loads, with each div sliding in one by one. Currently, all the divs fly in simultaneously, but I'd like them to come in sequentially. Here is my code snippet: <script> function ani ...

Having trouble connecting my jQuery file to my HTML page

I am struggling to connect my jquery file to my HTML document. It seems like such a simple task, yet it is causing me quite a headache. Below is my HTML code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html> <head> <meta ...

The background of the section does not extend to the full height

Currently, I am using the <section> tag as a background for a specific section on my website. However, I am facing an issue where the height of the background is being cut off, even though the content continues below it. I have attempted various pos ...

Using jQuery and AJAX to submit a dynamic form

I am currently facing an issue with cloning a HTML drop down list using jQuery. The main problem I am encountering is that there seems to be no restriction on the number of cloned sections, and I need to store all these sections in a mySQL database. How c ...

Accessing User Input Data with JQuery

Can someone help me figure out how to store the input value from a Materialize select form in HTML using a variable in javascript/jquery? Here is the HTML code for the form: <div class="input-field col s12"> <select> <option va ...

Ways to make the Bootstrap form's fields align horizontally

I've exhausted all options, but I just can't seem to get the fields aligned horizontally. <form class="form-horizontal" role="form"> <div class="form-inline"> <div class="form-group"> <label for="acctName ...

What is the best way to display items within a table using React?

I'm just starting to learn React. Can someone show me how to use the "map" function to list elements from two different arrays in two columns? state = { dates: ["2000", "2001", "2002"], cases: ["1", "2", "3"] } render() { return ( <thea ...

In the development environment, assets are concealed from view

In the HEAD block of my code, I have the following: {% stylesheets filter='yui_css' output='css/base.css' '@MutualContratosBundle/Resources/public/css/ui/south-street/jquery-ui.css' '@MutualCo ...

What is the best way to prioritize .py-md-6 over .p-3 in Bootstrap 4?

I have set the padding for the class .py-md-6 as follows: .py-md-6 { padding-top: 6rem !important; padding-bottom: 6rem !important; } Despite this, there seems to be a conflict with the .p-3 class in my HAML element: #id.p-3.py-md-6 It appears th ...

Can the Android Browser Display Seamless IFRAMES?

Did you know that HTML5 offers support for a seamless IFRAME? This feature allows you to easily include headers and footers in your HTML code. However, when I tested this on the Android (2.2) browser, which is supposed to be HTML5-based, it didn't wor ...

Convert an HTML table with a drop-down menu into a CSV file for exporting

My table has the capability for users to add additional columns, with each new column containing a dropdown list option. However, when attempting to export the table as a CSV file, I encountered an issue where the export functionality only works properly o ...

What are some ways to align text both vertically and horizontally within columns?

How can I center text both vertically and horizontally inside two columns using Bootstrap 4? <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css"> <div class="row"> <div class="co ...

Utilizing tags within the pre tag to incorporate additional styles

Using pre tags to display code works well, but I need to style certain parts of the code in green. However, when I wrap the desired code in a <div> tag with styling, it affects formatting by adding newlines and swallowing spacing. How can I achieve t ...

Modify the name of the selected option value

I am working on an html code where I need to replace the values 0, 1, and 2 with USA, Australia, and Canada respectively. I also need to know the name of these values in my MySQL database. Thanks! HTML <form method="post" id="countriesForm" name="cou ...