Tips for adjusting the width of the child list within a flex:auto element

Hello, I need help with a CSS issue. I have a layout with two columns: column A and column B. Column B has a fixed width of 100px, while column A should fill the remaining width. Inside column A, there is a list of child components that are added horizontally.

The problem arises when a new child component is added - it causes column A's width to extend beyond its limits, pushing column B down.

How can I ensure that when a child component exceeds the width of column A, it is automatically moved to the next line within column A?

Answer №1

To achieve the desired layout, apply flex-wrap to both the overall container and box A. Additionally, set the width of box A to calc(100% - 100px).

body {
  width: 100vw;
  padding: 0;
  margin: 0;
  color: white;
}

#flex {
  display: flex;
  background-color: grey;
  width: 100vw;
  height: fit-content;
  flex-wrap: wrap;
}

#a {
  width: calc(100% - 100px);
  height: fit-content;
  background-color: red;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
  align-items: center;
}

#a .x {
  width: 100px;
  margin: 10px;
  background-color: green;
  height: 200px;
}

#b {
  width: 100px;
  height: 500px;
  background-color: blue;
}
<div id="flex">
  <div id="a"> A
    <div class="x">X</div>
    <div class="x">X</div>
    <div class="x">X</div>

  </div>
  <div id="b">
    B
  </div>
</div>

For a single 'X' div: https://i.sstatic.net/xPQeF.png

For four 'X' divs:https://i.sstatic.net/oHgFl.png

Answer №2

Forget about using flex, switch to display grid instead. The beauty of grid lies in the ability to specify whether an item should behave dynamically or statically.

Take a look at this example:

.main {
  display: grid;
  grid-template-columns: 1fr 100px;
}
<div class="main">
  <div class="dynamic-size">
    I am dynamic in size
  </div>
  <div class="static-size">
    I'll always be 100px
  </div>
</div>

The 1fr specified in grid-template-columns signifies one free space, indicating that all available space except for the 100px dedicated to the second element will be occupied by the first item.

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

Mastering Bootstrap: Achieving flawless column stacking in succession

I am currently integrating bootstrap into my existing HTML code, but I only require it in two specific sections to avoid rewriting the entire code. Everything looks fine on my 1080p screen as intended. However, when I resize the screen slightly The titl ...

Set default values for input fields based on selected options in php and mysql

I need help creating a form that will submit details when an option is selected from a database. The MySQL table has the following fields under USERS : [email], [age], [name]. I want to automatically fill in the values of other input fields when a user s ...

I am looking to add values to this HTML request

I have come across this code that helps convert an HTML file being read in .xls format to Google Sheets. The code provided below serves the purpose of finding HTML tables in Google Sheets, but I am looking for a way to append these tables. Here is an exam ...

Centered Menu with Left Alignment

I am trying to figure out how to align a sublist directly under a main list point that is centered. I have attached some images to show what I am aiming for: Currently, when I center the main list point (LEISTUNGEN), I am facing issues placing the sublist ...

Unlocking the secrets of text parsing

When retrieving a text field from a document using JQuery, it sometimes includes <em></em> or <cite></cite> tags that I want to remove in order to get the raw text. To retrieve the text by its id, I use the following code: $(' ...

Copy and paste the code from your clipboard into various input fields

I have been searching for a Vanilla JavaScript solution to copy and paste code into multiple input fields. Although I have found solutions on the internet, they are all jQuery-based. Here is an example of such a jQuery solution <input type="text" maxl ...

After clicking, the radio button in Bootstrap 4 shifts to the left

I encountered an issue where the radio button I added to a table using Bootstrap 4 moves slightly to the left whenever I click on it. To better illustrate the problem, I have included a GIF below: https://i.sstatic.net/4aj8f.gif Below is the code for one ...

Creating an image button within a form using Twitter Bootstrap

My current situation involves having images like this: <img src="image1.jpg" width="80" height="80" id = "wp" alt="workplace"> <img src = "image2.jpg" width="80" id = "tm" height="80" alt="team"> and then writing the onclick event for ...

"Struggling to transform HTML form data into JSON format, only to end up with

I've been struggling to convert an HTML form into JSON. Unfortunately, when I try to submit the form, my alert only shows an empty array. Any assistance with this issue would be highly appreciated! Here is the form: <form action="" method="post" ...

What could be the issue preventing .find from functioning correctly with this other css selector in JQuery?

I'm facing an issue with the .find method when using a name=value selector. Despite having the correct syntax and knowing that the object I am selecting from contains 7 elements with the desired attribute, the .find is unable to locate any elements. T ...

Designing a webpage feature that enables users to choose an image from the selection

Can someone help me with coding a feature on my page where users can simply click an image to select their favorite design? I want the selection to be recorded and sent to me without requiring any text input from the user. I plan to use jQuery to display t ...

Unable to extract text from a div using web scraping

While attempting to scrape text from a div, I encountered an error that is displayed below. Any assistance with resolving this issue would be greatly appreciated! from selenium import webdriver import pandas as pd driver = webdriver.Chrome(executable_pa ...

What are the steps to adjust the width of a website from the standard size to a widescreen

Is it possible to create a "floating" screen adjustment on websites? I know you can set the standard size of pixels, but how do sites adjust for different screen sizes like wider laptop screens? Does it automatically detect the reader's screen size an ...

Monitor and refresh the Vue Nuxt api directory

Is there a way to make Nuxt watch for "non standard" directories and automatically recompile/reload itself, especially for directories containing additional server APIs? For example, I have my express API in ~/api/. Even though I reference this directory ...

What is the method for calculating the total price based on three distinct data attributes?

I have chosen an option that calculates the sum of values with data attributes. It adds up #mark1 + #series1 + #mark2 + #series2 function validate() { var $selected = $('#mark1, #series1,#mark2, #series2').children(":selected"); var sum = ...

Incorporate Bootstrap styling into a layout featuring a row containing numerous input fields

I need some help with styling using bootstrap 5. I have set up a codepen for reference: https://codepen.io/korazy/pen/xxmWGOx Here are the issues I am facing: The input height changes to match the row height when text wraps. How can I keep the input at ...

Transforming a few pages to be generated server-side using Vue3

I am exploring ways to convert just two pages within my Vue3 project to be server-side rendered for improved SEO without having to switch to Nuxt.js. Your assistance in this matter would be greatly appreciated. My attempt to use Node.js configurations ba ...

I encountered an issue during test payment where the error message "The class 'Stripe' was not found" appeared

Upon clicking the submit payment button, I encountered the following error message: Fatal error: Class 'Stripe' not found in /opt/lampp/htdocs/stripe-php/stripe_api.php on line 10 Despite including the Stripe.php file in my code using require ...

How to select the final td element in every row using JQuery or JavaScript, excluding those with a specific class

I am looking for a solution with my HTML table structure: <table> <tbody> <tr> <td>1</td> <td>2</td> <td class="treegrid-hide-column">3</td> < ...

Ways to obtain an attribute through random selection

Figuring out how to retrieve the type attribute from the first input element: document.getElementById('button').addEventListener('click', function() { var type = document.querySelectorAll('input')[0].type; document.getE ...