Challenge with organizing space within a nested layout of flexboxes and CSS grids

Is it possible for my aside element and div.content to evenly split the available space in the container? Additionally, should the grid within div.content take up all the vertical space?

The issue I'm encountering is that grid items aren't able to adjust their height dynamically. Also, the vertical space taken by the grid (due to grid-gap) is also affecting the heights of both the aside element and div.content, causing a disparity in their heights.

The height of aside should ideally be equal to the height of div.content minus the height of the grid itself.

In summary, setting a height for div.content using flexbox seems restricted, with child elements behaving more like siblings rather than being contained within it.

* {
  box-sizing: border-box;
}

body {
  min-height: 100%;
  margin: 0;
  display: flex;
  flex-direction: column;
}

html {
  height: 100%;
}

header,
footer {
  background-color: #1A1C22;
  height: 100px;
}

.container {
  flex: 1 0 400px;
  display: flex;
  flex-direction: column;
  padding-top: 12px;
}

aside {
  background-color: #6C757D;
  flex: 1 1 auto;
}

.content {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: repeat(6, 1fr);
  grid-gap: 20px;
  border: 2px solid blue;
}

section {
  background-color: #343A40;
  border: 1px solid red;
}
<body>
  <header></header>
  <div class="container">
    <aside></aside>
    <div class="content">
      <section></section>
      <section></section>
      <section></section>
      <section></section>
      <section></section>
      <section></section>
    </div>
  </div>
  <footer></footer>
</body>

Answer №1

If you want to ensure compatibility with both Chrome and Firefox, it's best to explicitly set the `height` instead of using `flex-basis`. Additionally, make sure to apply `flex: 1 0 auto` to the `.container`, and use `flex: 1` instead of `flex: 1 1 auto` for both `content` and `aside`. This adjustment sets `flex-basis: 0` instead of `auto`. You can refer to the demo below and this fiddle for more details:

* {
  box-sizing: border-box;
}

body {
  min-height: 100%;
  margin: 0;
  display: flex;
  flex-direction: column;
}

html {
  height: 100%;
}

header,
footer {
  background-color: #1A1C22;
  height: 100px;
}

.container {
  /* flex: 1 0 400px; */
  display: flex;
  flex-direction: column;
  padding-top: 12px;
  height: 400px; /* added */
  flex: 1 0 auto; /* added */
}

aside {
  background-color: #6C757D;
  flex: 1; /* changed */
}

.content {
  flex: 1; /* changed */
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: repeat(6, 1fr);
  grid-gap: 20px;
  border: 2px solid blue;
}

section {
  background-color: #343A40;
  border: 1px solid red;
}
<header></header>
<div class="container">
  <aside></aside>
  <div class="content">
    <section></section>
    <section></section>
    <section></section>
    <section></section>
    <section></section>
    <section></section>
  </div>
</div>
<footer></footer>


However, there are a few shortcomings in the above approach:

  • It may not be immediately clear that specifying the `height` is necessary for proper behavior in column flexboxes,

  • There is still a 4px height difference between the `aside` and `content` due to the 2px border of the content.


Consider Using CSS Grid for Layout

A grid layout offers an easy solution to these problems - while flexboxes excel in 1-D scenarios, they can fall short in complex layouts like this one. In this case, I would remove the `container` wrapper and opt for a 4-row layout (`grid-template-rows: 100px minmax(200px, 1fr) minmax(200px, 1fr) 100px;`):

* {
  box-sizing: border-box;
}

html {
  height: 100%;
}

body {
  min-height: 100%;
  margin: 0;
  display: grid;
  grid-template-rows: 100px minmax(200px, 1fr) minmax(200px, 1fr) 100px;
}

aside {
  background-color: #6C757D;
}

.content {
  display: grid;
  grid-auto-rows: 1fr;
  grid-gap: 10px;
  border: 2px solid blue;
}

.content section {
  background-color: #343A40;
  border: 1px solid red;
}

header,
footer {
  background-color: #1A1C22;
}
<header></header>
<aside></aside>
<article class="content">
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
</article>
<footer></footer>

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

What are the disadvantages of not incorporating the main CSS file directly into the web page?

Optimizing the loading time of my webpages is a priority for me, especially when it comes to first-time loading and from cache. Despite that, I still prefer to have the browser re-validate all resources to avoid any strange displays or update issues caused ...

utilizing the target attribute within a form to open the link in the current page

I have been working on a web application and implemented a drop-down menu using a form to display information about the selected category. However, I noticed that when an option is selected, the link opens in a new window instead of the same window. Below ...

Shifting static information both above and below a 100vh element

