The gap in rows is not displayed in CSS subgrid

Currently, I am delving into the world of CSS grid and subgrid. My current project involves creating a simple form with two columns and incorporating a few subgrids.

In my design, the parent grid displays a gap of 1em - both the columns and rows have a proper 1em gap. However, when it comes to the subgrid, only the column gap is visible while the row gap seems to be non-existent, set at 0. I'm unable to pinpoint the root cause of this issue...

I have found a workaround by assigning the subgrid a gap-row of 1em, which does seem to work. Nevertheless, I would prefer to tackle the actual problem head-on in order to understand what exactly triggers this puzzling situation in the first place.

Here's a snippet of the HTML and CSS:

#gridContainer {
  display: grid;
  grid-template-columns: 1fr 3fr;
  gap: 1em;
  padding: 1em;
  justify-items: left;
}

.subgrid {
  display: grid;
  grid-template-columns: subgrid;
  grid-column: span 2;
  justify-items: left;
}
<div id="gridContainer">
  <div class="subgrid">
    <label for="nameCompanyDebt1">Naam onderneming:</label>
    <input type="text" id="nameCompanyDebt1" name="nameCompanyDebt1">
    <label for="companyDebt1">Zakelijke schuld</label>
    <input type="text" id="companyDebt1" name="companyDebt1">
  </div>
</div>

It has come to my attention that #gridContainer (the parent) lacks grid-template-rows. Therefore, there are no 'rows' to inherit. However, considering that gap: 1em should apply to both columns and rows, this shouldn't pose an issue, right?

I did experiment by adding grid-template-rows to #gridContainer, but this merely led to overlapping/stacking rows.

I also acknowledge the distinction between the parent (#subGrid as an ID) and the subgrid (.subgrid as a class). Surprisingly, the columns function seamlessly despite this difference in naming conventions.

Answer №1

You're just subgriding the columns, which means the entire subgrid fits within the first row of the grid. This is why there appears to be no gap displayed. To rectify this issue, make sure to also subgrid the rows.

Ensure that you set grid-row to span the same number of rows as in the subgrid.

#gridContainer {
  display: grid;
  grid-template-columns: 1fr 3fr;
  gap: 1em;
  padding: 1em;
  justify-items: left;
}

.subgrid {
  display: grid;
  grid-template-columns: subgrid;
  grid-template-rows: subgrid;
  grid-column: span 2;
  grid-row: span 2;
  justify-items: left;
}
<div id="gridContainer">  
  <div class="subgrid">
    <label for="nameCompanyDebt1">Naam onderneming:</label>
    <input type="text" id="nameCompanyDebt1" name="nameCompanyDebt1">
    <label for="companyDebt1">Zakelijke schuld</label>
    <input type="text" id="companyDebt1" name="companyDebt1">
  </div>
</div>

Answer №2

It finally clicked for me today! The answer to my dilemma wasn't utilizing a div with display: subgrid, but rather a div using diplay: contents!

By doing this, all the elements within act as direct children of the main grid, while still allowing me to hide the entire div (along with its children) using its specific ID.

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

Mastering the Art of Utilizing $(this) in jQuery

$(".item_listing").hover(function(){ $(".overlay_items").display(); },function(){ $(".overlay_items").hide(); } ); This is my jQuery code. I am trying to display the "overlay_items" div when the "item_listing" div is hovered ov ...

Optimizing Bootstrap for Tablet Browsing in Internet Explorer

Hey there, I have a question regarding Internet Explorer and tablets. As I revamp this website and integrate Bootstrap into it, my focus is currently on the small-sized IE browser for tablets. My dilemma arises from not knowing if there are any tablets th ...

HTML: A peculiar table setup with an image displayed on the right side and text aligned to the left side. Strangely, the

Just starting out and seeking assistance <table style="border:none" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="border:none" valign="top" vertical-align:"top"; width="20%"> <div><img class= ...

Icon button with label positioned underneath - utilizing Font Awesome icons

