Ways to increase the shrinking capacity of flex items when the entire container width is occupied

Recent Post Below: I've replicated a minimized issue: https://codepen.io/MH-123/pen/ExJWQWq?editors=1100 In the provided CodePen, the problem persists. Two divs are present, but flex shrink functionality is not working as expected.

The main flex container consists of a single row with two columns. Each column is also a flex container, but this detail may not be relevant. My goal is for the left column to shrink faster than the right column when the screen width is reduced horizontally, resulting in a proportional reduction in size for the items within the columns.

Despite numerous attempts, such as applying flex shrink to the columns, adjusting flex basis, and eliminating widths, the issue remains unresolved:

https://codepen.io/MH-123/pen/gOymoqM?editors=1100

/* BASE STYLES */
:root {
  --clr-dark: #0f172a;
  --clr-light: #f1f5f9;
  --clr-accent: #e11d48;
}

*,*::before,*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
  line-height: 1.6;
  word-spacing: 1.4px;
  font-family: "Roboto", sans-serif;
  color: var(--clr-dark);
  background-color: var(--clr-light);
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.container {
  width: 50%;
  height: 650px;
  margin: 0 auto;
  border: 10px solid var(--clr-dark);
}

.item {
  background-color: #fb7185;
  
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px solid red;
}

/* END OF BASE STYLES */
.container {
  display: flex;
  flex: row nowrap;
}

.colLeft, .colRight {
  height: 100%;
  border: 5px solid blue;
  
  display: flex;
  flex-flow: column nowrap;
  justify-content: space-evenly;
  align-items: center;
  align-content: space-around;
}
.colLeft {
  flex-grow: 1;
  flex-shrink: 10;
}
.colRight {
  flex-grow: 1;
  flex-shrink 1;
}

.item-1 {
  flex: 0 1 30%;
  width: 95%;
  
  display: flex;
}

.item-a {
  height: 100%;
  width: 50%;
}

.item-b {
  height: 100%;
  width: 50%;
}

.item-2 {
  flex: 0 1 55%;
  width: 95%;
}
.item-3 {
  flex: 0 0 55%;
  width: 95%;;
}
.item-4 {
  flex: 0 0 30%;
  width: 95%;
}
<div class="container">
  <div class="colLeft">
    <div class="item item-1">
      <div class="item item-a">1a</div>
      <div class="item item-b">1b</div>
    </div>
  
    <div class="item item-2">2</div>
  </div>
  <div class="colRight">
    <div class="item item-3">3</div>
    <div class="item item-4">4</div>
  </div>
</div>

edit: Here's another CodePen version where I've removed all widths from the children https://codepen.io/MH-123/pen/abxJqZg?editors=0100

Answer №1

To achieve the desired layout, consider adding flex: 1; to both columns.

.colLeft, .colRight {
  height: 100%;
  border: 5px solid blue;
  
  display: flex;
  flex: 1; /* Utilize flex to make both columns share space */
  flex-flow: column nowrap;
  justify-content: space-evenly;
  align-items: center;
  align-content: space-around;
}

In my experience, this adjustment produced the desired outcome.

I also spotted a few errors that need fixing.

.colRight {
  flex-grow: 1;
  flex-shrink: 1; /* Corrected missing colon */
}

.item-3 {
  flex: 0 0 55%;
  width: 95%; /* Removed extra semicolon */
}

Implement these changes, and I believe you'll be satisfied with the end result.

Answer №2

I utilized JavaScript to retrieve the width of the .container element before resizing it, and then utilized this width as a CSS variable to calculate the flex-basis.

