Flexbox children are being resized by the text instead of wrapping as expected due to the use of flex-grow

I'm currently experimenting with flex-grow and media queries to create a responsive website design. However, I've encountered an issue where instead of text wrapping as expected, it's resizing the containing divs and causing alignment problems. Can anyone provide insight into why this might be happening?

I suspect that the problem may be related to my use of flex-grow. I've attempted to set minimum and maximum widths, as well as different word wrapping properties, but haven't been successful in resolving the issue.

Below is the code snippet:

jsFiddle

<div id="container">
  <div id="title">
    <h1>Title</h1>
  </div>
  <div class="work" id="motion">
    <h2>Sub</h2>
  </div>
  <div class="work" id="print">
    <h2>Sub</h2>
  </div>
  <div class="work" id="interaction">
    <h2>Sub</h2>
  </div>
</div>
* {
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
  width: 100%;
}

h1,
h2,
h3,
h4,
h5,
h6,
p {
  font-family: sans-serif;
  font-weight: normal;
  max-width: 100%;
}

#container h1,
h2 {
  padding: 1rem;
}

#container {
  height: 100%;
  display: flex;
  flex-direction: row;
}

#title {
  background-color: red;
  flex-grow: 2;
  margin: 1em;
  min-height: 15em;
}

.work {
  margin: 1em;
  flex-grow: 1;
  background-color: gray;
  min-width: 4em;
}

#motion,
#print,
#title {
  margin-right: 0;
  margin-bottom: 1em;
}

#interaction {}

@media only screen and (max-width: 1020px) {
  body {
    background-color: blue;
  }
  #container {
    flex-direction: column;
    flex-wrap: wrap;
    height: 100%;
  }
  #motion,
  #print {
    margin-right: 1em;
    margin-bottom: 0;
  }
  .work {
    min-height: 10em;
  }
  #title {
    height: 100%;
  }
}

@media only screen and (max-width: 600px) {
  body {
    background-color: yellow;
  }
  #motion,
  #print,
  #title {
    margin-right: 1em;
    margin-bottom: 0;
  }
  #container {
    flex-wrap: nowrap;
  }
}

Answer №1

The flexbox children in your code do not have specific widths assigned, which causes them to expand when there is more text content.

Check out the jsFiddle link for a visual representation.

To address this issue, you can implement the following CSS:

#container > div {
  width: calc(25% - 2em); /* Adjusted for the 1em margin on left and right */
}

@media only screen and (max-width: 1020px) {
    #container>div {
       width: calc(33% - 2em);
     }
}

@media only screen and (max-width: 600px) {
    #container>div {
       width: calc(100% - 2em);
     }
}

* {
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
  width: 100%;
}

h1,
h2,
h3,
h4,
h5,
h6,
p {
  font-family: sans-serif;
  font-weight: normal;
  max-width: 100%;
}

#container h1,
h2 {
  padding: 1rem;
}

#container {
  height: 100%;
  display: flex;
  flex-direction: row;
}

#container>div {
  width: calc(25% - 2em);
}

#title {
  background-color: red;
  flex-grow: 2;
  margin: 1em;
  min-height: 15em;
}

.work {
  margin: 1em;
  flex-grow: 1;
  background-color: gray;
  min-width: 4em;
}

#motion,
#print,
#title {
  margin-right: 0;
  margin-bottom: 1em;
}

#interaction {}

@media only screen and (max-width: 1020px) {
  body {
    background-color: blue;
  }
  #container {
    flex-direction: column;
    flex-wrap: wrap;
    height: 100%;
  }
  #container>div {
    width: calc(33% - 2em);
  }
  #motion,
  #print {
    margin-right: 1em;
    margin-bottom: 0;
  }
  .work {
    min-height: 10em;
  }
  #title {
    height: 100%;
  }
}

@media only screen and (max-width: 600px) {
  body {
    background-color: yellow;
  }
  #motion,
  #print,
  #title {
    margin-right: 1em;
    margin-bottom: 0;
  }
  #container {
    flex-wrap: nowrap;
  }
  #container>div {
    width: calc(100% - 2em);
  }
}
<div id="container">
  <div id="title">
    <h1>Title</h1>
  </div>
  <div class="work" id="motion">
    <h2>Sub Sub Sub Sub Sub Sub Sub Sub Sub Sub </h2>
  </div>
  <div class="work" id="print">
    <h2>Sub</h2>
  </div>
  <div class="work" id="interaction">
    <h2>Sub</h2>
  </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

How can AngularJS focus on the last element using ng-repeat and md-focus?

, there is a code snippet that utilizes AngularJS ng-repeat directive to iterate through 'items' and 'md-autofocus' attribute to focus on the last element. The code is contained within an md-dialog with vertical scrolling functionality. ...

My latest project involves creating a Selenium BOT that can navigate to Google Classroom and join online meetings effortlessly