I am looking to design a button that features a Font Awesome icon with text below it for a navigation menu. The goal is to achieve a layout similar to the icons found in the Microsoft ribbon. Additionally, I want to include an arrow below the button (simi ...

Modify the border color of a div contained within another div

I recently incorporated a Vue component (available at ): <vue-editor id="description" name="description" :class="{'uk-form-danger': errors.has('description') }" v-model="description" :editorToolbar="customToolbar" v-validate="' ...

What is the best way to implement an AppBar that fades in and out when scrolling within a div?

I'm trying to implement a Scrollable AppBar that hides on scroll down and reappears when scrolling up. Check out this image for reference To achieve this functionality, I am following the guidelines provided by Material-UI documentation export defa ...

The Bootstrap model popup darkens the screen, but there are a few areas that remain at the same level of brightness and do not get dim

https://i.sstatic.net/09QtK.png Within my Angular 7 project, I have implemented a Bootstrap modal. However, I am facing an issue where only components with position: relative are darkened when the modal is opened. Some components have position: fixed, as ...

For a while now, I've been attempting to transform my paragraph into a block within a section that showcases elements in an inline-flex layout

I have been attempting to adjust the paragraph with the errorDate class so that it displays as a block element directly beneath the input element similar to the image provided here. The section is currently set to appear as an inline-flex. Below is the CS ...

Challenges with formatting the <legend> tag and grid setup in bootstrap 4

My intention was for it to be displayed like this: But unfortunately, it is appearing like this instead: You can see that the word "fruit" is misaligned. I am currently using Bootstrap 4 alpha v6. Here is the code snippet: <fieldset class="form- ...

Javascript - issue with accurately retrieving element offset value while scrolling

My goal is to dynamically change classes for elements based on their position during scrolling. I am trying to calculate the top offset element value and adjust the classes accordingly. Here is the function I am using: handleScroll () { const header = ...

Unique Text: "Personalized marker/pin for interactive map feature"

Looking to create a custom image map marker/pin with a unique bottom design resembling a union shape using CSS and Vue.js. I've attempted it myself but haven't been able to achieve the exact look as shown in the reference image. Any advice or ass ...

Make a div come alive with animation when hovered over

I'm trying to achieve a rotating effect on a div when the cursor hovers over it, similar to what is shown in this video. My preference is to utilize keyframes for this animation as they offer more customization options. div.myElement { ... a ...

How can I specify the exact width for Bootstrap 3 Progress Bars?

I have a single page that displays all my files in a table format, and I also have the count of open and closed files. My goal is to represent the percentage of closed files using a progress bar. The width of the progress bar should change based on this p ...

how to style a page with a CSS arrow shape

Can someone help me figure out how to create an arrow similar to the one in this button using CSS? https://i.sstatic.net/gQi0l.png I've been able to create triangle-like arrows like the one below #triangle_arrow { top: 3pt; content: ...

Struggling with connecting relative hyperlinks

When constructing a website, I have opted to use relative hyperlinks. The file structure is organized as follows: /root/website1/ All my code resides within the website1 folder. Within website1, the structure looks like this: /css style.css /static ...

Adjusting the transparency of the background color seamlessly without the need for an image

After creating a div element with a green background color, I am looking to progressively change the opacity of the color from top (darkest green) to bottom (lightest, white). Is there a CSS-based way to achieve this effect without resorting to using an ...

What steps can I take to create a slideshow with dynamic animations?

I am trying to create a unique category display by turning it into a slider. When the user clicks on any category, I want it to move to the left and become larger. To see an example of how the sliding effect works, check out this link: slideshow https://i ...

I need help getting rid of the unwanted text and objects that are appearing on my website

I recently designed a basic website and everything was running smoothly until I decided to insert a new ul division in the HTML page as a spacer between two elements: <div> <ul> <li><a></a></li> </u ...

How to align text to the left and image to the right using CSS

I'm fairly new to CSS and struggling with a design issue. I want to display a series of stacked divs on my page, each containing text and one or more images. My goal is to have the text left-aligned and vertically centered, while the images are right ...

Getting the value from a dropdown menu and displaying it in a text area

I have 3 functions (radio, textarea, and dropdown) I was able to successfully retrieve the values of radio and textarea into the textarea. However, the drop down functionality is not working as expected. My goal is to display the selected option from the ...