Tips for combining grid and flex layout to increase the size of a specific flex item

My dashboard at home consists of various independent components with different design structures. Some are managed using CSS Grid, some using CSS Flexbox, and others as default blocks.

I am looking to rearrange these components so they stack on top of each other with one taking up all available space.

The current design

Previously, I had applied justify-content: space-around; to the outermost element (the general application container) to align my components:

.border {
  margin: 2px;
  border-style: dashed;
  border-width: 2px;
}

.boxofelements {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  width: 200px;
}

.element {
  width: 50px;
}

.bigbox {
  display: flex;
  flex-direction: column;
  height: 200px;
  justify-content: space-around;
}
<div class="bigbox border">
  <div class="boxofelements border">
    <div class="element border">one</div>
    <div class="element border">two</div>
    <div class="element border">three</div>
  </div>

  <div class="boxofelements border">
    <div class="element border">one</div>
    <div class="element border">two</div>
    <div class="element border">three</div>
  </div>

  <div class="boxofelements border">
    <div class="element border">one</div>
    <div class="element border">two</div>
    <div class="element border">three</div>
  </div>
</div>

The desired changes

I wanted to update the layout by having the middle box expand to take up all available space below it. Similar to this concept:

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

To achieve this, I removed the justify-content: space-around; property and attempted to make the middle element "grow" using:

.border {
  margin: 2px;
  border-style: dashed;
  border-width: 2px;
}

.boxofelements {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  width: 200px;
}

.element {
  width: 50px;
}

.bigbox {
  display: flex;
  flex-direction: column;
  height: 200px;
  /* no more space around
    justify-content: space-around;
  */
}
<div class="bigbox border">
  <div class="boxofelements border">
    <div class="element border">one</div>
    <div class="element border">two</div>
    <div class="element border">three</div>
  </div>

  <div class="boxofelements border" style="grow: 1">
    <div class="element border">one</div>
    <div class="element border">two</div>
    <div class="element border">three</div>
  </div>

  <div class="boxofelements border">
    <div class="element border">one</div>
    <div class="element border">two</div>
    <div class="element border">three</div>
  </div>
</div>

However, nothing changed.

How can I specify that the element should take up all available space along a specific axis relative to its border? (For example, "below" in my case, or "to the right" if considering a row-based axis.)

Answer №1

perhaps flex-grow could be the solution you are seeking.

.border {
  margin: 2px;
  border-style: dashed;
  border-width: 2px;
}

.boxofelements {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  width: 200px;
}

.element {
  width: 50px;
}

.bigbox {
  display: flex;
  flex-direction: column;
  height: 200px;
  /* no more space around
    justify-content: space-around;
  */
}
<div class="bigbox border">
  <div class="boxofelements border">
    <div class="element border">one</div>
    <div class="element border">two</div>
    <div class="element border">three</div>
  </div>

  <div class="boxofelements border" style="flex-grow: 1;">
    <div class="element border">one</div>
    <div class="element border">two</div>
    <div class="element border">three</div>
  </div>

  <div class="boxofelements border">
    <div class="element border">one</div>
    <div class="element border">two</div>
    <div class="element border">three</div>
  </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

The fixed positioned div with jQuery disappears when scrolling in Firefox but functions properly in Chrome, IE, and Safari

Click here to see a div located at the bottom of the right sidebar that is supposed to behave as follows: As you scroll down the page, the div should change its class and become fixed at the top of the screen until you reach the bottom of the parent el ...

The CSS scale transformation in iOS Safari causes the page to zoom out

