Utilizing CSS Grid to have an element occupy available space

In my CSS grid layout, I have some elements that span multiple rows. I've been trying to adjust the positioning so that the lower elements fit snugly against the ones above them, but they are sticking to the bottom of the grid no matter what I do.

Even setting align-items to start or stretch doesn't resolve the issue.

ul {
  display: grid;
  grid-template-areas:
    "a b"
    "c b"
    "c d";
  grid-template-columns: repeat(2, minmax(140px, 1fr));
  grid-template-rows: repeat(3, auto);
  grid-gap: 10px;
  align-items: start;
}
.a { grid-area: a; }
.b { grid-area: b; height: 5em; }
.c { grid-area: c; height: 10em; }
.d { grid-area: d; }



<ul>
  <li class="a">A</li>
  <li class="b">B</li>
  <li class="c">C</li>
  <li class="d">D</li>
</ul>

When implementing this code, A and C touch as desired, but B and D remain separated unless additional content is added to D. My objective is to have B & D touch each other while maintaining a gap at the bottom (or causing D to fill up the remaining space).

For a visual representation, you can view the Fiddle here: https://jsfiddle.net/JasonTank/Ltkhn6so/14/

Answer №1

To achieve the desired outcome, you can remove the align-items: start; property from the ul and adjust it in another section to create 4 rows.

body {
  padding: 20px;
  font-family: Helvetica;
  background-color: #20262e;
}

ul {
  display: grid;
  grid-template-areas:
    "a b"
    "c b"
    "c d"
    "c d";
  grid-gap: 10px;
}
.a { grid-area: a; }
.b { grid-area: b; }
.c { grid-area: c; height: 10em; }
.d { grid-area: d; }

li {
  background-color: #fff;
  border-radius: 3px;
  padding: 20px;
  font-size: 14px;
}


button {
  background: #f0f;
  color: #000;
  border-color: #00f;
  margin: 0 0 1em;
}
<ul>
  <li class="a">A</li>
  <li class="b">B</li>
  <li class="c">C</li>
  <li class="d">D</li>
</ul>

You can view the updated version of your code on this fiddle.

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

Scroll-activated Image Accordion

Looking to create a unique image accordion feature that responds to page scroll. As you scroll down the page, the accordion should open and close horizontally in sync with your scrolling movement. Once all images are opened, the vertical scrolling of the p ...

Transform a list into two separate columns using only CSS styling

Looking to split a list into 2 columns, displayed vertically rather than horizontally. Currently achieved with the CSS: li { float: left; width: 50%; } However, I need all elements past the 5th child to float right. Something like: li { float: lef ...

Create a bolded section within a TextField component using Material UI version 5

I am working with a TextField component that has a specific defaultValue set. Here is the code snippet: <TextField autoFocus={props.autoFocus} fullWidth defaultValue={props.defaultValue} value={text} onKeyPress={handleKey ...

Soundtrack featured on the main page

Currently, I am trying to incorporate an audio file into my homepage without interrupting its playback when the user navigates to another page or title. Essentially, I want the audio .mp3 file to continue playing in the background seamlessly. I am currentl ...

Content is pushed by a scrolling drop down menu

I had a drop-down menu that was functioning well. However, when I made changes to allow scrolling for too many items, it started behaving differently. The drop-down without scrolling doesn't move down, but the one with scrolling does. JS var maxHei ...

Creating a design utilizing HTML columns with a set height and the ability to scroll horizontally

For my android project, I have a requirement to display a view (WebView) that dynamically loads content. The content will consist of <div class="content-item"> tags that are added by JavaScript to <div class="content-holder"> after the page has ...

The box inside a MUI TextField input is consistently visible

My form utilizes MUI Form within a Dialog, but I am facing an issue where the TextField always displays with the border of a filled variant instead of the standard look. Additionally, when trying to integrate an Input from the NextUi library, I encountered ...

Step by step guide to showcasing images dynamically in user interface

My current project involves displaying a screen with an HTML table and an image. The HTML table is fully dynamic. The Code Working Process When the user loads a page (with a URL), I render an HTML table in different parts as the page loads. I retrieve al ...

What could be preventing my bootstrap class from being applied as expected?

As a newcomer to HTML, CSS, and bootstrap, I am struggling with updating my stylesheet values in the preview. This is the button tag that I am working with: <button class="btn btn-primary btn-xl">Find out More</button> However, when ...

What is the best way to ensure an SVG maintains a fluid width while keeping its height constant?

My goal is to achieve the following: The SVG should dynamically scale its width to occupy 100% of the container's width. The SVG should either stretch or compress when the container's width changes, specifically affecting the wave drawn wit ...

Guide to incorporating a fadeIn effect in Jquery triggered by a change event

Looking for some guidance on implementing a fade-in effect on a <p> tag with relevant CSS after making an AJAX call in response to a change event. Below is the current code snippet: $('#scenarioDropdownList').change(function() { var sc ...

Adjust the text placement following its entry on the screen

I'm currently facing an issue with my code that involves text sliding in and then moving to the left after the slide completes. How can I ensure that the text remains in place once the slide is finished? <style> div.slide-left { width: ...

Implement real-time reporting functionality in Selenium

I have been working on implementing a run-time reporting mechanism for my automated test cases created using Java with Selenium and TestNG. The test results and details are stored in a MySQL database. Although I can successfully insert the test results in ...

Function Executing Prior to Parameters Being Accessible

I am currently developing a tic tac toe game using HTMl, CSS (SCSS), and JS. However, I am facing some challenges. The function I created to add an X or O to each grid space seems to be adding them automatically before having the correct parameters, althou ...

Adjust the background color of the panel heading when it is toggled in a Bootstrap collapsible panel group

I'm currently working on a collapsible panel group design using Bootstrap. I want the panel heading to have a different background color when the panel group is expanded versus when it is collapsed. I initially attempted to use the :active selector, b ...

Enhancing the appearance of select borders

I am trying to add a border to a <select> element, but I want it to overlap the default button border so that it looks more visually appealing. I am looking for a CSS-only solution that can be applied to all <select> elements on my website. If ...

Having trouble incorporating Bootstrap 5's SASS variables into my project's SASS files

I'm attempting to utilize Bootstrap 5 variables in my custom styles.scss file. I have integrated the entire Bootstrap 5 source code into my project. I have noticed that I need to import specific .scss files in my styles.scss file for it to function co ...

Issue with electron/vuejs - uikit modal not taking up full screen height when nested within sub components

Can anyone explain why the modal appears with a dark background that doesn't cover the top and bottom when it's placed within a subsection of the larger code? https://i.sstatic.net/SIOw7.png Modal code Modal.vue <template> <!-- Th ...

Unable to set maximum width for images in Firefox browser

Welcome to my page where I am utilizing the latest version of Bootstrap v3.2.0 I have encountered an issue when using the display: table-cell; CSS property within a div block. Specifically, the max-width: 100%; CSS property does not function as expected f ...

Animate the object in SVG with periodic movements

Illustrate once and move items every five seconds in the sequence shown from left to right. I have looked extensively but cannot find a method to draw outer boundaries and position objects close to them. <animate xlink:href="#blue-rectangle" a ...