Flexbox Columns Maintaining Width when Window is Resized

My current challenge involves utilizing Flexbox to create a responsive layout with 5 boxes that shrink when the browser window is resized. However, 4 of these boxes are not reducing their width as expected when placed in columns.

(Despite Bob behaving correctly, Kate, Nick, Jane, and Fred are resisting shrinking their width.)

How can I adjust the column widths so they resize accordingly when the browser window size changes?

.container {
  display: flex;
  width: 100%;
  justify-content: center;
  border: 2px solid red;
}

.column {
  display: flex;
  flex-direction: column;
}

.big {
  display: flex;
  width: 400px;
  height: 400px;
  background: yellow;
  border: 5px solid blue;
}

.small {
  display: flex;
  width: 195px;
  height: 195px;
  background: yellow;
  border: 5px solid blue;
}
<div class="container">
  <div class="big"><span>Bob</span></div>

  <div class="column">
    <div class="small"><span>Kate</span></div>
    <div class="small"><span>Nick</span></div>
  </div>

  <div class="column">
    <div class="small"><span>Jane</span></div>
    <div class="small"><span>Fred</span></div>
  </div>
</div>

View the demo here: https://jsfiddle.net/CodeCabbie/1xvjeo3p/44/

Answer №1

Solution

Transfer the width: 195px property from .small to .column.


Explanation

The reason why the second and third columns are not shrinking is that they are sized within a nested container (.column) rather than the primary flex container (.container). This makes them out of reach for the flex-shrink property.


Code

.container {
  display: flex;
  justify-content: center;
  border: 2px solid red;
}

.column {
  display: flex;
  flex-direction: column;
  width: 195px;  /* updated */
}

.big {
  display: flex;
  width: 400px;
  height: 400px;
  background: yellow;
  border: 5px solid blue;
}

.small {
  border: 5px solid blue;
  /* width: 195px; */
  height: 195px;
  background: yellow;
  display: flex;
}

body {
  margin: 0;
}
<div class="container">
  <div class="big"><span>Bob</span></div>

  <div class="column">
    <div class="small"><span>Kate</span></div>
    <div class="small"><span>Nick</span></div>
  </div>

  <div class="column">
    <div class="small"><span>Jane</span></div>
    <div class="small"><span>Fred</span></div>
  </div>
</div>

Check out the jsFiddle demo

Answer №2

Instead of relying on a flexbox, consider utilizing a grid layout for a more streamlined and comprehensible code structure:

.container {
  display: grid;
  grid-template-columns: 2fr 1fr 1fr;
  /* optional */
  grid-gap: 5px;
  max-width: 800px;
  margin: 0 auto;
  background-color: blue;
  padding: 5px;
}

.big {
  grid-row: span 2;
  min-height: 400px;
  background: yellow;
}

.small {
  background: yellow;
}
<div class="container">
  <div class="big">Bob</div>
  <div class="small">Kate</div>
  <div class="small">Nick</div>
  <div class="small">Jane</div>
  <div class="small">Fred</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

Simple Steps for Dynamically Adjusting Table Cells without Disturbing Existing Data

Is there a way to make a table automatically arrange cells in rows of two, adjusting as necessary when adding or removing cells (with one cell in the last row if there is an odd number of total cells)? For example: <table> <tr> <td>o ...

What is the best way to customize the link style for individual data links within a Highcharts network graph?

I am currently working on creating a Network Graph that visualizes relationships between devices and individuals in an Internet of Things environment. The data for the graph is extracted from a database, including information about the sender and receiver ...

Strange spacing below body element discovered while browsing website in Firefox 7

Currently, I am facing an issue on my website in Firefox 7. A margin is causing the gradient in #wrapper to shift upwards from the bottom, disrupting the layout. Despite resetting the margin to 0 on body and html, I am unable to pinpoint the root of the pr ...

Unresolved styles in React component linked to styles.css file

As I dive into creating a registration page in ReactJS, I encounter a frustrating issue with my styles not applying correctly from the styles.css file. Let's take a look at my RegisterPage.jsx component: export default function RegisterPage() { ret ...

Ways to reveal concealed div elements when hovering the mouse?