At the moment, I have a stationary image in the center of my screen that moves horizontally when scrolling with the mouse. Now, I want to include a screen above and below this element, each with a height of 100vh. However, when I attempt to do so, the fixe ...

Achieving consistent width for flex items in each row

Can I ensure that all flex items have the same width? Currently, my code looks like this: .video-container { background-color: blue; min-height: 90vh; display: flex; flex-wrap: wrap; } .video { flex: 1 0 25%; } <div class="video-contain ...

Show a variety of logos on the website based on the current date

I've been experimenting with displaying a unique logo on my website based on the current date (for example, showing a Christmas-themed logo during December). Here's what I have so far: <img class="logo" id="global_logo" sty ...

No change in the element's text content when clicking

I'm working on creating a timer that counts down from either 15 or 30 seconds. However, I'm having trouble changing the text value when the 15 button is clicked. Can someone help me figure out what's wrong? Thank you in advance HTML <h ...

HTML and CSS link conflict

As a beginner in CSS, I am experimenting with creating a simple website using HTML and CSS. However, I have encountered an issue where the links on the left side of my page are being interfered with by a paragraph in the middle section. This causes some of ...

scrollable header within the confines of the container

I have been trying to find a solution for my scrolling issue, but I just can't seem to get it right. I want the content within a div to scroll inside its container without affecting the header. Here's the updated JSFiddle link with the jQuery cod ...

Top tips for seamless image transitions

Currently working on a slideshow project and aiming to incorporate smooth subpixel image transitions that involve sliding and resizing, similar to the Ken Burns effect. After researching various techniques used by others, I am curious to learn which ones ...

Using JavaScript to dynamically load Cascading Style Sheets based on the current

I am trying to dynamically load different CSS files based on the current date (season). I attempted to tweak an image script that I found on Stack Overflow, but it didn't yield the desired result. Could someone please guide me on where I might be ma ...

if jade is being inconsistent

I am currently using expressjs in my app and have the following setup: app.get('/profile',index.profile); app.get('/',index.home); Within layout.jade, I have the following code: ... if typeof(username)!=='undefined' ...

What is the best way to design a dynamic gallery that showcases three columns for large screens, but switches to two columns for mobile devices?

My goal is to create an image gallery using Bootstrap, with 3 columns on large screens and 2 columns on mobile. I want to maintain the aspect ratio of each image while fixing the width so they align within the columns. I was able to achieve this on large s ...

Which is the preferable option for creating a circular daily planner: canvas or SVG?

As a novice programmer delving into the world of coding (or at least hoping to), I have a question regarding graphic tools in html5 for my latest project. My goal is to create a planner app using electron, with the unique twist that the planner form sho ...

Having trouble getting buttons to wrap onto a new line instead of spilling over the container

Struggling with getting a JSFiddle to work properly with React and other dependencies, I've decided to share the Github repo link to demonstrate the issue: https://github.com/ishraqiyun77/button-issues/ The main problem arises when a group of button ...

Tips for implementing a document ready function on a nested page within a larger full-page website

I am currently working on a website that utilizes fullpage.js, but the same principle applies to all single-page websites. I am trying to figure out how to implement the $(document).ready() function on a 'nested' page within the site. Since every ...

Using Bootstrap 4, create a responsive design that centers images within a div on mobile devices and positions them differently

I am currently working on a DIV that contains social media buttons. The code for this DIV is as follows: <div class="redes-sociales col-sm-pull-6 col-md-6 col-lg-push-4 float-right py-2"> <img class="img-rounded img-social" alt="f ...

I want to locate every child that appears after the <body> element, and extract the HTML of the element containing a specific class within it

It may sound a bit confusing at first, but essentially I have some dynamically generated HTML that resembles the following: <body> <div class="component" id="465a496s5498"> <div class="a-container"> <div class="random-div"> ...

How can I use D3.js to form a circular group in an organization structure, link it with a circular image, and connect

Is it possible to create a radial grouped circle using d3.js, similar to the image below: https://i.sstatic.net/1Hwd2.jpg I have written some code as shown below. However, I am facing challenges in connecting every circle with a curved line and displayi ...

Blog entries alternating between a pair of distinct hues

I want to create a design where each post container has a different color from the one next to it. Essentially, I would like the containers to alternate between two distinct colors. The left side shows how it currently appears, while the right side depict ...

Fuel Calculation - Unable to pinpoint the error

My JavaScript Code for Calculating CO2 Emissions I'm working on a program that calculates how much CO2 a person produces per kilometer. This is part of my school project and I'm still learning, so please bear with me... I also just signed up on ...