I'm currently working on creating a selenium BOT that will navigate to Google Classroom and initiate a meeting. However, I'm encountering an issue where the camera is not turning off during the Google Meet session. As I'm fairly new to selen ...

Customize the appearance of select elements using jQuery with the dropkick.js plugin, now compatible

Greetings! I recently added Dropkick.js to my website, resulting in some visually appealing select elements. However, I encountered an issue on iOS devices where I am unable to scroll through the options. After reviewing the Dropkick.js documentation, it ...

The form submission feature is malfunctioning due to JavaScript issues

I have forms that allow file uploads (images), and I am trying to restrict the size of those images to 500KB. However, for some reason, the forms are not submitting and I am unsure why. Below is the jQuery code: $('form').on('submit', ...

Ways to conceal #div element from displaying in the href attribute within the anchor tag

My anchor tag has an href attribute that looks like this: <a onclick='loadReview(\"" + strexternalURL + "\");' href='#productName1'. When clicking on it, the URL appears as http://localhost:54986/Dealerlist.aspx#productName ...

Is there a way to eliminate a style from the final element of a list?

I have just completed a project that looks similar to this: I am wondering if there is a way to remove the line at the end of the last column? (it's within a PHP while loop) Here is the code snippet: <div style="border-bottom: 1px solid #cdc ...

Trouble aligning image in the middle with bootstrap

After attempting the solutions proposed in previous posts, I find myself sharing my amateur code where the embedded image refuses to center and I am at a loss as to why. The image is meant to be positioned in the middle of a login screen/box; it worked for ...

Clearing a signature pad in Angular has been a useful functionality for me. I incorporated two signature pads into my project, and discovered that clicking the clear button in the second

@ViewChild(SignaturePad) signaturePadStaff!: SignaturePad; @ViewChild(SignaturePad) signaturePadCustomer!: SignaturePad; i used for different signature pad like this.enter image description here Customized Staff Signature Card <div class="col- ...

Always keep this checkbox checked and only uncheck the others when necessary

Whenever I check a checkbox and then click on another checkbox, I don't want the initially checked checkbox to be unchecked, but rather a different one... For example: If I check checkbox 1, only checkbox 1 is checked; If I check checkbox 2, both ...

Improve the readability of a series of string.replace statements

When dealing with HTML code in Python, special characters need to be replaced using the following lines of code: line = string.replace(line, "&quot;", "\"") line = string.replace(line, "&apos;", "'") line = string.replace(line, "&amp ...

Exploring the engaging section of interconnected images featuring rounded edges

I'm currently working on a unique widget that can be found at this link: http://codepen.io/JonnyNineToes/pen/zGYxwV This widget features an image that reveals additional information when clicked, with the extra info sliding out from behind the image. ...

When I open my website in the browser, the bootstrap card-deck design is not being displayed correctly

I encountered a strange issue while designing my website. The code I wrote for the Bootstrap card-deck class in codeply.com was working perfectly fine. However, when I copied the same code into my PyCharm code editor and then pasted the file link into my b ...

The final input is the only one that functions

I am currently working on a large form, but I am encountering a major issue with it. In certain sections of the form, only the last input field seems to be functional. For example: [...] <div class="main_data"> <span class="info">main dat ...

Having trouble getting the full width of a hidden div to work properly?

I am facing an issue with a two-part menu design: the left side consists of a traditional UL, while the right side contains a link within a div element. The fixed-width of the right div is causing complications as the left div needs to occupy all remaining ...

Deactivate the image button when no image has been chosen from the input field

Is it possible to have the image button disabled when no image is selected in the input field? The button should be inactive if an image is not chosen. import { useState } from "react"; const CommentForm = ({ handleSubmit, submitLabel }) => ...

Only the box that I click on will receive the applied color

I am facing an issue with applying colors to the label colorCheckBox, each with a unique data-id that is derived from the variable colorBoxId, which is globally accessible using colorBoxId = $(this).closest('tr').data('id');. The proble ...

Is there a way to connect a function to a div using anchor tags that mimic a dropdown menu?

Here is the HTML code for a navigation bar that I have created <div class="navbar"> <a href="#home">Home</a> <div class="dropdown"> <button class="dropbtn">Categories ...

Getting the x-axis points on Google charts to start at the far left point

I have integrated the Google API for charts into my application and am using multiple charts on the same page. However, I am facing an issue with setting padding for the charts. When I include more data points, the chart area occupies more space in the div ...

Turn off the second dropdown menu if the first one has not been chosen

I need to implement a feature where the second dropdown is disabled if the first dropdown is not selected. Is there a way to achieve this using JavaScript? The requirement is that if the user does not select an option from the first dropdown, the second dr ...

Is it possible to change the title of a QGroupBox using HTML expressions in Python?

Does anyone know how to set the title of a QGroupBox with HTML expressions in a Python program? For example, using ABC for subscript. If you have any insights or solutions, please share! Thank you. ...