I am attempting to implement a text snippet that will "zoom and fade in" upon the page loading. In order to do this, I am creating a div containing the text and applying an immediate transform to it: .whatIwantZoomed { opacity:0; /* Add Vendor Pref ...

What is the process for creating URL query parameters?

What is the process for generating parameters after node?_=xxxxxx? If I am using a Python script to access the URL, how can I retrieve these parameters? edit: I apologize for not providing enough information. This is my first post as a novice. I am atte ...

Bootstrap 4: In collapsed navbar, li items are shown horizontally beside navbar-brand

I've been troubleshooting this issue for hours. It seems like the navbar is not collapsing correctly in Bootstrap 4, causing the next list item to display to the right of the navbar-brand text instead of below it like the other items. <nav class=" ...

Updating HTML using Angular JS with a nested dictionary is a breeze

As a newcomer to AngularJS, I am facing an issue with my code. I have created a dictionary in the controller scope and populated it after an HTTP request (key, value pair). The dictionary is being created successfully and there are no errors, but the HTML ...

What is the best way to populate a div with multiple other div elements?

My current project involves creating a sketchpad using jQuery for an Odin Project assignment. However, I have encountered an issue with filling the canvas (wrapper div) with pixels (pixel divs). The canvas does not populate correctly. Here is the link to m ...

Is there a way to alter multiple classes using hover effect on a single class without the use of JavaScript, only employing

Hello there! I have a question about applying styles to specific classes when hovering over certain elements. Unfortunately, I am unable to make changes to the HTML files and can only work with the CSS file. Take a look at the image below for reference: ht ...

The Dilemma of Hover Effects in Internet Explorer 7

How can I make an image display another image with 4 absolutely positioned links when hovered over? The code currently works in Chrome, Firefox, Safari, and all versions of IE except IE7. Any suggestions for IE7 compatibility? For reference, here is the J ...

Customize your Bootstrap navbar with a specific width and perfectly centered position

Using Bootstrap 4, I aim to customize the inline form in the navbar to have a specific width on laptops and extend to the full screen on mobile devices. Here is how it appears on my mobile device: https://i.sstatic.net/gjewu.png https://i.sstatic.net/KJh ...

The battle between DataBind and control property settings

When considering these two methods: <asp:Label ID="Label1" runat="server"><%# DateTime.Now %></asp:Label> and Label1.Text = DateTime.Now.ToString(); Which approach do you prefer and what is your reasoning behind it? ...

Submitting form based on HTML select input issue

I am facing an issue with a select form where the value resets to "10" every time I send the form. Is there a way to keep the selected option, for example "20", after submitting the form? Below is the code snippet: <form method='get' name=&a ...

Assign a value to an input element with JavaScript and retrieve the value using PHP

Below is a form and input element that can be found in the cart.php file: <form id="cartform" action="cart.php?action=update&pd=<?php echo $row['product_id'] ?>" name="form1" method="post"> <?php $query = "select * from ca ...

Closing a Bootstrap 4 accordion that is dynamically generated within a table when another accordion is opened

Currently, I am utilizing bootstrap-4 to create accordions within a table. Whenever clicking on the image, another tr element is appended which triggers the opening of the accordion. Furthermore, within each accordion, I am adding another tr element, essen ...

Increase the options available in the dropdown menu by adding more selected items, without removing any already selected

I came across this code on the internet http://jsfiddle.net/bvotcode/owhq5jat/ When I select a new item, the old item is replaced by the new item. How can I add more items without replacing them when I click "dropdown list"? Thank you <select id=&qu ...

Tips for hiding a modal when clicking outside of it

function openModal(){ document.getElementById("overlay").style.display='block'; document.getElementById("modal").style.display='block'; } function closeModal(){ document.getElementById("overlay").styl ...

Tips for creating a connecting line between two elements

I am currently working on developing a tree-like structure. I have successfully created numbers within circles using HTML badges. However, I am facing difficulty in drawing lines connecting one element to another. I attempted using left and right diagonal ...

A function that handles HTTP request and response

In order to decrypt data, I need to retrieve the encrypted response by hitting a specific URL and then use that encrypted data along with a key to decrypt it. Initially, I attempted to fetch the data response using POSTMAN, but all I got back was a series ...

Accessing and manipulating web elements using either XPath or CSS selectors

Having trouble selecting a specific piece of information using xpath or css selector. Error messages keep appearing. Can someone help identify the issue? This snippet is from my code: output = driver.find_element_by_xpath("//td[@class_= 'sku']" ...

Information backed by the HTML5 Local Storage feature

Is it possible to use a Local Storage object to store another Local Storage object? Thank you in advance. ...

The size of table cells automatically adjusts when zooming in the browser

While trying to display my table in Chrome, I noticed that the cell sizes of the table change when zooming in. Additionally, the table is rendered incorrectly in other browsers like IE and Firefox. I attempted adjusting the sizes to percentage values but i ...