Neighbor wrapping causing Flex to shrink

Is there a way to make the element shrink when the flex container below grows due to flex-wrapping? Currently, the new space is created below the container instead of shrinking the one above. The flex-grow and flex-shrink properties do not seem to work as expected when wrapping occurs. The goal is to ensure that everything fits within the viewport without requiring the user to scroll.

https://i.sstatic.net/SMO3h.jpg

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
}

.main-container {
  display: flex;
  width: 100vw;
  height: 100vh;
  flex-direction: column;
}

.main-container>* {
  text-align: center;
}

.shrink {
  background-color: #c0caad;
  padding-top: 30%;
  height: 80vh;
  width: 100vw;
  flex-shrink: 0;
}

.grow {
  height: 20vh;
  width: 30vw;
  background-color: #9da9a0;
  display: flex;
  flex-wrap: wrap;
  flex-grow: 1;
}

.grow>* {
  border: 5px solid #654c4f;
  height: 100%;
  background-color: #b26e63;
  width: 50%;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <link rel="stylesheet" href="style.css" />
</head>

<body>
  <div class="main-container">
    <div class="shrink">Shrink</div>
    <div class="grow">
      <div class="growItems">Wrap</div>
      <div class="growItems">Wrap</div>
      <div class="growItems">Wrap</div>
    </div>
  </div>
</body>

</html>

Answer №1

It seems there are a few issues with your CSS that need to be addressed. Take a look at the updated snippet below for clarification.

1. flex-shrink: 0; is preventing the element from shrinking properly.

To ensure flex-children can grow and shrink as needed, consider using flex: 1; or adjusting the shrink/grow values rather than setting it to 0.

2. Setting fixed heights of 80vh and 20vh will keep them static.

This means they will always remain at these specific heights instead of adjusting proportionally based on the layout. It may be best to remove these height values.

3. Using padding-top: 30% pushes content down and affects height calculations.

If this padding was added just for placeholder text positioning, it could be interfering with the flexibility of the layout. Consider removing it if unnecessary.

4. Combining width: 50% with border: 5px without box-sizing: border-box can cause unexpected wrapping.

Without setting box-sizing property correctly, the width calculation may not behave as expected when borders are added. Make sure to include box-sizing: border-box; for accurate sizing.

Below are some cleaned up suggestions based on the provided drawing in your question.

html, body {
  padding: 0;
  margin: 0;
}

.main-container {
  display: flex;
  border: 1px solid red;
  width: 100vw;
  height: 100vh;
  flex-direction: column;
}

.main-container>* {
  text-align: center;
}

.shrink {
  background-color: #c0caad;
  width: 100vw;
  display: flex; 
  align-items: center;
  justify-content: center;
  flex: 3;
}

.grow {
  background-color: #9da9a0;
  display: flex;
  flex-wrap: wrap;
  flex: 1;
  align-items: flex-start;
}

.grow>* {
  box-sizing: border-box;
  border: 5px solid #654c4f;
  height: 50px;
  background-color: #b26e63;
  flex-basis: 50%;
}
<div class="main-container">
  <div class="shrink">Shrink</div>
  <div class="grow">
    <div class="growItems">Wrap</div>
    <div class="growItems">Wrap</div>
    <div class="growItems">Wrap</div>
    <div class="growItems">Wrap</div>
    <div class="growItems">Wrap</div>
    <div class="growItems">Wrap</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

Ensure that the default value in the text box remains unchanged until new text is input by utilizing jQuery

Can you please review my code snippet on http://jsfiddle.net/7995m/? The issue I'm facing is that the default text in the text box disappears as soon as it's clicked. What I want is for the default text to remain until the user starts typing. Her ...

What is the best method for restricting the visibility of an animation to specific regions within an SVG?

I am seeking assistance with adding a "shine" animation to my SVG. I want the animation to only appear within specific parts of the SVG, but I'm uncertain about how to achieve this. Below is what I have accomplished so far. The aim is for the white ...

Unable to reinitialize the DataTable using Angular Datatable

I've been working on an Angular application that has a simple CRUD functionality. Initially, I tested my data with a static HTML table and everything was functioning as expected. However, I decided to implement a data table framework called Angular da ...

The Art of Capturing Sound: Unleashing

Currently, I am attempting to capture video via html5 .getUserMedia and play it back without needing to upload it to a server. Despite following numerous tutorials, I have only been successful on Chrome by utilizing canvas to draw the webp image and then c ...

Is it possible to have a full-width thumbnail navigator in a full-width slider container with Jssor Slider?

I apologize if this topic has already been discussed, In a responsive full-width slider container, I am utilizing thumbnail navigator 03 with the scaling option disabled. Is it feasible to have a fixed-height, but fully (100%) width thumbnail navigator t ...

Struggling to align form inputs next to each other

I have tried various methods such as adjusting padding and margins, using tables, displaying as inline and inline-block, and separating parts of the code into different divs. However, I am still unable to get my inputs to be displayed side by side. Here is ...

The callback function fails to execute the click event following the .load() method

Hey there, I've hit a roadblock and could really use some help figuring out where I went wrong. Let me break down my script for you. On page1.html, I have a div that gets replaced by another div from page2.html using jQuery's .load() method. Here ...

What is the best way to deselect the first radio button while selecting the second one, and vice versa, when there are two separate radio groups?

I am looking to achieve a functionality where if the first radio button is selected, I should receive the value true and the second radio button should be unselected with the value false. Similarly, if the second radio button is selected, I should receive ...

Save to clipboard HTML with forward slash

I need help creating a button that will copy a specific text to the clipboard when clicked. <button onclick="copyToClipboard('A\B\C\D')">Click here</button> However, I've encountered an issue where the text copie ...

When an image is clicked, I would like it to redirect to a different page

In my slider image list, each image has an ID. If the ID becomes equal to a specific number, such as 24, I want that particular image to move to another page when clicked. Here is the code snippet: <?php $sql = "select * from ".$tblImages." Where cid= ...

"Learn the step-by-step process of generating a transcript with a WebVTT file in JWPlayer

We have integrated JWPlayer into our website and are now looking to add transcript captioning. I have attempted to create a sample application using regular HTML code, but have not been successful. Despite reviewing multiple tutorials, I was unable to ach ...

Leverage the power of Next.js to dynamically render a specific section of an HTML webpage

I want to incorporate Next.js into a specific section of my website, while the rest of the site is built using different frameworks that are not even based on React. I aim to use Next.js within a <div id="nextjs_div">...</div> element ...

How to disable the Rubber/Elastic scrolling feature on your iPad?

On my webpage, there is a combination of vertical and horizontal scrolling. Everything works fine when the user swipes left or right exactly, but issues arise when the swipe is not precise along a straight line (diagonally). In this case, both vertical and ...

Click the link to capture the ID and store it in a variable

Currently working on a project involving HTML and JavaScript. Two HTML pages ("people.html" and "profile.html") are being used along with one JavaScript file ("people.js") that contains an object with a list of names, each assigned a unique ID: var person ...

Oops! Looks like we have encountered an issue with reading the property 'checked' of nothing

Whenever I click the checkbox, an image should be displayed. Unfortunately, I encountered an error: Uncaught TypeError: Cannot read property 'checked' of null at (index):185 at dispatch (VM576 jquery.min.js:3) at i (VM5 ...

How can you iterate over the input elements that are currently visible within a form using Javascript?

Is there a way to clear the values of all visible input fields in a form using JavaScript? I'm currently struggling with setting text inputs to empty as they come out as undefined. Any suggestions on how to achieve this? ...

XML powered jQuery menu

Admittedly, I have not yet used XML with jQuery. Below is a snippet of the XML: <folder title="Home"> <item title="Welcome" /> <folder title="My Photos"> <folder title="Holiday"> <item title="Photo 1" /> ...

Media Queries seem to be unresponsive on Tablet and Desktop devices

I've been trying to resolve this issue for quite some time now, but I'm still unable to find a solution. My goal is to center the Wufoo Forms visually on different screen sizes using media queries, however, despite rewriting them multiple times a ...

Text aligned vertically within an element using the table-cell display attribute in the Chrome browser

Struggling to align text inside an element styled with display: table-cell? The issue arises when dealing with multiline text in a tab format. Achieving the correct vertical-align: middle; requires displaying it as a table-cell, which works well in most ca ...

The quickForm Meteor encountered an exception in the template helper: There was an error stating that Recipes is not within the window

I'm having trouble getting the app to work on Meteor. The quickform is not connecting to my Collection. "Error: Recipes is not in the window scope" Can anyone offer assistance with this issue? Below is my quickform code: <template name="NewRe ...