Is there a way to display a group of hidden div's when hovering over them? For instance: <div id="div1">Div 1 Content</div> <div id="div2">Div 2 Content</div> <div id="div3">Div 3 Content</div> All div's sho ...

Is it possible to create a sticky element that stays fixed to the window without using JavaScript?

Using position: sticky has been a game-changer for me. It resolves many issues without the need for JavaScript. However, I've encountered a roadblock. I am trying to create a sticky element that is nested inside multiple <div> elements. Since po ...

Enhance your website with Bootstrap 4 by separating text and images in your carousel

I've been struggling to create a slider similar to the one shown in the image. I attempted using the Bootstrap carousel, but ran into issues with inconsistent image sizes and the text on the left side jumping around before settling in its correct posi ...

It seems that Firefox is ignoring the word-wrap style when the class of a child element is changed

Take a look at this: var iconIndex = 0; var icons = ['check', 'chain-broken', 'flag-o', 'ban', 'bell-o']; $('button:eq(0)').click(function() { iconIndex = (iconIndex + 1) % icons ...

Display block disappearance persists even after being set to none

Presented below is my menu design: <div class="span2 main-menu-span"> <div class="well nav-collapse sidebar-nav"> <ul class="nav nav-tabs nav-stacked main-menu"> <li class="nav-header hidden-tablet"><span ...

What's the best way to modify the style property of a button when it's clicked in

I am working with a react element that I need to hide when a button is clicked. The styles for the element are set in the constructor like this: constructor(props) { super(props); this.state = { display: 'block' }; this. ...

Traverse through an array of pictures and add the data to a Bootstrap placeholder within HTML markup

In my quest to create a function that populates placeholders in my HTML with images from an array, I am encountering a problem. Instead of assigning each image index to its corresponding placeholder index, the entire array of images is being placed in ever ...

Fine-tuning the page design

I'm working on a registration page using Bootstrap-5 for the layout. Is there a way to place all elements of the second row (starting with %MP1) on the same row, including the last field that is currently on the third row? See image below: https://i. ...

Centering H1 and dropdown button horizontally using Bootstrap

I positioned a dropdown button next to an H1 tag, but I noticed that they are not perfectly aligned and I want to move the dropdown button up slightly. My project is utilizing Bootstrap 5.1.3 https://i.sstatic.net/muIwB.png <div class="ps-4 pt-4 ...

Tips for resizing all HTML elements when adjusting browser window dimensions

I am working with the following HTML code snippet: <div id="sectionOne" class="container-fluid d-flex flex-column justify-content-center align-items-center" style="height: 80vh;"> <div class="row d-flex justify-content-center" style="color: #00 ...

Is it possible to change the order of elements in the navbar using HTML/Bootstrap depending on the state of the responsive menu toggle?

I am attempting to implement a responsive Bootstrap 4 navbar within Angular 8. When the menu is closed, the element order is correct: COMPANY, BROWSE, LOGIN The issue arises when I open the menu and the #menu receives the navbar-collapse flex attributes: ...

Expand and Collapse Button for Customizing Table Height Individually

I trust everything is going smoothly. A challenge I'm facing involves implementing a button to expand a specific row within a table. Currently, clicking the "show more/show less" button extends all rows when the goal is to do it for each individual ta ...

Centered on the page vertically, two distinct sentences gracefully align without overlapping, effortlessly adjusting to different screen sizes

I had no trouble centering horizontally but I'm struggling with vertical alignment. I want these two sentences to be positioned in the middle of the page, regardless of device size. I attempted using vertical-align without success and then tried posit ...

Determine whether the child element extends beyond the boundaries of the parent element

I am currently working on determining whether a child element is visible within its parent element. To achieve this, I am comparing the width of the parent element to the position of the child element using position().left. Given that I have multiple dist ...

Instructions for changing the background of a specific area to gray

Could use some assistance in changing the red area to grey, tried numerous solutions but can't figure it out. Any help would be highly appreciated! Looking to change the background to a light grey excluding the top bar and navigation. Image Current ...

JavaScript (Create a button that toggles the visibility of an element)

I have implemented a hamburger menu that transforms into an x when clicked. I am looking to modify it so that clicking on the hamburger opens the menu, and clicking on the x closes the menu. Below is the code I have been working with: <!DOCTYPE html&g ...