Tips for achieving the growth of the final item exclusively when it moves to the subsequent row

Is it possible to make the last flex item fill up the remaining space only when it's wrapped to a new row in a flex container with flex-wrap:wrap set? The aim is for the last item, like the "Unknown" button in the example, to expand only when it moves to the next line, without growing when it's on the same line.

Currently, I'm using flex: 1000 1 0 for the previous item and flex: 1 for the last item. However, I'm seeking a solution that avoids such large differences in flex grow values, especially since I may need to adjust the behavior based on media queries, potentially removing flex grow for other items.

.ut_text {
  min-width: 150px!important;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82e0ededf6f1f6f0e3f2c2b6acb4acb0">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">

<div class="m-2 border border-primary" style="width:300px; resize:horizontal; overflow:hidden;">
  <div class="input-group">
    <div class="input-group flex-nowrap" style="flex: 1000 1 0;">
      <input type="text" placeholder="I am placeholder" class="form-control ut_text"/>
      <div class="input-group-append">
        <a class="form-control btn btn-dark">
              Search
            </a>
      </div>
    </div>
    
    <div class="input-group-append" style="flex: 1;">
      <a class="form-control btn btn-outline-danger">Unknown</a>
    </div>
  </div>
</div>

(Please note that any solution should not rely on the properties of the container, as its width/overflow/resize settings are purely for demonstration purposes.)

Answer №1

If you want your items in a flexbox container to behave a certain way, use these flex rules: set flex-grow to 1, flex-shrink to 0, and flex-basis to auto. You can use this shortcut code snippet:

.flex-item {
   flex: 1 0 auto;
}

Once you apply these rules, you can remove any inline styles. Check out the example on Codepen here: https://codepen.io/creativesolutions/pen/rNQvWzm

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 the video fills the entire width of its parent element and adjusts its height accordingly to maintain a 16:9

I am looking to make both videos fill 100% width of their parent element while keeping their aspect ratio intact. The parent element takes up 50% of the window's width, so the videos need to be responsive. I have come across numerous solutions that ...

Utilizing Bootstrap's column grid system (col-sm) to set the width of form controls

Is it possible to utilize col-sm or col-xs within the input tag to specify its width? <div> <input type="button" class="col-sm-2" value="Clear"> <input type="button" class="col-sm-2" value="Confirm"> </div> ...

First, the image is reduced by width until it reaches its maximum width, then the height is adjusted proportion

I have an image with the dimensions of 1200 x 200px. In the center of this image, there is a space measuring 500 x 200px which represents the main content of the entire image. Flanking this main content are additional contents on each side. It is important ...

Unable to manipulate or customize the V-slot component

I recently implemented the v-flip feature on my Nuxt webpage, resulting in the following template: <div class="about__cards"> <vue-flip class="about__single-card" active-click width = "350px" height="450px"> <template v-slot:front> ...

Utilizing JavaScript to enable a Bootstrap 5 dropdown menu to open on hover for desktop users and be clickable for mobile users

I am currently using Bootstrap 5 to design a website and I'm facing an issue with creating a navbar dropdown. On desktop, I want the dropdown to open on hover and redirect the user to a new page when clicked. However, on mobile devices, I only want th ...

Create a distinct CSS animation 'origin' declaration for every <g> element

I am using CSS animation to apply a 'wobble' effect to each letter within a word. The letters are represented by SVG groups <g>. However, in the provided example, the wobbling effect becomes more pronounced with each subsequent letter, wher ...

JavaScript DateTimePicker onChange event malfunctioning

Issue with JS datetimepicker onChange event not functioning. Attempts have been made using OnSelect, OnClose, OnChange, OnHide, OnChangeMonth to trigger the datetimepicker event, but none of them seem to be effective. <input type="text" class="form-co ...

Can multiple bootstrap classes be merged together?

Using various bootstrap classes multiple times within an HTML <form>. Example: <form id="frmData" name="frmData" method="POST"> <div class="row justify-content-sm-center mb-2 align-items-center"> ...

The items in the Bootstrap dropdown are not displaying

I am currently working on a project using Angular 12, and I encountered an issue with my Bootstrap dropdown menu not displaying any items. Below is the HTML code snippet causing the problem: <nav class="navbar navbar-expand navbar-dark"> ...

Discovering a hidden DIV beneath other DIVs without disrupting their visibility - a step-by-step guide

I have created four separate DIVs that divide the screen equally into quarters. Additionally, there is a full-screen DIV designated for canvas. My goal is to ensure that these DIVs do not overlap each other - meaning if a user clicks on a link within one ...

Fuzzy Background to Enhance Your Bootstrap 5 Offcanvas Navigation Menu

Currently utilizing Bootstrap v5.2, I am endeavoring to replicate the image displayed in the link below: Blurry Sidebar Backdrop As per the MDN Documentation, backdrop-filter is intended for blurring the background of an element. .offcanvas-backdrop { ...

How can I align the CSS Horizontal Menu directly under the parent menu instead of its current incorrect x-position?

I am working on creating a horizontal drop-down menu using CSS. However, I'm facing an issue where the submenu either appears all the way to the left of the website (when I use position: absolute) or directly to the left of the menu (when set to posit ...

Using Angular with Bootstrap to implement a typeahead feature

I've been working on getting my Typeahead feature to function properly: <div *ngFor="let item of items; index as i"> <input type="text" name="name{{i}}" [(ngModel)]="item.name" [ngbTypeahead]="searchItem" /> </div> Within the c ...

What are the steps to configure or reach a page located in a subdirectory of a different project within a Laravel/OctoberCMS website

I have developed a website using an OctoberCMS theme, hosted on a DigitalOcean server. I am looking to integrate a separate project (specifically the code from Matomo analytics) onto the same server and create a public page that can be accessed at my_site. ...

The container that utilizes the grid layout is compact and has a near-invisible height

grid-template: display; font-style: "Josefin Sans", sans-serif; size-of-text: 10px; element-height: 0px; spacing-bottom: 0px; spacing-left: 0px; spacing-right: 0px; spacing-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-up: 0 ...

Using JavaScript to insert a value through AJAX

I'm currently working on a website that displays the value of a .TXT file, and here is the progress I've made so far: <script> $(document).ready(function() { $("#responsecontainer").load("info.txt"); var refreshId = setInterval(function( ...

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 ...

Tips for customizing the border radius style of the menu in Vuetify's v-autocomplete component

I am looking to customize the appearance of the drop-down list in the v-autocomplete component by adding a border-radius style, as depicted in the image below. The current design I have achieved closely resembles the visual shown below. Previously, I app ...

Trouble with DevExpress ASPxButton: Some Properties Fail to Update

It's been a while and I'm facing a tough challenge. I need to change the image source of an ASPxButton from the code behind, but it seems like the height, width, and border style are getting lost in the process (resulting in an image with its own ...