Stop CSS Grid cell from causing vertical expansion of cells in adjacent columns

I believe that visual aids are more effective than text in illustrating the issue:

My Objective

https://i.sstatic.net/vBAsI.png

Current Situation

Looking at the example, you can see that the title and description expand equally to accommodate the gallery. I aim to achieve a layout similar to the first image, where the gallery extends past the container and generates a scrollbar, allowing for minimal content in the left column.

https://i.sstatic.net/BCI1M.png

* {
  margin: 0;
}

.container {
  background-color: aqua;
  display: grid;
  padding: 1rem;
  grid-template-columns: 1fr min-content; /* Avoid setting a fixed width for min-content */
  grid-template-rows: repeat(2, min-content);
  grid-template-areas:
      'title gallery'
      'description gallery';
  grid-gap: 1rem;
}

.gallery {
  grid-area: gallery;
  display: grid;
  grid-template-columns: 8rem 8rem;
  grid-auto-rows: 8rem;
  grid-gap: 1rem;
  overflow-y: auto; /* How do I have this overflow, depending on the cumulative min-content of the left column */
}

.gallery > * {
  background-color: magenta;
}

h2 {
  grid-area: title;
  background-color: white;
}

p {
  grid-area: description;
  background-color: lightgrey;
}
<div class="container">
  <div class="gallery">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
  </div>
  <h2>Some Title</h2>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>

JSFiddle

A Temporary Fix

To temporarily address the issue, I applied absolute positioning which necessitates knowing the width of the gallery in advance.

* {
  margin: 0;
}

.container {
  background-color: aqua;
  display: grid;
  padding: 1rem;
  grid-template-columns: 1fr 18rem; /* Reserve space for the gallery */
  grid-template-rows: repeat(2, min-content);
  grid-template-areas:
      'title gallery'
      'description gallery';
  grid-gap: 1rem;
  position: relative /* Create a context for absolute positioning */
}

.gallery {
  grid-area: gallery;
  display: grid;
  grid-template-columns: 8rem 8rem;
  grid-auto-rows: 8rem;
  grid-gap: 1rem;
  overflow-y: auto;
  position: absolute; /* Remove gallery from the flow */
  top: 0; /* Align with the container's boundaries */
  right: 0;
}

.gallery > * {
  background-color: magenta;
}

h2 {
  grid-area: title;
  background-color: white;
}

p {
  grid-area: description;
  background-color: lightgrey;
}
<div class="container">
  <div class="gallery">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
  </div>
  <h2>Some Title</h2>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>

JS Fiddle

Is there a way to achieve the desired layout without specifying a fixed width for the gallery? (And without using JavaScript). I haven't explored the use of flexbox yet, as my primary objective is to minimize the number of wrappers for optimal responsiveness. If achieving this with CSS grid alone is not feasible, I am open to reverting to flexbox.

Answer №1

One option could involve adjusting the height and min-height properties, along with adding right padding to prevent a horizontal scrollbar, or using overflow-x:hidden.

* {
  margin: 0;
}

.container {
  background-color: aquamarine;
  display: grid;
  padding: 1rem;
  padding-right: 0.2rem;
  grid-template-columns: 1fr min-content;
  /* Avoid specifying a fixed width for min-content here */
  grid-template-rows: repeat(2, min-content);
  grid-template-areas: 'title gallery' 'description gallery';
  grid-gap: 1rem;
}

.gallery {
  grid-area: gallery;
  display: grid;
  grid-template-columns: 8rem 8rem;
  grid-auto-rows: 8rem;
  grid-gap: 1rem;
  overflow-y: auto;
  /* Managing overflow based on cumulative min-content of the left column */
  height: 0;
  min-height: 100%;
  box-sizing: content-box; /* Add if necessary for other styles */
  padding-right: 1.2rem; /* or use overflow-x:hidden; */
}

.gallery>* {
  background-color: fuchsia;
}

h2 {
  grid-area: title;
  background-color: ivory;
}

p {
  grid-area: description;
  background-color: gainsboro;
}
<div class="container">
  <div class="gallery">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
  </div>
  <h2>Some Title</h2>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>

height:0;min-height:100% for .gallery is crucial, and the handling of padding and scrollbar is at your discretion.

Answer №2

Setting grid-auto-rows: min(8rem, 100%); on the gallery element will likely solve the issue.

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

Find the maximum width of an element within a Div that is larger than the widths of the other three elements using jQuery

After implementing a Dialog using JqueryUI, I noticed that inside the dialog there is a <div>. Within this <div>, there are three <button> elements labeled "Delete", "Cancel", and "Ok". Interestingly, the widths of these elements are auto ...

The challenge of using CSS Flexbox to position the logo with a margin-top

While learning flexbox, I encountered a challenge. I aim to center align h1, p, and button elements, with the logo positioned at the top margin-top of 1em. Essentially, I want the h1, p, and button to be centered, while the logo is positioned at the top wi ...