const container = document.querySelector(".container");
container.style.setProperty("--current-container-width", `${container.offsetWidth / 2}px`);
:root {
  --clr-dark: #0f172a;
  --clr-light: #f1f5f9;
  --clr-accent: #e11d48;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
  line-height: 1.6;
  word-spacing: 1.4px;
  font-family: "Roboto", sans-serif;
  color: var(--clr-dark);
  background-color: var(--clr-light);
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.container {
  width: 50%;
  height: 650px;
  margin: 0 auto;
  border: 10px solid var(--clr-dark);
}

.item {
  background-color: #fb7185;
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px solid red;
}


/* END OF BASE STYLES */

.container {
  display: flex;
  flex: row nowrap;
}

.colLeft,
.colRight {
  height: 100%;
  border: 5px solid blue;
  display: flex;
  flex-flow: column nowrap;
  justify-content: space-evenly;
  align-items: center;
  align-content: space-around;
}

.colLeft {
  flex-grow: 1;
  flex-shrink: 1;
}

.colRight {
  flex-basis: max(50%, var(--current-container-width));
  max-width: calc(100% - 53.4px);
  flex-shrink: 0;
}

.item-1 {
  flex: 0 1 30%;
  width: 95%;
  display: flex;
}

.item-a {
  height: 100%;
  width: 50%;
}

.item-b {
  height: 100%;
  width: 50%;
}

.item-2 {
  flex: 0 1 55%;
  width: 95%;
}

.item-3 {
  flex: 0 0 55%;
  width: 95%;
  ;
}

.item-4 {
  flex: 0 0 30%;
  width: 95%;
}
<div class="container">
  <div class="colLeft">
    <div class="item item-1">
      <div class="item item-a">1a</div>
      <div class="item item-b">1b</div>
    </div>

    <div class="item item-2">2</div>
  </div>
  <div class="colRight">
    <div class="item item-3">3</div>
    <div class="item item-4">4</div>
  </div>
</div>

Answer №3

Method 1: To maintain the right column's width, consider setting a min-width. This will cause the left column to adjust its width based on the window size.

.colRight{ min-width: 300px; }

Method 2: If you need to adjust the width of the right column, utilize media queries. The following example demonstrates 4 media queries:

For less than 400px => Ratio is 1 to 4

For between 401px and 800px => Ratio is 1 to 3

For between 801px and 1000px => Ratio is 1 to 2

For greater than 1001px => Ratio is 1 to 1

/* BASE STYLES */
.container {
  display: flex;
}

.colLeft, .colRight {
  height: 100%;
  border: 5px solid blue;
  
  display: flex;
  flex-flow: column nowrap;
  justify-content: space-evenly;
  align-items: center;
  align-content: space-around;
}

@media screen and (max-width: 400px) {
  .colLeft {
    flex: 1;
  }

  .colRight {
    flex: 4;
  }
}

@media screen and (min-width: 401px) and (max-width: 800px) {
  .colLeft {
    flex: 1;
  }

  .colRight {
    flex: 3;
  }
}

@media screen and (min-width: 801px) and (max-width: 1000px) {
  .colLeft {
    flex: 1;
    }

  .colRight {
    flex: 2;
  }
}

@media screen and (min-width: 1001px) {
  .colLeft {
    flex: 1;
    flex-grow: 1;
  }

  .colRight {
    flex: 1;
    flex-grow: 1;
  }
}

Method 3: Alternatively, use media queries with a grid layout instead of flexbox. The concept remains similar, with the following proportions:

For less than 400px => Ratio is 1 to 6

For between 401px and 800px => Ratio is 1 to 4

For between 801px and 1000px => Ratio is 1 to 2

For greater than 1001px => Ratio is 1 to 1

/* BASE STYLES */
.container {
    display: grid;
    grid-template-columns: 1fr 1fr;
}

@media screen and (max-width: 400px) {
    .container{
        grid-template-columns: 1fr 6fr;
    }
}
@media screen and (min-width: 401px) and (max-width: 800px) {
   .container{
        grid-template-columns: 1fr 4fr;
    }
}
@media screen and (min-width: 801px) and (max-width: 1000px) {
    .container{
        grid-template-columns: 1fr 2fr;
    }
}

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

Creating a PHP JSON response that incorporates an HTML layout is a dynamic

I'm having some trouble with a certain issue: I am attempting to develop a jQuery/AJAX/PHP live search bar. Although I am successfully calling search.php, the response displayed in the console always includes the contents of my master.php file (which ...

Tips for integrating Bootstrap JavaScript into a React project

I am encountering an issue while trying to incorporate Bootstrap JavaScript into my web app. I have successfully included the CSS, but I am facing difficulties with Bootstrap JavaScript for components like modals and alerts. My package.json includes: "boo ...

An HTML table featuring rows and columns that can be adjusted in size separately from the content within each cell

Looking to create an HTML table where the columns and rows can be sized independently of the cell content? If a row or column isn't large enough to display all the content, it should simply overflow behind the cell. A solution was found that worked in ...

What is the best way to update the state by either adding to it or replacing it if the key

I'm currently working on a project involving radio buttons and I need to create dynamic state by setting the initial state on the first click, then updating it with subsequent clicks by either concatenating the new value onto the existing state or rep ...

Are There Other Ways to Replicate the Actions of an Anchor Link/Target Pseudo-Class?

New Inquiry: Can the onclick attribute within an <a> tag function the same way as the href attribute? In the code snippet below, I have applied <a href="#view"> to all images, and <a id="close-customlightbox" class="lb-animate" href="#!"&g ...

Importing JSON Data into an HTML File

I need to load a JSON file containing HTML content into my main HTML file upon clicking a button. ABC.json includes: <li><img src="images/picture6.jpg" /></li> <li><img src="images/picture5.jpg" /></li> <li><i ...

Verify if session is in existence

Currently in the process of setting up my NodeJS and Express App, utilizing Passport for authentication through Google Sign In and Login. All functionalities work flawlessly when tested on localhost. The sign-in process is smooth, and upon checking, I can ...

Running a code from a plugin in Wordpress site

I am currently utilizing the "wp-video-lightbox" plugin for WordPress, which generates small floating boxes for my videos. I am interested in incorporating variables like http://www.example.com/?video3 to provide shortcuts similar to what YouTube offers. ...

Send information through a form by utilizing a personalized React hook

I'm having difficulty understanding how to implement a hook for submitting a form using fetch. Currently, this is what I have. The component containing the form: const MyForm = (): ReactElement => { const [status, data] = useSubmitForm('h ...

Troubleshooting problem: Unable to restrict table selections - issue with Material UI table in React

I seem to be overlooking the simple solution here. Currently, I have a ternary on my table. If the length of the selected array is greater than a certain number, a table with disabled checkboxes is rendered. I also implement a different handleClick functio ...

Open the link and input the text into the text box?

Suppose I have the following JavaScript code: <script language="javascript" type="text/javascript"> function addText() { var newText = document.myForm.inputText.value; document.myForm.description.value += newText; } </script> I want t ...

Issue with Angular drag and drop functionality arises when attempting to drop elements within an n-ary tree structure displayed using a recursive template

I am currently exploring the functionality of angular material drag and drop. Within my application, I have implemented an n-ary tree structure. Since the specific form of the tree is unknown beforehand, I have resorted to using a recursive template in or ...

Encountered a runtime error while trying to insert a list item <li> into a paragraph <p> element

Take a look at this code snippet: <%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication18._Default" %> <!DOCTYPE html> <html> <body> <p id="someId"></p& ...

Simple steps to trigger a PHP page from a JavaScript page by utilizing express functions

Currently, I am attempting to showcase a PHP page using JavaScript with express functions. Here is an illustration of the code: var express = require('express') var app = express() app.get('/', function (req, res) { res.send(' ...

Can you navigate to HTML files using Vue router?

Hey there! I'm currently utilizing VueJS along with the webpack template. I've got a variety of components that I can easily showcase with Vue Router. However, the testing framework utilized by my team is Robot Framework and we typically create a ...

emulating an android device to test html5 applications

Recently, I embarked on developing an HTML5 game with the help of VS Code. I envision it being predominantly accessed from PCs and laptops. I successfully configured the Chrome debugger to facilitate debugging the webpage in Chrome. However, I am eager to ...

Error encountered in Express.js blogging app: Issue arises when attempting to filter posts by category, resulting in a "Cast to ObjectId failed" error

Currently, I am developing a blogging application using technologies like Express, EJS, and MongoDB. In the application, I have structured posts into different categories, each stored in its own collection. However, I encountered an issue while attemptin ...

Encountered an error: Object(...) does not conform to function standards in the handleChange method

When using JavaScript, everything works fine. However, when trying to implement TypeScript with the handleChange function, an error is consistently thrown whenever something is typed into the entries. The error message reads: "TypeError not captured: Objec ...

What is the process for turning off caching in CORS-Anywhere?

Encountering an issue with CSRF token validation while making a POST request to an endpoint on a different server using cors-anywhere. The error occurs because the CSRF Token passed to the cors-server is cached, leading to validation failure. Referenced a ...

Does using .stopImmediatePropagation() in the click event of a menu item have any impact on analytical tools?

Scenario I've implemented a navigation menu that loads subpages into a div using AJAX. While everything seems to be working fine, I noticed a bug where if I navigate to a subpage, then return to the main page and revisit the same subpage, it gets loa ...