Navigating a table while keeping headers in place at the top

Trying to construct a table where the thead remains fixed while the tbody scrolls. Utilizing percentages and fixed width for cell size determination, aiming for uniformity and alignment between percentage td's and thead headers. Referenced JSFiddle d ...

The utilization of CSS variables within intricate properties

Imagine having a CSS property called box-shadow: box-shadow: 5px 5px 0 #ccc; What if there is a need to create a variable from the color #ccc? While in SCSS, you could accomplish this with code like: $grey: #ccc; .element { box-shadow: 5px 5px 0 $gre ...

Linking an element's class to the focus of another element in Angular

In my Angular application, I have multiple rows of elements that are wrapped with the myelement directive (which is a wrapper for the input tag). To highlight or focus on one of these elements at a time, I apply the .selected class in the styles. Everythi ...

Tips for incorporating a sticky toast notification in the ready state of a CI web application

Can you help me with adding sticky toast notifications to my CodeIgniter site's home page? I've tried a few things but it doesn't seem to be working correctly. <div> <p> <span class="description">To show a success ...

Is there a way to temporarily halt a CSS animation when it reaches the final frame

Can anyone assist me with getting the "-webkit-animation-fill-mode: forwards;" to work? It seems like it's getting stuck on the first frame, and I can't figure out why. JS FIDDLE <div class="logo"></div> .logo { width: 328px; ...

Using CSS grid can allow for paragraphs to overlap on top of each

https://i.sstatic.net/LVsgQ.jpgCurrently working on constructing a biography page filled with text content, and I aim to utilize CSS GRID instead of floats. I encountered an issue in getting the text to occupy all the available white space within the layou ...

supplying various color variables to a single class

I am working on a webpage with multiple elements sharing the same CSS class. I want each element to have a unique background color, but I don't want to create a bloated CSS file. Is there a way to achieve this by adding a parameter in my HTML like cla ...

Error in WordPress: The table prefix cannot be blank

While setting up WordPress, I encountered an issue during the database details input in the first step of the installation process. I am using XAMPP and have tables in my database named wordpress_tbl_table1 and wordpress_tbl_table2. I entered "wordpress_tb ...

Is there a way for me to perfectly align the image and the h1 header on the webpage?

I'm facing some challenges when it comes to aligning an image and an h1 tag on the same line. I've already tried using display: inline and inline-block, but they only affect the container of the two elements. I also set the width to 100% on the s ...

Child element fails to show up upon parent element's appearance

My issue involves having two divs nested inside each other. I programmed it so that when I hover over a link, both divs should appear. Strangely, only the parent div is visible while the child div remains hidden. However, if I set the parent to "display:bl ...

Issue with Google Chart overlapping drop down menu in CSS紿orizontal scrollingyan

I've exhausted all my options. I've experimented with positioning and z-indexes, but no matter what I try, the drop-down menu remains hidden behind the google chart. It functions properly on Firefox, but not on Chrome or IE. Any helpful advice wo ...

Trigger a fixed bottom bar animation upon hover

I have a bar fixed to the bottom of the browser that I want to hide by default. When a user hovers over it, I want the bar to be displayed until they move their cursor away. <!doctype html> <html> <head> <meta charset="utf-8"> &l ...

Understanding the hierarchy of LESS css class structure is crucial for

In my chat contact list, each contact can have a different state represented by classes: .online .offline .online .selected .offline .selected Here are the corresponding styles (example): .online { background-color: #fff; p { color: #000; } } . ...

The img:first-of-type selector targets specifically the first image within a group

When a user visits my website using a touch device or without Javascript support, I want the first of two images to display. To achieve this, I am targeting them using Modernizr. The menu button at the top of the page: <img src="menubutton.png" alt="M ...

The background of the div fails to appear

Why is my div's background not displaying properly? Check out the code below: <div style=" width:676px; height:230px; display: inline; margin: 0 0 0 0; background-image: url('http://www.goodsstall.co.uk/ekmps/shops/goo ...

Is it possible to incorporate an element using absolute positioning using jQuery or Javascript?

When trying to add a new element on the screen, I am facing an issue with the absolute positioning not working properly. Sample Code in Javascript function drawElement(e,name){ $("#canvas").append("<div id='" + name + "' class='e ...

Another class is overriding the border of the text-box

I'm facing a challenge in customizing the material ui css. Specifically, I want to set the border color of a textbox to red. The issue arises when the bottom border gets overwritten. Upon investigation, I discovered that the culprit is the class MuiIn ...

react-datepicker displaying error message "Preventing default action not possible within a passive event listener invocation"

I've integrated the react-datepicker library in portal mode. While it functions well on browsers, I encounter an error when using mobile browser mode. An issue arises stating: "Unable to preventDefault inside passive event listener invocation" Des ...